[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[netPanzer-CVS] netpanzer/src/Lib Log.cpp Log.hpp
From: |
Ivo Danihelka |
Subject: |
[netPanzer-CVS] netpanzer/src/Lib Log.cpp Log.hpp |
Date: |
Wed, 17 Sep 2003 19:46:43 -0400 |
CVSROOT: /cvsroot/netpanzer
Module name: netpanzer
Branch:
Changes by: Ivo Danihelka <address@hidden> 03/09/17 19:46:42
Modified files:
src/Lib : Log.cpp Log.hpp
Log message:
Simple log levels: debug, info, warning
Patches:
Index: netpanzer/src/Lib/Log.cpp
diff -u netpanzer/src/Lib/Log.cpp:1.4 netpanzer/src/Lib/Log.cpp:1.5
--- netpanzer/src/Lib/Log.cpp:1.4 Tue Sep 16 16:16:09 2003
+++ netpanzer/src/Lib/Log.cpp Wed Sep 17 19:46:42 2003
@@ -1,5 +1,6 @@
/*
-Copyright (C) 2003 Matthias Braun <address@hidden>
+Copyright (C) 2003 Matthias Braun <address@hidden>,
+Ivo Danihelka <address@hidden>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -24,30 +25,61 @@
Logger logger;
#endif
+// like syslog levels
+const int Logger::LEVEL_DEBUG = 7;
+const int Logger::LEVEL_INFO = 6;
+const int Logger::LEVEL_WARNING = 4;
+//-----------------------------------------------------------------
Logger::Logger()
{
- logfile = new std::ofstream("log.txt");
- if(!logfile->good()) {
- delete logfile;
- logfile = 0;
- }
+ m_logfile = fopen("log.txt", "w");
}
-
+//-----------------------------------------------------------------
Logger::~Logger()
{
- delete logfile;
+ if (m_logfile != 0) {
+ fclose(m_logfile);
+ }
+}
+//-----------------------------------------------------------------
+ void
+Logger::log(int priority, const char *fmt, va_list ap)
+{
+ if (m_logLevel >= priority) {
+ vfprintf(stderr, fmt, ap);
+ if (m_logfile != 0) {
+ vfprintf(m_logfile, fmt, ap);
+ }
+ }
}
+//-----------------------------------------------------------------
+ void
+Logger::debug(const char *fmt, ...)
+{
+ va_list ap;
-void Logger::log(const char* msg, ...)
+ va_start(ap, fmt);
+ log(Logger::LEVEL_DEBUG, fmt, ap);
+ va_end(ap);
+}
+//-----------------------------------------------------------------
+ void
+Logger::info(const char *fmt, ...)
{
- char buf[1024];
+ va_list ap;
- va_list args;
- va_start(args, msg);
- vsnprintf(buf, sizeof(buf), msg, args);
- va_end(args);
+ va_start(ap, fmt);
+ log(Logger::LEVEL_INFO, fmt, ap);
+ va_end(ap);
+}
+//-----------------------------------------------------------------
+ void
+Logger::warning(const char *fmt, ...)
+{
+ va_list ap;
- fprintf(stderr, "%s\n", buf);
- *logfile << buf << "\n";
+ va_start(ap, fmt);
+ log(Logger::LEVEL_WARNING, fmt, ap);
+ va_end(ap);
}
Index: netpanzer/src/Lib/Log.hpp
diff -u netpanzer/src/Lib/Log.hpp:1.4 netpanzer/src/Lib/Log.hpp:1.5
--- netpanzer/src/Lib/Log.hpp:1.4 Tue Sep 16 16:16:09 2003
+++ netpanzer/src/Lib/Log.hpp Wed Sep 17 19:46:42 2003
@@ -1,5 +1,6 @@
/*
-Copyright (C) 2003 Matthias Braun <address@hidden>
+Copyright (C) 2003 Matthias Braun <address@hidden>,
+Ivo Danihelka <address@hidden>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -19,29 +20,43 @@
#define __LIB_LOG_HPP__
#include <config.h>
-#include <fstream>
+#include <stdio.h>
class Logger
{
public:
+ static const int LEVEL_DEBUG;
+ static const int LEVEL_INFO;
+ static const int LEVEL_WARNING;
+
Logger();
~Logger();
- void log(const char* msg, ...)
- __attribute__((format (__printf__, 2, 3)));
+ void setLogLevel(int logLevel);
+ int getLogLevel();
+
+ void debug(const char *fmt, ...)
+ __attribute__((format (__printf__, 2, 3)));
+ void info(const char *fmt, ...)
+ __attribute__((format (__printf__, 2, 3)));
+ void warning(const char *fmt, ...)
+ __attribute__((format (__printf__, 2, 3)));
private:
- std::ofstream* logfile;
+ void log(int priority, const char *fmt, va_list ap);
+
+ int m_logLevel;
+ FILE* m_logfile;
};
-#ifdef DO_LOGGING
extern Logger logger;
-#define FUNC(funcname) logger.log("Entering function '%s'.", funcname);
-#define LOG(x) logger.log x
+#ifdef DO_LOGGING
+#define FUNC(funcname) logger.debug("Entering function '%s'.", funcname);
+#define LOG(x) logger.info x
#else
#define FUNC(funcname)
#define LOG(x)
-#endif
+#endif // DO_LOGGING
#endif