]> git.vomp.tv Git - vompclient.git/blob - log.cc
Rewrite timers class using std::thread/mutex/cond/chrono
[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 #ifdef __ANDROID__
27 #include <android/log.h>
28 #endif
29
30 Log* Log::instance = NULL;
31
32 Log::Log()
33 {
34   if (instance) return;
35   instance = this;
36   logfile = NULL;
37   initted = 0;
38   logLevel = 0;
39   extlog = NULL;
40 }
41
42 Log::~Log()
43 {
44   instance = NULL;
45 }
46
47 Log* Log::getInstance()
48 {
49   return instance;
50 }
51
52 void Log::upLogLevel()
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 (logLevel == Log::CRAZY)
67   {
68     log("Log", logLevel, "Log level is at its lowest already");
69     return;
70   }
71
72   logLevel--;
73   log("Log", logLevel, "Log level is now %i", logLevel);
74 }
75
76 int Log::init(int startLogLevel, const char* fileName, int tenabled)
77 {
78   (void)fileName;
79
80   initted = 1;
81   logLevel = startLogLevel;
82   enabled = tenabled;
83   //logfile = fopen(fileName, "a");
84 //  logfile = fopen(stdout, "a");
85   logfile = stdout;
86 //  logfile = fopen("/log", "a");
87
88   if (logfile) return 1;
89   else return 0;
90 }
91
92 int Log::shutdown()
93 {
94   if (!initted) return 1;
95   if (enabled && (logfile != stdout)) fclose(logfile);
96   return 1;
97 }
98
99 int Log::log(const char *fromModule, int level,const char* message, ...)
100 {
101   if (!instance || !logfile) return 0;
102
103   if (!enabled && !extlog) return 1;
104   if (level > logLevel) return 1;
105
106   char buffer[151];
107   int spaceLeft = 150;
108
109 #ifndef _MSC_VER
110   struct timeval tv;
111   gettimeofday(&tv, NULL);
112   struct tm tms;
113   LOCALTIME_R(&tv.tv_sec, &tms);
114   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", &tms);
115   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", static_cast<unsigned long>(tv.tv_usec));
116 #else
117   struct _timeb tb;
118   _ftime(&tb);
119   struct tm* tms = localtime(&tb.time);
120   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
121   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
122 #endif
123
124   char levelString[10];
125   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
126   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
127   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
128   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
129   if (level == ERR)     strcpy(levelString, "[ERR]   ");
130   if (level == WARN)    strcpy(levelString, "[WARN]  ");
131   if (level == NOTICE)  strcpy(levelString, "[notice]");
132   if (level == INFO)    strcpy(levelString, "[info]  ");
133   if (level == DEBUG)   strcpy(levelString, "[debug] ");
134
135 #ifndef WIN32
136   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %lu %s - ", levelString, pthread_self(), fromModule);
137 #else
138   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString,  fromModule);
139 #endif
140
141   va_list ap;
142   va_start(ap, message);
143   spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
144   va_end(ap);
145
146   int messageLength = strlen(buffer);
147   if (messageLength < 150)
148   {
149     buffer[messageLength] = '\n';
150     buffer[messageLength+1] = '\0';
151   }
152   else
153   {
154     buffer[149] = '\n';
155     buffer[150] = '\0';
156   }
157   
158   int success = 1;
159   if (enabled)
160   {
161 #ifndef __ANDROID__
162     success = fputs(buffer, logfile);
163     fflush(NULL);
164 #else
165     int and_level=0;
166     switch (level) {
167     case CRAZY:
168     case ALERT:
169     case EMERG :
170     case CRIT:{
171         and_level=ANDROID_LOG_FATAL;
172     } break;
173     case ERR: {
174        and_level=ANDROID_LOG_ERROR;
175     } break;
176     case WARN: {
177        and_level=ANDROID_LOG_WARN;
178     } break;
179     case NOTICE:
180     case INFO: {
181         and_level=ANDROID_LOG_INFO;
182     } break;
183     case DEBUG :{
184         and_level=ANDROID_LOG_DEBUG;
185     } break;
186     };
187     __android_log_vprint(and_level, fromModule,
188                              message,  ap);
189 #endif
190   }
191
192   if (extlog) extlog->LogExtern(buffer); //Replacement for network logging
193
194
195   if (success != EOF)
196     return 1;
197   else
198     return 0;
199
200 }
201
202 void Log::logLongString(const char *fromModule, int level,const char *message)
203 {
204         int string_size=strlen(message);
205         char buffer[100];
206         const char * pointer=message;
207         for (int str_written=0; str_written<string_size;str_written+=99) {
208                 strncpy(buffer,pointer,99);
209                 buffer[99]=0;
210                 pointer+=99;
211                 log(fromModule,level,"%s",buffer);
212         }
213
214 }
215
216 int Log::status()
217 {
218   if (instance && logfile) return 1;
219   else return 0;
220 }
221
222 void Log::setExternLogger(ExternLogger* login) {
223         extlog=login;
224     log("Log", Log::DEBUG, "Extern logging started");
225 }
226
227
228
229