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 #define LOGLEVEL_FATAL 0
31 #define LOGLEVEL_ERROR 1
32 #define LOGLEVEL_WARNING 2
33 #define LOGLEVEL_NOTICE 3
34 #define LOGLEVEL_VERBOSE 4
35 #define LOGLEVEL_DEBUG 5
49 void initlogSocket(char* servAddr, char* logPort);
51 void initLog(char* pLogName, int fileloglevel, char* servAddr, char* logPort, int serverlevel, int screenlevel)
53 screenloglevel = screenlevel;
54 fileloglevel = fileloglevel;
55 socketloglevel = screenlevel;
59 if (pLogName && fileloglevel>=0)
60 logFile = fopen(pLogName, "a+");
61 bLogToSock = (servAddr && logPort && (serverlevel>=0));
63 initlogSocket(servAddr, logPort);
66 void initlogSocket(char* servAddr, char* logPort)
72 // check and prepare WinSock DLL
73 WORD wVersionRequested = MAKEWORD( 2, 2 );
75 if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
80 // Confirm that the WinSock DLL supports 2.2.
81 // Note that if the DLL supports versions greater
82 // than 2.2 in addition to 2.2, it will still return
83 // 2.2 in wVersion since that is the version we
86 if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
93 hp = gethostbyname(servAddr);
94 if (hp == NULL) // we don't know who this host is
101 sockaddr_in SocketAddress;
103 memset(&SocketAddress, 0, sizeof(SocketAddress));
104 memcpy((char*)&SocketAddress.sin_addr, hp->h_addr, hp->h_length); // set address
105 SocketAddress.sin_family = hp->h_addrtype;
106 SocketAddress.sin_port = htons((u_short)atoi(logPort));
108 logSocket = socket(hp->h_addrtype, SOCK_STREAM, 0);
109 if (logSocket == INVALID_SOCKET)
115 // try to connect to the specified socket
116 if ( connect(logSocket, (struct sockaddr*)&SocketAddress, sizeof (SocketAddress)) == SOCKET_ERROR) {
126 // close socket communication
131 closesocket(logSocket);
139 static char * logimportance[]= {"Fatal","Error","Warning","Notice","Verbose","Debug"};
140 static int loglevels=6;
141 static char * logimportance2[]= {" ","FATAL ","ERROR ","WARNING","NOTICE ","VERBOSE","DEBUG "};
142 void log(char* logString)
153 logBuffer = (char*)malloc (strlen(logString) + 24 + 15);
156 /*time_t t = time(0);
157 tm*t2 = localtime(t);
158 strftime(dbuffer, 8, "%m %d", t2);
159 strftime(tbuffer, 8, "%m %d", t2);
160 dbuffer[0]=0; //FIXME
165 while(a[l-1] == 13 || a[l-1] == 10)
168 sprintf(timebuffer, "%s", a);
173 sprintf(timebuffer, "%s - %s",dbuffer,tbuffer);
176 // search for <level> field
178 lt=strchr(logString, '<');
179 gt=strchr(logString, '>');
180 if(lt && gt && lt<gt)
183 for(t=0;t<loglevels;t++)
186 if(!strncasecmp(lt+1,logimportance[t],strlen(logimportance[t])))
188 if(!strnicmp(lt+1,logimportance[t],strlen(logimportance[t])))
192 while(logString[0]==' ') logString ++;
199 // sprintf(logBuffer, "%s: %s %s", timebuffer, logimportance2[level + 1],logString);
200 sprintf(logBuffer, "%s %s", logimportance2[level + 1],logString);
202 // we always do exactly one newline.
204 l=strlen(logBuffer)-1;
205 while((logBuffer[l]==13 || logBuffer[l]==10) && l>=0)
211 if (level <= screenloglevel)
213 printf("%s\n", logBuffer);
217 if (level <= fileloglevel)
221 fprintf(logFile, "%s\n", logBuffer);
226 if (level <= socketloglevel)
232 write(logSocket, logBuffer, strlen(logBuffer));
234 send(logSocket, logBuffer, strlen(logBuffer), 0);
241 void logf(const char* pszFormat, ...)
245 va_start(arglist, pszFormat);
247 vsprintf(&buf[strlen(buf)], pszFormat, arglist);