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