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