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