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