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