removed duplicate md5
[swftools.git] / lib / log.c
blobe4b7aa909d7b414b667b589af42e15a7c73bca4d
1 /* log.c
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 */
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #ifdef WIN32
27 //#include "stdafx.h"
28 #include <malloc.h>
29 #if _MSC_VER > 1000
30 #pragma once
31 #endif // _MSC_VER > 1000
32 #else
33 #include <stdio.h>
34 #include <unistd.h>
35 #endif
37 #include "log.h"
39 int maxloglevel = 1;
40 static int screenloglevel = 1;
41 static int fileloglevel = -1;
42 static FILE *logFile = 0;
44 int getScreenLogLevel()
46 return screenloglevel;
48 int getLogLevel()
50 return maxloglevel;
53 void setConsoleLogging(int level)
55 if(level>maxloglevel)
56 maxloglevel=level;
57 screenloglevel = level;
59 void setFileLogging(char*filename, int level, char append)
61 if(level>maxloglevel)
62 maxloglevel=level;
63 if(logFile) {
64 fclose(logFile);logFile=0;
66 if(filename && level>=0) {
67 logFile = fopen(filename, append?"ab+":"wb");
68 fileloglevel = level;
69 } else {
70 logFile = 0;
71 fileloglevel = 0;
74 /* deprecated */
75 void initLog(char* filename, int filelevel, char* s00, char* s01, int s02, int screenlevel)
77 setFileLogging(filename, filelevel, 0);
78 setConsoleLogging(screenlevel);
81 void exitLog()
83 // close file
84 if(logFile != NULL) {
85 fclose(logFile);
86 logFile = 0;
87 fileloglevel = -1;
88 screenloglevel = 1;
89 maxloglevel = 1;
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)
99 char timebuffer[32];
100 char* logBuffer;
101 char dbuffer[9];
102 char tbuffer[9];
103 int level;
104 char*lt;
105 char*gt;
106 int l;
108 logBuffer = (char*)malloc (strlen(logString) + 24 + 15);
109 #ifndef __NT__
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
116 tbuffer[0]=0;*/
117 time_t t = time(0);
118 char* a = ctime(&t);
119 int l = strlen(a);
120 while(a[l-1] == 13 || a[l-1] == 10)
121 l--;
122 a[l]=0;
123 sprintf(timebuffer, "%s", a);
125 #else
126 _strdate( dbuffer );
127 _strtime( tbuffer );
128 sprintf(timebuffer, "%s - %s",dbuffer,tbuffer);
129 #endif
131 // search for <level> field
132 level = -1;
133 lt=strchr(logString, '<');
134 gt=strchr(logString, '>');
135 if(lt && gt && lt<gt)
137 int t;
138 for(t=0;t<loglevels;t++)
140 #ifndef __NT__
141 if(!strncasecmp(lt+1,logimportance[t],strlen(logimportance[t])))
142 #else
143 if(!strnicmp(lt+1,logimportance[t],strlen(logimportance[t])))
144 #endif
146 logString = gt+1;
147 while(logString[0]==' ') logString ++;
148 level = t;
149 break;
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)
162 logBuffer[l]=0;
163 l--;
166 if (level <= screenloglevel)
168 printf("%s\n", logBuffer);
169 fflush(stdout);
172 if (level <= fileloglevel)
174 if (logFile != NULL)
176 fprintf(logFile, "%s\r\n", logBuffer);
177 fflush(logFile);
181 free (logBuffer);
184 void msg_str(const char* buf)
186 if(buf[0]=='<') {
187 char*z = "fewnvdt";
188 char*x = strchr(z,buf[1]);
189 if(x && (x-z)>maxloglevel)
190 return;
192 log_str(buf);
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, ...)
203 char buf[1024];
204 va_list arglist;
205 va_start(arglist, format);
207 /* speed up hack */
208 if(format[0]=='<') {
209 char*z = "fewnvdt";
210 char*x = strchr(z,format[1]);
211 if(x && (x-z)>maxloglevel)
212 return 0;
215 vsnprintf(buf, sizeof(buf)-1, format, arglist);
216 va_end(arglist);
217 strcat(buf, "\n");
218 log_str(buf);
219 return 0;