]> git.vomp.tv Git - vompclient.git/blob - log.cc
Remove manual sync buttons
[vompclient.git] / log.cc
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 (logLevel == Log::DEBUG)
48   {
49     log("Log", logLevel, "Log level is at its highest already");
50     return;
51   }
52
53   logLevel++;
54   log("Log", logLevel, "Log level is now %i", logLevel);
55 }
56
57 void Log::downLogLevel()
58 {
59   if (logLevel == Log::CRAZY)
60   {
61     log("Log", logLevel, "Log level is at its lowest already");
62     return;
63   }
64
65   logLevel--;
66   log("Log", logLevel, "Log level is now %i", logLevel);
67 }
68
69 int Log::init(int startLogLevel, char* fileName, int tenabled)
70 {
71   initted = 1;
72   logLevel = startLogLevel;
73   enabled = tenabled;
74 //  logfile = fopen(fileName, "a");
75 //  logfile = fopen(stdout, "a");
76   logfile = stdout;
77   if (logfile) return 1;
78   else return 0;
79 }
80
81 int Log::shutdown()
82 {
83   if (!initted) return 1;
84   if (logfile) fclose(logfile);
85   return 1;
86 }
87
88 int Log::log(char *fromModule, int level, char* message, ...)
89 {
90   if (!instance || !logfile) return 0;
91
92   if (!enabled) return 1;
93   if (level > logLevel) return 1;
94
95   char buffer[151];
96   int spaceLeft = 150;
97
98 #ifndef _MSC_VER
99   struct timeval tv;
100   gettimeofday(&tv, NULL);
101   struct tm* tms = localtime(&tv.tv_sec);
102 #else
103   struct _timeb tb;
104   _ftime(&tb);
105   struct tm* tms = localtime(&tb.time);
106 #endif
107   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
108 #ifndef _MSC_VER
109   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
110 #else
111   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
112 #endif
113
114   char levelString[10];
115   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
116   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
117   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
118   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
119   if (level == ERR)     strcpy(levelString, "[ERR]   ");
120   if (level == WARN)    strcpy(levelString, "[WARN]  ");
121   if (level == NOTICE)  strcpy(levelString, "[notice]");
122   if (level == INFO)    strcpy(levelString, "[info]  ");
123   if (level == DEBUG)   strcpy(levelString, "[debug] ");
124
125   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
126
127   va_list ap;
128   va_start(ap, message);
129   spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
130   va_end(ap);
131
132   int messageLength = strlen(buffer);
133   if (messageLength < 150)
134   {
135     buffer[messageLength] = '\n';
136     buffer[messageLength+1] = '\0';
137   }
138   else
139   {
140     buffer[149] = '\n';
141     buffer[150] = '\0';
142   }
143
144   int success = fputs(buffer, logfile);
145   fflush(NULL);
146
147   if (success != EOF)
148     return 1;
149   else
150     return 0;
151
152 }
153
154 int Log::status()
155 {
156   if (instance && logfile) return 1;
157   else return 0;
158 }