]> git.vomp.tv Git - vompserver.git/blob - log.c
FSF address change
[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   char buffer[151];
104   int spaceLeft = 150;
105
106   struct timeval tv;
107   gettimeofday(&tv, NULL);
108   struct tm* tm = localtime(&tv.tv_sec);
109   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm);
110   spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
111
112
113   char levelString[10];
114   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
115   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
116   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
117   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
118   if (level == ERR)     strcpy(levelString, "[ERR]   ");
119   if (level == WARN)    strcpy(levelString, "[WARN]  ");
120   if (level == NOTICE)  strcpy(levelString, "[notice]");
121   if (level == INFO)    strcpy(levelString, "[info]  ");
122   if (level == DEBUG)   strcpy(levelString, "[debug] ");
123
124   spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
125
126   va_list ap;
127   va_start(ap, message);
128   spaceLeft = vsnprintf(&buffer[150-spaceLeft], spaceLeft, message, ap);
129   va_end(ap);
130
131   int messageLength = strlen(buffer);
132   if (messageLength < 150)
133   {
134     buffer[messageLength] = '\n';
135     buffer[messageLength+1] = '\0';
136   }
137   else
138   {
139     buffer[149] = '\n';
140     buffer[150] = '\0';
141   }
142
143   int success = fputs(buffer, logfile);
144   fflush(NULL);
145
146   if (success != EOF)
147     return 1;
148   else
149     return 0;
150
151 }