From 302fbd5948c9934fdde0cd8899ca23828450bdb9 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Mon, 1 Aug 2005 22:07:53 +0000 Subject: [PATCH] New logging system --- log.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ log.h | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100755 log.c create mode 100755 log.h diff --git a/log.c b/log.c new file mode 100755 index 0000000..1f36bb3 --- /dev/null +++ b/log.c @@ -0,0 +1,157 @@ +/* + Copyright 2004-2005 Chris Tallon + Copyright 2003-2004 University Of Bradford + + This file is part of VOMP. + + VOMP is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + VOMP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with VOMP; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "log.h" + +Log* Log::instance = NULL; + +Log::Log() +{ + if (instance) return; + instance = this; + logfile = NULL; + initted = 0; + logLevel = 0; +} + +Log::~Log() +{ + instance = NULL; +} + +Log* Log::getInstance() +{ + return instance; +} + +void Log::upLogLevel() +{ + if (logLevel == Log::DEBUG) + { + log("Log", logLevel, "Log level is at its highest already"); + return; + } + + logLevel++; + log("Log", logLevel, "Log level is now %i", logLevel); +} + +void Log::downLogLevel() +{ + if (logLevel == Log::CRAZY) + { + log("Log", logLevel, "Log level is at its lowest already"); + return; + } + + logLevel--; + log("Log", logLevel, "Log level is now %i", logLevel); +} + +int Log::init(int startLogLevel, char* fileName, int tenabled) +{ + initted = 1; + logLevel = startLogLevel; + enabled = tenabled; + + if (!enabled) return 1; + + logfile = fopen(fileName, "a"); + if (logfile) + { + return 1; + } + else + { + enabled = 0; + return 0; + } +} + +int Log::shutdown() +{ + if (!initted) return 1; + if (logfile) fclose(logfile); + return 1; +} + +int Log::log(char *fromModule, int level, char* message, ...) +{ + if (!instance || !logfile) return 0; + + if (!enabled) return 1; + if (level > logLevel) return 1; + + char buffer[151]; + int spaceLeft = 150; + + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm* tm = localtime(&tv.tv_sec); + spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm); + spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec); + + + char levelString[10]; + if (level == CRAZY) strcpy(levelString, "[CRAZY] "); + if (level == EMERG) strcpy(levelString, "[EMERG] "); + if (level == ALERT) strcpy(levelString, "[ALERT] "); + if (level == CRIT) strcpy(levelString, "[CRIT] "); + if (level == ERR) strcpy(levelString, "[ERR] "); + if (level == WARN) strcpy(levelString, "[WARN] "); + if (level == NOTICE) strcpy(levelString, "[notice]"); + if (level == INFO) strcpy(levelString, "[info] "); + if (level == DEBUG) strcpy(levelString, "[debug] "); + + spaceLeft -= snprintf(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule); + + va_list ap; + va_start(ap, message); + spaceLeft = vsnprintf(&buffer[150-spaceLeft], spaceLeft, message, ap); + va_end(ap); + + int messageLength = strlen(buffer); + if (messageLength < 150) + { + buffer[messageLength] = '\n'; + buffer[messageLength+1] = '\0'; + } + else + { + buffer[149] = '\n'; + buffer[150] = '\0'; + } + + int success = fputs(buffer, logfile); + fflush(NULL); + + if (success != EOF) + return 1; + else + return 0; + +} + +int Log::status() +{ + if (instance && logfile) return 1; + else return 0; +} diff --git a/log.h b/log.h new file mode 100755 index 0000000..bf45f42 --- /dev/null +++ b/log.h @@ -0,0 +1,89 @@ +/* + Copyright 2004-2005 Chris Tallon + Copyright 2003-2004 University Of Bradford + + This file is part of VOMP. + + VOMP is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + VOMP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with VOMP; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include +#include + +class Log +{ + public: + Log(); + ~Log(); + static Log* getInstance(); + + int init(int defaultLevel, char* fileName, int enabled); + int shutdown(); + int log(char *fromModule, int level, char *message, ...); + int status(); + void upLogLevel(); + void downLogLevel(); + + const static int CRAZY = 0; // mad crazy things that should never happen + const static int EMERG = 1; // human assist required NOW + const static int ALERT = 2; // system unusable, but happy to sit there + const static int CRIT = 3; // still working, but maybe about to die + const static int ERR = 4; // that response is not even listed... + const static int WARN = 5; // this could be a bad thing. still running tho + const static int NOTICE = 6; // significant good thing + const static int INFO = 7; // verbose good thing + const static int DEBUG = 8; // debug-level messages + + private: + static Log* instance; + int initted; + int logLevel; + int enabled; + + FILE *logfile; +}; + +#endif + +/* + +Documentation +------------- + +This class is intended to be instatiated once by the core. +For a one off use: + +Log::getInstance()->log("", Log::, ""); + +Or, a pointer can be stored and used: + +Log *myptr = Log::getInstance(); + +myptr->log("", Log::, ""); +myptr->log("", Log::, ""); + +Level usages are above. + +The message parameter in the log function can be used in the same way as printf, eg. + +myptr->log("", Log::, "Success: %s %i", stringpointer, integer); + +*/ -- 2.39.5