]> git.vomp.tv Git - vompclient.git/blob - log.cc
Rewritten vomp discovery protocol
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 */
21
22 #include "log.h"
23
24 #include "vdr.h"
25
26 Log* Log::instance = NULL;
27
28 Log::Log()
29 {
30   if (instance) return;
31   instance = this;
32   logfile = NULL;
33   initted = 0;
34   logLevel = 0;
35   netLog = false;
36 }
37
38 Log::~Log()
39 {
40   instance = NULL;
41 }
42
43 Log* Log::getInstance()
44 {
45   return instance;
46 }
47
48 void Log::upLogLevel()
49 {
50   if (logLevel == Log::DEBUG)
51   {
52     log("Log", logLevel, "Log level is at its highest already");
53     return;
54   }
55
56   logLevel++;
57   log("Log", logLevel, "Log level is now %i", logLevel);
58 }
59
60 void Log::downLogLevel()
61 {
62   if (logLevel == Log::CRAZY)
63   {
64     log("Log", logLevel, "Log level is at its lowest already");
65     return;
66   }
67
68   logLevel--;
69   log("Log", logLevel, "Log level is now %i", logLevel);
70 }
71
72 int Log::init(int startLogLevel, char* fileName, int tenabled)
73 {
74   initted = 1;
75   logLevel = startLogLevel;
76   enabled = tenabled;
77 //  logfile = fopen(fileName, "a");
78 //  logfile = fopen(stdout, "a");
79   logfile = stdout;
80 //  logfile = fopen("/log", "a");
81
82   if (logfile) return 1;
83   else return 0;
84 }
85
86 int Log::shutdown()
87 {
88   if (!initted) return 1;
89   if (enabled) fclose(logfile);
90   return 1;
91 }
92
93 int Log::log(char *fromModule, int level, char* message, ...)
94 {
95   if (!instance || !logfile) return 0;
96
97   if (!enabled && !netLog) return 1;
98   if (level > logLevel) return 1;
99
100   char buffer[151];
101   int spaceLeft = 150;
102
103 #ifndef _MSC_VER
104   struct timeval tv;
105   gettimeofday(&tv, NULL);
106   struct tm* tms = localtime(&tv.tv_sec);
107 #else
108   struct _timeb tb;
109   _ftime(&tb);
110   struct tm* tms = localtime(&tb.time);
111 #endif
112   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
113 #ifndef _MSC_VER
114   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
115 #else
116   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
117 #endif
118
119   char levelString[10];
120   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
121   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
122   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
123   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
124   if (level == ERR)     strcpy(levelString, "[ERR]   ");
125   if (level == WARN)    strcpy(levelString, "[WARN]  ");
126   if (level == NOTICE)  strcpy(levelString, "[notice]");
127   if (level == INFO)    strcpy(levelString, "[info]  ");
128   if (level == DEBUG)   strcpy(levelString, "[debug] ");
129
130 #ifndef WIN32
131   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %d %s - ", levelString, getpid(), fromModule);
132 #else
133   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString,  fromModule);
134 #endif
135
136   va_list ap;
137   va_start(ap, message);
138   spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
139   va_end(ap);
140
141   int messageLength = strlen(buffer);
142   if (messageLength < 150)
143   {
144     buffer[messageLength] = '\n';
145     buffer[messageLength+1] = '\0';
146   }
147   else
148   {
149     buffer[149] = '\n';
150     buffer[150] = '\0';
151   }
152   
153   int success = 1;
154   if (enabled)
155   {
156     success = fputs(buffer, logfile);
157     fflush(NULL);
158   }
159
160   if (netLog) vdr->networkLog(buffer);
161
162   if (success != EOF)
163     return 1;
164   else
165     return 0;
166
167 }
168
169 int Log::status()
170 {
171   if (instance && logfile) return 1;
172   else return 0;
173 }
174
175 void Log::netLogOn()
176 {
177   vdr = VDR::getInstance();
178   netLog = true;
179   log("Log", Log::DEBUG, "Network logging started");
180 }
181
182 void Log::netLogOff()
183 {
184   netLog = false;
185   vdr = NULL;
186 }
187
188