more fiddling with edgestyles
[swftools.git] / lib / log.c
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.
4
5    Part of the swftools package.
6    
7    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
8
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.
13
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.
18
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 */
22
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
36
37 #include "log.h"
38
39 static int screenloglevel = 1;
40 static int fileloglevel = -1;
41 static FILE *logFile = 0;
42 static int maxloglevel = 1;
43
44 int getScreenLogLevel()
45 {
46     return screenloglevel;
47 }
48 int getLogLevel()
49 {
50     return maxloglevel;
51 }
52
53 void setConsoleLogging(int level)
54 {
55     if(level>maxloglevel)
56         maxloglevel=level;
57     screenloglevel = level;
58 }
59 void setFileLogging(char*filename, int level, char append)
60 {
61     if(level>maxloglevel)
62         maxloglevel=level;
63     if(logFile) {
64         fclose(logFile);logFile=0;
65     }
66     if(filename && level>=0) {
67         logFile = fopen(filename, append?"ab+":"wb");
68         fileloglevel = level;
69     } else {
70         logFile = 0;
71         fileloglevel = 0;
72     }
73 }
74 /* deprecated */
75 void initLog(char* filename, int filelevel, char* s00, char* s01, int s02, int screenlevel)
76 {
77     setFileLogging(filename, filelevel, 0);
78     setConsoleLogging(screenlevel);
79 }
80
81 void exitLog()
82 {
83    // close file
84    if(logFile != NULL) {
85      fclose(logFile);
86      logFile = 0;
87      fileloglevel = -1;
88      screenloglevel = 1;
89      maxloglevel = 1;
90    }
91 }
92
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  "};
96
97 static inline void log_str(const char* logString)
98 {
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;
107
108    logBuffer = (char*)malloc (strlen(logString) + 24 + 15);
109 #ifndef __NT__
110    {
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);
124    }
125 #else
126    _strdate( dbuffer );
127    _strtime( tbuffer );
128    sprintf(timebuffer, "%s - %s",dbuffer,tbuffer);
129 #endif
130
131    // search for <level> field
132    level = -1;
133    lt=strchr(logString, '<');
134    gt=strchr(logString, '>');
135    if(lt && gt && lt<gt)
136    {
137        int t;
138        for(t=0;t<loglevels;t++)
139        {
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
145            {
146                logString = gt+1;
147                while(logString[0]==' ') logString ++;
148                level = t;
149                break;
150            }
151        }
152    }
153    
154 //   sprintf(logBuffer, "%s: %s %s", timebuffer, logimportance2[level + 1],logString);
155    sprintf(logBuffer, "%s %s", logimportance2[level + 1],logString);
156
157    // we always do exactly one newline.
158    
159    l=strlen(logBuffer)-1;
160    while((logBuffer[l]==13 || logBuffer[l]==10) && l>=0)
161    {
162        logBuffer[l]=0;
163        l--;
164    }
165
166    if (level <= screenloglevel)
167    {
168        printf("%s\n", logBuffer); 
169        fflush(stdout);
170    }
171
172    if (level <= fileloglevel)
173    {
174        if (logFile != NULL)
175        {
176           fprintf(logFile, "%s\r\n", logBuffer); 
177           fflush(logFile);
178        }
179    }
180
181    free (logBuffer);
182 }
183
184 void msg_str(const char* buf)
185 {
186     if(buf[0]=='<') {
187         char*z = "fewnvdt";
188         char*x = strchr(z,buf[1]);
189         if(x && (x-z)>maxloglevel)
190                 return;
191     }
192     log_str(buf);
193 }
194 void msg(const char* format, ...)
195 {
196     char buf[1024];
197         va_list arglist;
198         va_start(arglist, format);
199     
200     /* speed up hack */
201     if(format[0]=='<') {
202         char*z = "fewnvdt";
203         char*x = strchr(z,format[1]);
204         if(x && (x-z)>maxloglevel)
205                 return;
206     }
207
208     vsnprintf(buf, sizeof(buf)-1, format, arglist);
209         va_end(arglist);
210     strcat(buf, "\n");
211     log_str(buf);
212 }
213