]> git.vomp.tv Git - vompclient.git/blob - log.cc
Bug fix 2 for duplicating epg rows
[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
108   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
109   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
110
111
112   char levelString[10];
113   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
114   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
115   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
116   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
117   if (level == ERR)     strcpy(levelString, "[ERR]   ");
118   if (level == WARN)    strcpy(levelString, "[WARN]  ");
119   if (level == NOTICE)  strcpy(levelString, "[notice]");
120   if (level == INFO)    strcpy(levelString, "[info]  ");
121   if (level == DEBUG)   strcpy(levelString, "[debug] ");
122
123   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
124
125   va_list ap;
126   va_start(ap, message);
127   spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
128   va_end(ap);
129
130   int messageLength = strlen(buffer);
131   if (messageLength < 150)
132   {
133     buffer[messageLength] = '\n';
134     buffer[messageLength+1] = '\0';
135   }
136   else
137   {
138     buffer[149] = '\n';
139     buffer[150] = '\0';
140   }
141
142   int success = fputs(buffer, logfile);
143   fflush(NULL);
144
145   if (success != EOF)
146     return 1;
147   else
148     return 0;
149
150 }
151
152 int Log::status()
153 {
154   if (instance && logfile) return 1;
155   else return 0;
156 }