several cleanups.
[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 #ifdef __NT__
24 #include "stdafx.h"
25 #include <string.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #if _MSC_VER > 1000
29 #pragma once
30 #endif // _MSC_VER > 1000
31 #else
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37 #endif
38
39 #include "log.h"
40
41 static int screenloglevel = 1;
42 static int fileloglevel = -1;
43 static FILE *logFile = 0;
44 static int maxloglevel = 1;
45
46 int getScreenLogLevel()
47 {
48     return screenloglevel;
49 }
50
51 void setConsoleLogging(int level)
52 {
53     if(level>maxloglevel)
54         maxloglevel=level;
55     screenloglevel = level;
56 }
57 void setFileLogging(char*filename, int level, char append)
58 {
59     if(level>maxloglevel)
60         maxloglevel=level;
61     if(logFile) {
62         fclose(logFile);logFile=0;
63     }
64     if(filename && level>=0) {
65         logFile = fopen(filename, append?"ab+":"wb");
66         fileloglevel = level;
67     } else {
68         logFile = 0;
69         fileloglevel = 0;
70     }
71 }
72 /* deprecated */
73 void initLog(char* filename, int fileloglevel, char* s00, char* s01, int s02, int screenlevel)
74 {
75     setFileLogging(filename, fileloglevel, 0);
76     setConsoleLogging(screenloglevel);
77 }
78
79 void exitLog()
80 {
81    // close file
82    if(logFile != NULL) {
83      fclose(logFile);
84      logFile = 0;
85      fileloglevel = -1;
86      screenloglevel = 1;
87      maxloglevel = 1;
88    }
89 }
90
91 static char * logimportance[]= {"Fatal","Error","Warning","Notice","Verbose","Debug","Trace"};
92 static int loglevels=7;
93 static char * logimportance2[]= {"       ","FATAL  ","ERROR  ","WARNING","NOTICE ","VERBOSE","DEBUG  ", "TRACE  "};
94
95 static inline void log(const char* logString)
96 {
97    char timebuffer[32];
98    char* logBuffer;
99    char dbuffer[9];
100    char tbuffer[9];
101    int level;
102    char*lt;
103    char*gt;
104    int l;
105
106    logBuffer = (char*)malloc (strlen(logString) + 24 + 15);
107 #ifndef __NT__
108    {
109      /*time_t t = time(0);
110      tm*t2 = localtime(t);
111      strftime(dbuffer, 8, "%m %d", t2);
112      strftime(tbuffer, 8, "%m %d", t2);
113      dbuffer[0]=0; //FIXME
114      tbuffer[0]=0;*/
115      time_t t = time(0);
116      char* a = ctime(&t);
117      int l = strlen(a);
118      while(a[l-1] == 13 || a[l-1] == 10)
119        l--;
120      a[l]=0;
121      sprintf(timebuffer, "%s", a);
122    }
123 #else
124    _strdate( dbuffer );
125    _strtime( tbuffer );
126    sprintf(timebuffer, "%s - %s",dbuffer,tbuffer);
127 #endif
128
129    // search for <level> field
130    level = -1;
131    lt=strchr(logString, '<');
132    gt=strchr(logString, '>');
133    if(lt && gt && lt<gt)
134    {
135        int t;
136        for(t=0;t<loglevels;t++)
137        {
138 #ifndef __NT__
139            if(!strncasecmp(lt+1,logimportance[t],strlen(logimportance[t])))
140 #else
141            if(!strnicmp(lt+1,logimportance[t],strlen(logimportance[t])))
142 #endif
143            {
144                logString = gt+1;
145                while(logString[0]==' ') logString ++;
146                level = t;
147                break;
148            }
149        }
150    }
151    
152 //   sprintf(logBuffer, "%s: %s %s", timebuffer, logimportance2[level + 1],logString);
153    sprintf(logBuffer, "%s %s", logimportance2[level + 1],logString);
154
155    // we always do exactly one newline.
156    
157    l=strlen(logBuffer)-1;
158    while((logBuffer[l]==13 || logBuffer[l]==10) && l>=0)
159    {
160        logBuffer[l]=0;
161        l--;
162    }
163
164    if (level <= screenloglevel)
165    {
166        printf("%s\n", logBuffer); 
167        fflush(stdout);
168    }
169
170    if (level <= fileloglevel)
171    {
172        if (logFile != NULL)
173        {
174           fprintf(logFile, "%s\n", logBuffer); 
175           fflush(logFile);
176        }
177    }
178
179    free (logBuffer);
180 }
181
182 void msg_str(const char* buf)
183 {
184     if(buf[0]=='<') {
185         char*z = "fewnvdt";
186         char*x = strchr(z,buf[1]);
187         if(x && (x-z)>maxloglevel)
188                 return;
189     }
190     log(buf);
191 }
192 void msg(const char* format, ...)
193 {
194     char buf[1024];
195         va_list arglist;
196         va_start(arglist, format);
197     
198     /* speed up hack */
199     if(format[0]=='<') {
200         char*z = "fewnvdt";
201         char*x = strchr(z,format[1]);
202         if(x && (x-z)>maxloglevel)
203                 return;
204     }
205
206     vsprintf(buf, format, arglist);
207         va_end(arglist);
208     strcat(buf, "\n");
209     log(buf);
210 }
211