]> git.vomp.tv Git - jsonserver.git/blob - log.c
Make SSL cert filename part of config. Fix a shutdown segfault
[jsonserver.git] / log.c
1 /*
2     Copyright 2004-2013 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 <time.h>
23 #include <sys/time.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "log.h"
28
29 Log* Log::instance = NULL;
30
31 Log::Log()
32 {
33   if (instance) return;
34   instance = this;
35   logfile = NULL;
36   initted = 0;
37   logLevel = 0;
38 }
39
40 Log::~Log()
41 {
42   instance = NULL;
43 }
44
45 Log* Log::getInstance()
46 {
47   return instance;
48 }
49
50 void Log::upLogLevel()
51 {
52   if (!initted) return;
53
54   if (logLevel == Log::DEBUG)
55   {
56     log("Log", logLevel, "Log level is at its highest already");
57     return;
58   }
59
60   logLevel++;
61   log("Log", logLevel, "Log level is now %i", logLevel);
62 }
63
64 void Log::downLogLevel()
65 {
66   if (!initted) return;
67
68   if (logLevel == Log::CRAZY)
69   {
70     log("Log", logLevel, "Log level is at its lowest already");
71     return;
72   }
73
74   logLevel--;
75   log("Log", logLevel, "Log level is now %i", logLevel);
76 }
77
78 int Log::init(int startLogLevel, const char* fileName)
79 {
80   logLevel = startLogLevel;
81
82   logfile = fopen(fileName, "a");
83   if (logfile)
84   {
85     initted = 1;
86     return 1;
87   }
88   else
89   {
90     return 0;
91   }
92 }
93
94 int Log::shutdown()
95 {
96   if (!initted) return 1;
97   initted = 0;
98   if (logfile) fclose(logfile);
99   return 1;
100 }
101
102 int Log::log(const char *fromModule, int level, const char* message, ...)
103 {
104   if (!initted) return 0;
105
106   if (level > logLevel) return 1;
107
108   int lineLength = 250;
109
110   char buffer[lineLength + 1];
111   int spaceLeft = lineLength;
112
113   struct timeval tv;
114   gettimeofday(&tv, NULL);
115   struct tm* tm = localtime(&tv.tv_sec);
116   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm);
117   spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
118
119
120   char levelString[10];
121   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
122   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
123   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
124   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
125   if (level == ERR)     strcpy(levelString, "[ERR]   ");
126   if (level == WARN)    strcpy(levelString, "[WARN]  ");
127   if (level == NOTICE)  strcpy(levelString, "[notice]");
128   if (level == INFO)    strcpy(levelString, "[info]  ");
129   if (level == DEBUG)   strcpy(levelString, "[debug] ");
130
131   spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
132
133   va_list ap;
134   va_start(ap, message);
135   spaceLeft = vsnprintf(&buffer[lineLength-spaceLeft], spaceLeft, message, ap);
136   va_end(ap);
137
138   int messageLength = strlen(buffer);
139   if (messageLength < lineLength)
140   {
141     buffer[messageLength] = '\n';
142     buffer[messageLength+1] = '\0';
143   }
144   else
145   {
146     buffer[lineLength-1] = '\n';
147     buffer[lineLength] = '\0';
148   }
149
150   int success = fputs(buffer, logfile);
151   fflush(NULL);
152
153   if (success != EOF)
154     return 1;
155   else
156     return 0;
157
158 }