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()
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 if (logfile) return 1;
83 if (!initted) return 1;
84 if (logfile) fclose(logfile);
88 int Log::log(char *fromModule, int level, char* message, ...)
90 if (!instance || !logfile) return 0;
92 if (!enabled) return 1;
93 if (level > logLevel) return 1;
99 gettimeofday(&tv, NULL);
100 struct tm* tm = localtime(&tv.tv_sec);
101 spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm);
102 spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
105 char levelString[10];
106 if (level == CRAZY) strcpy(levelString, "[CRAZY] ");
107 if (level == EMERG) strcpy(levelString, "[EMERG] ");
108 if (level == ALERT) strcpy(levelString, "[ALERT] ");
109 if (level == CRIT) strcpy(levelString, "[CRIT] ");
110 if (level == ERR) strcpy(levelString, "[ERR] ");
111 if (level == WARN) strcpy(levelString, "[WARN] ");
112 if (level == NOTICE) strcpy(levelString, "[notice]");
113 if (level == INFO) strcpy(levelString, "[info] ");
114 if (level == DEBUG) strcpy(levelString, "[debug] ");
116 spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule);
119 va_start(ap, message);
120 spaceLeft = vsnprintf(&buffer[150-spaceLeft], spaceLeft, message, ap);
123 int messageLength = strlen(buffer);
124 if (messageLength < 150)
126 buffer[messageLength] = '\n';
127 buffer[messageLength+1] = '\0';
135 int success = fputs(buffer, logfile);
147 if (instance && logfile) return 1;