]> git.vomp.tv Git - vompserver.git/blob - log.c
15 years that line of code has been waiting to crash
[vompserver.git] / log.c
1 /*
2     Copyright 2004-2005 Chris Tallon
3     Copyright 2003-2004 University Of Bradford
4
5     This file is part of VOMP.
6
7     VOMP is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     VOMP is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with VOMP; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 */
21
22 #include "log.h"
23
24 Log* Log::instance = NULL;
25
26 Log::Log()
27 {
28   if (instance) return;
29   instance = this;
30   logfile = NULL;
31   initted = 0;
32   logLevel = 0;
33 }
34
35 Log::~Log()
36 {
37   instance = NULL;
38 }
39
40 Log* Log::getInstance()
41 {
42   return instance;
43 }
44
45 void Log::upLogLevel()
46 {
47   if (!initted) return;
48
49   if (logLevel == Log::DEBUG)
50   {
51     log("Log", logLevel, "Log level is at its highest already");
52     return;
53   }
54
55   logLevel++;
56   log("Log", logLevel, "Log level is now %i", logLevel);
57 }
58
59 void Log::downLogLevel()
60 {
61   if (!initted) return;
62
63   if (logLevel == Log::CRAZY)
64   {
65     log("Log", logLevel, "Log level is at its lowest already");
66     return;
67   }
68
69   logLevel--;
70   log("Log", logLevel, "Log level is now %i", logLevel);
71 }
72
73 int Log::init(int startLogLevel, char* fileName)
74 {
75   logLevel = startLogLevel;
76
77   logfile = fopen(fileName, "a");
78   if (logfile)
79   {
80     initted = 1;
81     return 1;
82   }
83   else
84   {
85     return 0;
86   }
87 }
88
89 int Log::shutdown()
90 {
91   if (!initted) return 1;
92   initted = 0;
93   if (logfile) fclose(logfile);
94   return 1;
95 }
96
97 int Log::log(const char *fromModule, int level, const char* message, ...)
98 {
99   if (!initted) return 0;
100
101   if (level > logLevel) return 1;
102
103   int lineLength = 250;
104
105   char buffer[lineLength + 1];
106   int spaceLeft = lineLength;
107
108   struct timeval tv;
109   gettimeofday(&tv, NULL);
110   struct tm* tm = localtime(&tv.tv_sec);
111   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm);
112   spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
113
114
115   char levelString[10];
116   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
117   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
118   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
119   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
120   if (level == ERR)     strcpy(levelString, "[ERR]   ");
121   if (level == WARN)    strcpy(levelString, "[WARN]  ");
122   if (level == NOTICE)  strcpy(levelString, "[notice]");
123   if (level == INFO)    strcpy(levelString, "[info]  ");
124   if (level == DEBUG)   strcpy(levelString, "[debug] ");
125
126   spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
127
128   va_list ap;
129   va_start(ap, message);
130   spaceLeft = vsnprintf(&buffer[lineLength-spaceLeft], spaceLeft, message, ap);
131   va_end(ap);
132
133   int messageLength = strlen(buffer);
134   if (messageLength < lineLength)
135   {
136     buffer[messageLength] = '\n';
137     buffer[messageLength+1] = '\0';
138   }
139   else
140   {
141     buffer[lineLength-1] = '\n';
142     buffer[lineLength] = '\0';
143   }
144
145   int success = fputs(buffer, logfile);
146   fflush(NULL);
147
148   if (success != EOF)
149     return 1;
150   else
151     return 0;
152
153 }