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
40 static int screenloglevel
= 1;
41 static int fileloglevel
= -1;
42 static FILE *logFile
= 0;
44 int getScreenLogLevel()
46 return screenloglevel
;
53 void setConsoleLogging(int level
)
57 screenloglevel
= level
;
59 void setFileLogging(char*filename
, int level
, char append
)
64 fclose(logFile
);logFile
=0;
66 if(filename
&& level
>=0) {
67 logFile
= fopen(filename
, append
?"ab+":"wb");
75 void initLog(char* filename
, int filelevel
, char* s00
, char* s01
, int s02
, int screenlevel
)
77 setFileLogging(filename
, filelevel
, 0);
78 setConsoleLogging(screenlevel
);
93 static char * logimportance
[]= {"Fatal","Error","Warning","Notice","Verbose","Debug","Trace"};
94 static int loglevels
=7;
95 static char * logimportance2
[]= {" ","FATAL ","ERROR ","WARNING","NOTICE ","VERBOSE","DEBUG ", "TRACE "};
97 static inline void log_str(const char* logString
)
108 logBuffer
= (char*)malloc (strlen(logString
) + 24 + 15);
111 /*time_t t = time(0);
112 tm*t2 = localtime(t);
113 strftime(dbuffer, 8, "%m %d", t2);
114 strftime(tbuffer, 8, "%m %d", t2);
115 dbuffer[0]=0; //FIXME
120 while(a
[l
-1] == 13 || a
[l
-1] == 10)
123 sprintf(timebuffer
, "%s", a
);
128 sprintf(timebuffer
, "%s - %s",dbuffer
,tbuffer
);
131 // search for <level> field
133 lt
=strchr(logString
, '<');
134 gt
=strchr(logString
, '>');
135 if(lt
&& gt
&& lt
<gt
)
138 for(t
=0;t
<loglevels
;t
++)
141 if(!strncasecmp(lt
+1,logimportance
[t
],strlen(logimportance
[t
])))
143 if(!strnicmp(lt
+1,logimportance
[t
],strlen(logimportance
[t
])))
147 while(logString
[0]==' ') logString
++;
154 // sprintf(logBuffer, "%s: %s %s", timebuffer, logimportance2[level + 1],logString);
155 sprintf(logBuffer
, "%s %s", logimportance2
[level
+ 1],logString
);
157 // we always do exactly one newline.
159 l
=strlen(logBuffer
)-1;
160 while((logBuffer
[l
]==13 || logBuffer
[l
]==10) && l
>=0)
166 if (level
<= screenloglevel
)
168 printf("%s\n", logBuffer
);
172 if (level
<= fileloglevel
)
176 fprintf(logFile
, "%s\r\n", logBuffer
);
184 void msg_str(const char* buf
)
188 char*x
= strchr(z
,buf
[1]);
189 if(x
&& (x
-z
)>maxloglevel
)
195 char char2loglevel
[32] =
196 /* a b c d e f g h i j k l m n o */
197 {-1,-1,-1,-1, /*debug*/5, /*error*/1, /*fatal*/0, -1, -1, -1,-1,-1,-1,-1,/*notice*/3,-1,
198 /* p q r s t u v w x y z */
199 -1,-1,-1,-1, -1, /*trace*/6, -1,/*verbose*/4,/*warning*/2, -1,-1,-1,-1,-1, -1,-1};
201 int msg_internal(const char* format
, ...)
205 va_start(arglist
, format
);
210 char*x
= strchr(z
,format
[1]);
211 if(x
&& (x
-z
)>maxloglevel
)
215 vsnprintf(buf
, sizeof(buf
)-1, format
, arglist
);