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.
22 #include <sys/types.h>
25 #include <sys/syscall.h>
36 #include <android/log.h>
39 Log* Log::instance = NULL;
56 Log* Log::getInstance()
61 void Log::upLogLevel()
63 if (logLevel == Log::DEBUG)
65 log("Log", logLevel, "Log level is at its highest already");
70 log("Log", logLevel, "Log level is now %i", logLevel);
73 void Log::downLogLevel()
75 if (logLevel == Log::CRAZY)
77 log("Log", logLevel, "Log level is at its lowest already");
82 log("Log", logLevel, "Log level is now %i", logLevel);
85 int Log::init(int startLogLevel, const char* fileName, int tenabled)
90 logLevel = startLogLevel;
93 // FIXME fix this with NCONFIG
94 // logfile = fopen(fileName, "a");
95 // logfile = fopen(stdout, "a");
97 // logfile = fopen("/log", "a");
99 if (logfile) return 1;
105 if (!initted) return 1;
106 if (enabled && (logfile != stdout)) fclose(logfile);
110 int Log::log(const char *fromModule, int level,const char* message, ...)
112 if (!instance || !logfile) return 0;
114 if (!enabled && !extlog) return 1;
115 if (level > logLevel) return 1;
122 gettimeofday(&tv, NULL);
124 LOCALTIME_R(&tv.tv_sec, &tms);
125 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", &tms);
126 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", static_cast<unsigned long>(tv.tv_usec));
130 struct tm* tms = localtime(&tb.time);
131 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
132 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
135 char levelString[10];
136 if (level == CRAZY) strcpy(levelString, "[CRAZY] ");
137 if (level == EMERG) strcpy(levelString, "[EMERG] ");
138 if (level == ALERT) strcpy(levelString, "[ALERT] ");
139 if (level == CRIT) strcpy(levelString, "[CRIT] ");
140 if (level == ERR) strcpy(levelString, "[ERR] ");
141 if (level == WARN) strcpy(levelString, "[WARN] ");
142 if (level == NOTICE) strcpy(levelString, "[notice]");
143 if (level == INFO) strcpy(levelString, "[info] ");
144 if (level == DEBUG) strcpy(levelString, "[debug] ");
147 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %lu %li %s - ", levelString, pthread_self(), syscall(SYS_gettid), fromModule);
149 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
153 va_start(ap, message);
154 spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
157 int messageLength = strlen(buffer);
158 if (messageLength < 150)
160 buffer[messageLength] = '\n';
161 buffer[messageLength+1] = '\0';
173 success = fputs(buffer, logfile);
182 and_level=ANDROID_LOG_FATAL;
185 and_level=ANDROID_LOG_ERROR;
188 and_level=ANDROID_LOG_WARN;
192 and_level=ANDROID_LOG_INFO;
195 and_level=ANDROID_LOG_DEBUG;
198 __android_log_vprint(and_level, fromModule,
203 if (extlog) extlog->LogExtern(buffer); //Replacement for network logging
213 void Log::logLongString(const char *fromModule, int level,const char *message)
215 int string_size=strlen(message);
217 const char * pointer=message;
218 for (int str_written=0; str_written<string_size;str_written+=99) {
219 strncpy(buffer,pointer,99);
222 log(fromModule,level,"%s",buffer);
229 if (instance && logfile) return 1;
233 void Log::setExternLogger(ExternLogger* login) {
235 log("Log", Log::DEBUG, "Extern logging started");