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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 Log* Log::instance = NULL;
40 Log* Log::getInstance()
45 void Log::upLogLevel()
49 if (logLevel == Log::DEBUG)
51 log("Log", logLevel, "Log level is at its highest already");
56 log("Log", logLevel, "Log level is now %i", logLevel);
59 void Log::downLogLevel()
63 if (logLevel == Log::CRAZY)
65 log("Log", logLevel, "Log level is at its lowest already");
70 log("Log", logLevel, "Log level is now %i", logLevel);
73 int Log::init(int startLogLevel, char* fileName)
75 logLevel = startLogLevel;
77 logfile = fopen(fileName, "a");
91 if (!initted) return 1;
93 if (logfile) fclose(logfile);
97 int Log::log(const char *fromModule, int level, const char* message, ...)
99 if (!initted) return 0;
101 if (level > logLevel) return 1;
107 gettimeofday(&tv, NULL);
108 struct tm* tm = localtime(&tv.tv_sec);
109 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm);
110 spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
113 char levelString[10];
114 if (level == CRAZY) strcpy(levelString, "[CRAZY] ");
115 if (level == EMERG) strcpy(levelString, "[EMERG] ");
116 if (level == ALERT) strcpy(levelString, "[ALERT] ");
117 if (level == CRIT) strcpy(levelString, "[CRIT] ");
118 if (level == ERR) strcpy(levelString, "[ERR] ");
119 if (level == WARN) strcpy(levelString, "[WARN] ");
120 if (level == NOTICE) strcpy(levelString, "[notice]");
121 if (level == INFO) strcpy(levelString, "[info] ");
122 if (level == DEBUG) strcpy(levelString, "[debug] ");
124 spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
127 va_start(ap, message);
128 spaceLeft = vsnprintf(&buffer[150-spaceLeft], spaceLeft, message, ap);
131 int messageLength = strlen(buffer);
132 if (messageLength < 150)
134 buffer[messageLength] = '\n';
135 buffer[messageLength+1] = '\0';
143 int success = fputs(buffer, logfile);