2 Copyright 2004-2013 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.
29 Log* Log::instance = NULL;
45 Log* Log::getInstance()
50 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()
68 if (logLevel == Log::CRAZY)
70 log("Log", logLevel, "Log level is at its lowest already");
75 log("Log", logLevel, "Log level is now %i", logLevel);
78 int Log::init(int startLogLevel, const char* fileName)
80 logLevel = startLogLevel;
82 logfile = fopen(fileName, "a");
96 if (!initted) return 1;
98 if (logfile) fclose(logfile);
102 int Log::log(const char *fromModule, int level, const char* message, ...)
104 if (!initted) return 0;
106 if (level > logLevel) return 1;
108 int lineLength = 250;
110 char buffer[lineLength + 1];
111 int spaceLeft = lineLength;
114 gettimeofday(&tv, NULL);
115 struct tm* tm = localtime(&tv.tv_sec);
116 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm);
117 spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
120 char levelString[10];
121 if (level == CRAZY) strcpy(levelString, "[CRAZY] ");
122 if (level == EMERG) strcpy(levelString, "[EMERG] ");
123 if (level == ALERT) strcpy(levelString, "[ALERT] ");
124 if (level == CRIT) strcpy(levelString, "[CRIT] ");
125 if (level == ERR) strcpy(levelString, "[ERR] ");
126 if (level == WARN) strcpy(levelString, "[WARN] ");
127 if (level == NOTICE) strcpy(levelString, "[notice]");
128 if (level == INFO) strcpy(levelString, "[info] ");
129 if (level == DEBUG) strcpy(levelString, "[debug] ");
131 spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
134 va_start(ap, message);
135 spaceLeft = vsnprintf(&buffer[lineLength-spaceLeft], spaceLeft, message, ap);
138 int messageLength = strlen(buffer);
139 if (messageLength < lineLength)
141 buffer[messageLength] = '\n';
142 buffer[messageLength+1] = '\0';
146 buffer[lineLength-1] = '\n';
147 buffer[lineLength] = '\0';
150 int success = fputs(buffer, logfile);