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>
24 #include <sys/syscall.h>
33 #include <android/log.h>
36 Log* Log::instance = NULL;
53 Log* Log::getInstance()
58 void Log::upLogLevel()
60 if (logLevel == Log::DEBUG)
62 log("Log", logLevel, "Log level is at its highest already");
67 log("Log", logLevel, "Log level is now %i", logLevel);
70 void Log::downLogLevel()
72 if (logLevel == Log::CRAZY)
74 log("Log", logLevel, "Log level is at its lowest already");
79 log("Log", logLevel, "Log level is now %i", logLevel);
82 int Log::init(int startLogLevel, const char* fileName, int tenabled)
87 logLevel = startLogLevel;
89 //logfile = fopen(fileName, "a");
90 // logfile = fopen(stdout, "a");
92 // logfile = fopen("/log", "a");
94 if (logfile) return 1;
100 if (!initted) return 1;
101 if (enabled && (logfile != stdout)) fclose(logfile);
105 int Log::log(const char *fromModule, int level,const char* message, ...)
107 if (!instance || !logfile) return 0;
109 if (!enabled && !extlog) return 1;
110 if (level > logLevel) return 1;
117 gettimeofday(&tv, NULL);
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));
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);
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] ");
142 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %lu %li %s - ", levelString, pthread_self(), syscall(SYS_gettid), fromModule);
144 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
148 va_start(ap, message);
149 spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
152 int messageLength = strlen(buffer);
153 if (messageLength < 150)
155 buffer[messageLength] = '\n';
156 buffer[messageLength+1] = '\0';
168 success = fputs(buffer, logfile);
177 and_level=ANDROID_LOG_FATAL;
180 and_level=ANDROID_LOG_ERROR;
183 and_level=ANDROID_LOG_WARN;
187 and_level=ANDROID_LOG_INFO;
190 and_level=ANDROID_LOG_DEBUG;
193 __android_log_vprint(and_level, fromModule,
198 if (extlog) extlog->LogExtern(buffer); //Replacement for network logging
208 void Log::logLongString(const char *fromModule, int level,const char *message)
210 int string_size=strlen(message);
212 const char * pointer=message;
213 for (int str_written=0; str_written<string_size;str_written+=99) {
214 strncpy(buffer,pointer,99);
217 log(fromModule,level,"%s",buffer);
224 if (instance && logfile) return 1;
228 void Log::setExternLogger(ExternLogger* login) {
230 log("Log", Log::DEBUG, "Extern logging started");