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