2 Logging facilities for displaying information on screen, as well as
3 (optional) storing it to a file and transmitting it over the network.
5 Part of the swftools package.
7 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
31 #endif // _MSC_VER > 1000
42 int screenloglevel = 1;
43 static int fileloglevel = -1;
44 static int socketloglevel = -1;
45 static int maxloglevel = 1;
46 static FILE *logFile = 0;
48 static SOCKET logSocket;
50 static int logSocket = 0;
53 static char bLogToSock = 0;
55 static void initlogSocket(char* servAddr, char* logPort);
57 void initLog(char* pLogName, int fileloglevel, char* servAddr, char* logPort, int serverlevel, int screenlevel)
59 screenloglevel = screenlevel;
60 fileloglevel = fileloglevel;
61 socketloglevel = screenlevel;
63 maxloglevel=screenloglevel;
64 if(fileloglevel>maxloglevel && pLogName)
65 maxloglevel=fileloglevel;
66 if(serverlevel>maxloglevel && servAddr)
67 maxloglevel=serverlevel;
77 if (pLogName && fileloglevel>=0)
78 logFile = fopen(pLogName, "a+");
79 bLogToSock = (servAddr && logPort && (serverlevel>=0));
81 initlogSocket(servAddr, logPort);
84 static void initlogSocket(char* servAddr, char* logPort)
90 // check and prepare WinSock DLL
91 WORD wVersionRequested = MAKEWORD( 2, 2 );
93 if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
98 // Confirm that the WinSock DLL supports 2.2.
99 // Note that if the DLL supports versions greater
100 // than 2.2 in addition to 2.2, it will still return
101 // 2.2 in wVersion since that is the version we
104 if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
111 hp = gethostbyname(servAddr);
112 if (hp == NULL) // we don't know who this host is
119 sockaddr_in SocketAddress;
121 memset(&SocketAddress, 0, sizeof(SocketAddress));
122 memcpy((char*)&SocketAddress.sin_addr, hp->h_addr, hp->h_length); // set address
123 SocketAddress.sin_family = hp->h_addrtype;
124 SocketAddress.sin_port = htons((u_short)atoi(logPort));
126 logSocket = socket(hp->h_addrtype, SOCK_STREAM, 0);
127 if (logSocket == INVALID_SOCKET)
133 // try to connect to the specified socket
134 if ( connect(logSocket, (struct sockaddr*)&SocketAddress, sizeof (SocketAddress)) == SOCKET_ERROR) {
144 // close socket communication
149 closesocket(logSocket);
157 static char * logimportance[]= {"Fatal","Error","Warning","Notice","Verbose","Debug"};
158 static int loglevels=6;
159 static char * logimportance2[]= {" ","FATAL ","ERROR ","WARNING","NOTICE ","VERBOSE","DEBUG "};
160 static inline void log(char* logString)
171 logBuffer = (char*)malloc (strlen(logString) + 24 + 15);
174 /*time_t t = time(0);
175 tm*t2 = localtime(t);
176 strftime(dbuffer, 8, "%m %d", t2);
177 strftime(tbuffer, 8, "%m %d", t2);
178 dbuffer[0]=0; //FIXME
183 while(a[l-1] == 13 || a[l-1] == 10)
186 sprintf(timebuffer, "%s", a);
191 sprintf(timebuffer, "%s - %s",dbuffer,tbuffer);
194 // search for <level> field
196 lt=strchr(logString, '<');
197 gt=strchr(logString, '>');
198 if(lt && gt && lt<gt)
201 for(t=0;t<loglevels;t++)
204 if(!strncasecmp(lt+1,logimportance[t],strlen(logimportance[t])))
206 if(!strnicmp(lt+1,logimportance[t],strlen(logimportance[t])))
210 while(logString[0]==' ') logString ++;
217 // sprintf(logBuffer, "%s: %s %s", timebuffer, logimportance2[level + 1],logString);
218 sprintf(logBuffer, "%s %s", logimportance2[level + 1],logString);
220 // we always do exactly one newline.
222 l=strlen(logBuffer)-1;
223 while((logBuffer[l]==13 || logBuffer[l]==10) && l>=0)
229 if (level <= screenloglevel)
231 printf("%s\n", logBuffer);
235 if (level <= fileloglevel)
239 fprintf(logFile, "%s\n", logBuffer);
244 if (level <= socketloglevel)
250 write(logSocket, logBuffer, strlen(logBuffer));
252 send(logSocket, logBuffer, strlen(logBuffer), 0);
259 void msg(const char* format, ...)
263 va_start(arglist, format);
268 char*x = strchr(z,format[1]);
269 if(x && (x-z)>maxloglevel)
273 vsprintf(buf, format, arglist);