2 Copyright 2004-2005 Chris Tallon
3 Copyright 2003-2004 University Of Bradford
5 This file is part of VOMP.
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.
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.
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.
27 #include <android/log.h>
30 Log* Log::instance = NULL;
47 Log* Log::getInstance()
52 void Log::upLogLevel()
54 if (logLevel == Log::DEBUG)
56 log("Log", logLevel, "Log level is at its highest already");
61 log("Log", logLevel, "Log level is now %i", logLevel);
64 void Log::downLogLevel()
66 if (logLevel == Log::CRAZY)
68 log("Log", logLevel, "Log level is at its lowest already");
73 log("Log", logLevel, "Log level is now %i", logLevel);
76 int Log::init(int startLogLevel,const char* fileName, int tenabled)
79 logLevel = startLogLevel;
81 // logfile = fopen(fileName, "a");
82 // logfile = fopen(stdout, "a");
84 // logfile = fopen("/log", "a");
86 if (logfile) return 1;
92 if (!initted) return 1;
93 if (enabled && (logfile != stdout)) fclose(logfile);
97 int Log::log(const char *fromModule, int level,const char* message, ...)
99 if (!instance || !logfile) return 0;
101 if (!enabled && !extlog) return 1;
102 if (level > logLevel) return 1;
109 gettimeofday(&tv, NULL);
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);
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);
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] ");
134 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %lu %s - ", levelString, pthread_self(), fromModule);
136 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
140 va_start(ap, message);
141 spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
144 int messageLength = strlen(buffer);
145 if (messageLength < 150)
147 buffer[messageLength] = '\n';
148 buffer[messageLength+1] = '\0';
160 success = fputs(buffer, logfile);
169 and_level=ANDROID_LOG_FATAL;
172 and_level=ANDROID_LOG_ERROR;
175 and_level=ANDROID_LOG_WARN;
179 and_level=ANDROID_LOG_INFO;
182 and_level=ANDROID_LOG_DEBUG;
185 __android_log_vprint(and_level, fromModule,
190 if (extlog) extlog->LogExtern(buffer); //Replacement for network logging
200 void Log::logLongString(const char *fromModule, int level,const char *message)
202 int string_size=strlen(message);
204 const char * pointer=message;
205 for (int str_written=0; str_written<string_size;str_written+=99) {
206 strncpy(buffer,pointer,99);
209 log(fromModule,level,"%s",buffer);
216 if (instance && logfile) return 1;
220 void Log::setExternLogger(ExternLogger* login) {
222 log("Log", Log::DEBUG, "Extern logging started");