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