2 Copyright 2004-2005 Chris Tallon
\r
3 Copyright 2003-2004 University Of Bradford
\r
5 This file is part of VOMP.
\r
7 VOMP is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 VOMP is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with VOMP; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
27 #include <android/log.h>
\r
30 Log* Log::instance = NULL;
\r
34 if (instance) return;
\r
47 Log* Log::getInstance()
\r
52 void Log::upLogLevel()
\r
54 if (logLevel == Log::DEBUG)
\r
56 log("Log", logLevel, "Log level is at its highest already");
\r
61 log("Log", logLevel, "Log level is now %i", logLevel);
\r
64 void Log::downLogLevel()
\r
66 if (logLevel == Log::CRAZY)
\r
68 log("Log", logLevel, "Log level is at its lowest already");
\r
73 log("Log", logLevel, "Log level is now %i", logLevel);
\r
76 int Log::init(int startLogLevel,const char* fileName, int tenabled)
\r
79 logLevel = startLogLevel;
\r
81 // logfile = fopen(fileName, "a");
\r
82 // logfile = fopen(stdout, "a");
\r
84 // logfile = fopen("/log", "a");
\r
86 if (logfile) return 1;
\r
92 if (!initted) return 1;
\r
93 if (enabled) fclose(logfile);
\r
97 int Log::log(const char *fromModule, int level,const char* message, ...)
\r
99 if (!instance || !logfile) return 0;
\r
101 if (!enabled && !extlog) return 1;
\r
102 if (level > logLevel) return 1;
\r
105 int spaceLeft = 150;
\r
109 gettimeofday(&tv, NULL);
\r
110 struct tm* tms = localtime(&tv.tv_sec);
\r
114 struct tm* tms = localtime(&tb.time);
\r
116 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
\r
118 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
\r
120 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
\r
123 char levelString[10];
\r
124 if (level == CRAZY) strcpy(levelString, "[CRAZY] ");
\r
125 if (level == EMERG) strcpy(levelString, "[EMERG] ");
\r
126 if (level == ALERT) strcpy(levelString, "[ALERT] ");
\r
127 if (level == CRIT) strcpy(levelString, "[CRIT] ");
\r
128 if (level == ERR) strcpy(levelString, "[ERR] ");
\r
129 if (level == WARN) strcpy(levelString, "[WARN] ");
\r
130 if (level == NOTICE) strcpy(levelString, "[notice]");
\r
131 if (level == INFO) strcpy(levelString, "[info] ");
\r
132 if (level == DEBUG) strcpy(levelString, "[debug] ");
\r
135 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %d %s - ", levelString, getpid(), fromModule);
\r
137 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
\r
141 va_start(ap, message);
\r
142 spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
\r
145 int messageLength = strlen(buffer);
\r
146 if (messageLength < 150)
\r
148 buffer[messageLength] = '\n';
\r
149 buffer[messageLength+1] = '\0';
\r
153 buffer[149] = '\n';
\r
154 buffer[150] = '\0';
\r
160 #ifndef __ANDROID__
\r
161 success = fputs(buffer, logfile);
\r
170 and_level=ANDROID_LOG_FATAL;
\r
173 and_level=ANDROID_LOG_ERROR;
\r
176 and_level=ANDROID_LOG_WARN;
\r
180 and_level=ANDROID_LOG_INFO;
\r
183 and_level=ANDROID_LOG_DEBUG;
\r
186 __android_log_vprint(and_level, fromModule,
\r
191 if (extlog) extlog->LogExtern(buffer); //Replacement for network logging
\r
194 if (success != EOF)
\r
201 void Log::logLongString(const char *fromModule, int level,const char *message)
\r
203 int string_size=strlen(message);
\r
205 const char * pointer=message;
\r
206 for (int str_written=0; str_written<string_size;str_written+=119) {
\r
207 strncpy(buffer,pointer,99);
\r
210 log(fromModule,level,"%s",buffer);
\r
217 if (instance && logfile) return 1;
\r
221 void Log::setExternLogger(ExternLogger* login) {
\r
223 log("Log", Log::DEBUG, "Extern logging started");
\r