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 file is distributed under the GPL, see file COPYING for details */
19 #endif // _MSC_VER > 1000
30 static int screenloglevel = 1;
31 static int fileloglevel = -1;
32 static int socketloglevel = -1;
33 static int maxloglevel = 1;
34 static FILE *logFile = 0;
36 static SOCKET logSocket;
38 static int logSocket = 0;
41 static char bLogToSock = 0;
43 static void initlogSocket(char* servAddr, char* logPort);
45 void initLog(char* pLogName, int fileloglevel, char* servAddr, char* logPort, int serverlevel, int screenlevel)
47 screenloglevel = screenlevel;
48 fileloglevel = fileloglevel;
49 socketloglevel = screenlevel;
51 maxloglevel=screenloglevel;
52 if(fileloglevel>maxloglevel && pLogName)
53 maxloglevel=fileloglevel;
54 if(serverlevel>maxloglevel && servAddr)
55 maxloglevel=serverlevel;
65 if (pLogName && fileloglevel>=0)
66 logFile = fopen(pLogName, "a+");
67 bLogToSock = (servAddr && logPort && (serverlevel>=0));
69 initlogSocket(servAddr, logPort);
72 static void initlogSocket(char* servAddr, char* logPort)
78 // check and prepare WinSock DLL
79 WORD wVersionRequested = MAKEWORD( 2, 2 );
81 if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
86 // Confirm that the WinSock DLL supports 2.2.
87 // Note that if the DLL supports versions greater
88 // than 2.2 in addition to 2.2, it will still return
89 // 2.2 in wVersion since that is the version we
92 if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
99 hp = gethostbyname(servAddr);
100 if (hp == NULL) // we don't know who this host is
107 sockaddr_in SocketAddress;
109 memset(&SocketAddress, 0, sizeof(SocketAddress));
110 memcpy((char*)&SocketAddress.sin_addr, hp->h_addr, hp->h_length); // set address
111 SocketAddress.sin_family = hp->h_addrtype;
112 SocketAddress.sin_port = htons((u_short)atoi(logPort));
114 logSocket = socket(hp->h_addrtype, SOCK_STREAM, 0);
115 if (logSocket == INVALID_SOCKET)
121 // try to connect to the specified socket
122 if ( connect(logSocket, (struct sockaddr*)&SocketAddress, sizeof (SocketAddress)) == SOCKET_ERROR) {
132 // close socket communication
137 closesocket(logSocket);
145 static char * logimportance[]= {"Fatal","Error","Warning","Notice","Verbose","Debug"};
146 static int loglevels=6;
147 static char * logimportance2[]= {" ","FATAL ","ERROR ","WARNING","NOTICE ","VERBOSE","DEBUG "};
148 static inline void log(char* logString)
159 logBuffer = (char*)malloc (strlen(logString) + 24 + 15);
162 /*time_t t = time(0);
163 tm*t2 = localtime(t);
164 strftime(dbuffer, 8, "%m %d", t2);
165 strftime(tbuffer, 8, "%m %d", t2);
166 dbuffer[0]=0; //FIXME
171 while(a[l-1] == 13 || a[l-1] == 10)
174 sprintf(timebuffer, "%s", a);
179 sprintf(timebuffer, "%s - %s",dbuffer,tbuffer);
182 // search for <level> field
184 lt=strchr(logString, '<');
185 gt=strchr(logString, '>');
186 if(lt && gt && lt<gt)
189 for(t=0;t<loglevels;t++)
192 if(!strncasecmp(lt+1,logimportance[t],strlen(logimportance[t])))
194 if(!strnicmp(lt+1,logimportance[t],strlen(logimportance[t])))
198 while(logString[0]==' ') logString ++;
205 // sprintf(logBuffer, "%s: %s %s", timebuffer, logimportance2[level + 1],logString);
206 sprintf(logBuffer, "%s %s", logimportance2[level + 1],logString);
208 // we always do exactly one newline.
210 l=strlen(logBuffer)-1;
211 while((logBuffer[l]==13 || logBuffer[l]==10) && l>=0)
217 if (level <= screenloglevel)
219 printf("%s\n", logBuffer);
223 if (level <= fileloglevel)
227 fprintf(logFile, "%s\n", logBuffer);
232 if (level <= socketloglevel)
238 write(logSocket, logBuffer, strlen(logBuffer));
240 send(logSocket, logBuffer, strlen(logBuffer), 0);
247 void logf(const char* format, ...)
251 va_start(arglist, format);
256 char*x = strchr(z,format[0]);
257 if(x && (x-z)>maxloglevel)
261 vsprintf(buf, format, arglist);