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.
24 Log* Log::instance = NULL;
40 Log* Log::getInstance()
45 void Log::upLogLevel()
47 if (logLevel == Log::DEBUG)
49 log("Log", logLevel, "Log level is at its highest already");
54 log("Log", logLevel, "Log level is now %i", logLevel);
57 void Log::downLogLevel()
59 if (logLevel == Log::CRAZY)
61 log("Log", logLevel, "Log level is at its lowest already");
66 log("Log", logLevel, "Log level is now %i", logLevel);
69 int Log::init(int startLogLevel, char* fileName, int tenabled)
72 logLevel = startLogLevel;
74 // logfile = fopen(fileName, "a");
75 // logfile = fopen(stdout, "a");
77 // logfile = fopen("/log", "a");
79 if (logfile) return 1;
85 if (!initted) return 1;
86 if (enabled) fclose(logfile);
90 int Log::log(char *fromModule, int level, char* message, ...)
92 if (!instance || !logfile) return 0;
94 if (!enabled) return 1;
95 if (level > logLevel) return 1;
102 gettimeofday(&tv, NULL);
103 struct tm* tms = localtime(&tv.tv_sec);
107 struct tm* tms = localtime(&tb.time);
109 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
111 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
113 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
116 char levelString[10];
117 if (level == CRAZY) strcpy(levelString, "[CRAZY] ");
118 if (level == EMERG) strcpy(levelString, "[EMERG] ");
119 if (level == ALERT) strcpy(levelString, "[ALERT] ");
120 if (level == CRIT) strcpy(levelString, "[CRIT] ");
121 if (level == ERR) strcpy(levelString, "[ERR] ");
122 if (level == WARN) strcpy(levelString, "[WARN] ");
123 if (level == NOTICE) strcpy(levelString, "[notice]");
124 if (level == INFO) strcpy(levelString, "[info] ");
125 if (level == DEBUG) strcpy(levelString, "[debug] ");
128 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %d %s - ", levelString, getpid(), fromModule);
130 spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
134 va_start(ap, message);
135 spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
138 int messageLength = strlen(buffer);
139 if (messageLength < 150)
141 buffer[messageLength] = '\n';
142 buffer[messageLength+1] = '\0';
150 int success = fputs(buffer, logfile);
162 if (instance && logfile) return 1;