diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-01-05 20:12:51 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-01-05 20:12:51 +0000 |
commit | 593e65eef0d43f2ff61dc1b4f3160c0a16b5f015 (patch) | |
tree | 8e0a1ca0c73778f0e4d71f4da4d5c47f272915b9 /src/utils | |
parent | f96ca90ba90da3175be96ff7ed34efb78ea5dfed (diff) | |
download | manaserv-593e65eef0d43f2ff61dc1b4f3160c0a16b5f015.tar.gz manaserv-593e65eef0d43f2ff61dc1b4f3160c0a16b5f015.tar.bz2 manaserv-593e65eef0d43f2ff61dc1b4f3160c0a16b5f015.tar.xz manaserv-593e65eef0d43f2ff61dc1b4f3160c0a16b5f015.zip |
Simplified handling of verbosity levels. Optimized code by generating only needed messages.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/logger.cpp | 277 | ||||
-rw-r--r-- | src/utils/logger.h | 249 | ||||
-rw-r--r-- | src/utils/stringfilter.cpp | 3 | ||||
-rw-r--r-- | src/utils/zlib.cpp | 8 |
4 files changed, 124 insertions, 413 deletions
diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp index 36d43f80..f7d26059 100644 --- a/src/utils/logger.cpp +++ b/src/utils/logger.cpp @@ -23,6 +23,7 @@ #include "logger.h" #include <ctime> +#include <fstream> #include <iomanip> #include <iostream> @@ -33,257 +34,97 @@ namespace utils { +static std::ofstream mLogFile; /**< Log file. */ +bool Logger::mHasTimestamp = true; /**< Timestamp flag. */ +bool Logger::mTeeMode = false; /**< Tee mode flag. */ +Logger::Level Logger::mVerbosity = Logger::INFO; /**< Verbosity level. */ /** - * Default constructor. - */ -Logger::Logger(void) - throw() - : mHasTimestamp(true), - mTeeMode(false), - mVerbosity(0) + * Gets the current time. + * + * @return the current time as string. + */ +static std::string getCurrentTime() { - // NOOP -} - - -/** - * Destructor. - */ -Logger::~Logger(void) - throw() -{ - // the destructor of std::ofstream takes care of closing the file - // if it is still open :) -} - - -/** - * Set the log file. - */ -void -Logger::setLogFile(const std::string& logFile) -{ - // close the current log file. - if (mLogFile.is_open()) { - mLogFile.close(); - } - - // open the file for output and remove the former file contents. - mLogFile.open(logFile.c_str(), std::ios::trunc); - - if (!mLogFile.is_open()) { - std::string msg("unable to open "); - msg += logFile; - msg += " for writing."; - - throw std::ios::failure(msg); - } - else { - // by default the streams do not throw any exception - // let std::ios::failbit and std::ios::badbit throw exceptions. - mLogFile.exceptions(std::ios::failbit | std::ios::badbit); - } -} + time_t now; + tm local; + // get current time_t value + time(&now); -/** - * Add/remove the timestamp. - */ -void -Logger::setTimestamp(bool flag) - throw() -{ - mHasTimestamp = flag; -} + // convert time_t to tm struct to break the time into individual + // constituents + local = *(localtime(&now)); + // stringify the time, the format is: [hh:mm:ss] + using namespace std; + ostringstream os; + os << "[" << setw(2) << setfill('0') << local.tm_hour + << ":" << setw(2) << setfill('0') << local.tm_min + << ":" << setw(2) << setfill('0') << local.tm_sec + << "]"; -/** - * Set tee mode. - */ -void -Logger::setTeeMode(bool flag) - throw() -{ - mTeeMode = flag; + return os.str(); } - -/** - * Log a generic message. - */ -void -Logger::log(const std::string& msg, unsigned short atVerbosity) +void Logger::output(std::ostream &os, std::string const &msg, char const *prefix) { - if ( mVerbosity >= atVerbosity ) + if (mHasTimestamp) { - if (mTeeMode) { - log(std::cout, msg); - - if (mLogFile.is_open()) { - log(mLogFile, msg); - } - } - else { - log((mLogFile.is_open() ? mLogFile : std::cout), msg); - } + os << getCurrentTime() << ' '; } -} - -/** - * Log a debug message. - */ -void -Logger::debug(const std::string& msg, unsigned short atVerbosity) -{ - if ( mVerbosity >= atVerbosity ) + if (prefix) { - if (mTeeMode) { - log(std::cout, msg, "[DBG]"); - - if (mLogFile.is_open()) { - log(mLogFile, msg, "[DBG]"); - } - } - else { - log((mLogFile.is_open() ? mLogFile : std::cout), msg, "[DBG]"); - } + os << prefix << ' '; } -} + os << msg << std::endl; +} -/** - * Log an info message. - */ -void -Logger::info(const std::string& msg, unsigned short atVerbosity) +void Logger::setLogFile(std::string const &logFile) { - if ( mVerbosity >= atVerbosity ) + // Close the current log file. + if (mLogFile.is_open()) { - if (mTeeMode) { - log(std::cout, msg, "[INF]"); - - if (mLogFile.is_open()) { - log(mLogFile, msg, "[INF]"); - } - } - else { - log((mLogFile.is_open() ? mLogFile : std::cout), msg, "[INF]"); - } + mLogFile.close(); } -} + // Open the file for output and remove the former file contents. + mLogFile.open(logFile.c_str(), std::ios::trunc); -/** - * Log a warn message. - */ -void -Logger::warn(const std::string& msg, unsigned short atVerbosity) -{ - if ( mVerbosity >= atVerbosity ) + if (!mLogFile.is_open()) { - if (mTeeMode) { - log(std::cerr, msg, "[WRN]"); - - if (mLogFile.is_open()) { - log(mLogFile, msg, "[WRN]"); - } - } - else { - log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[WRN]"); - } + throw std::ios::failure("unable to open " + logFile + "for writing"); } -} - - -/** - * Log an error message. - */ -void -Logger::error(const std::string& msg, unsigned short atVerbosity) -{ - if ( mVerbosity >= atVerbosity ) + else { - if (mTeeMode) { - log(std::cerr, msg, "[ERR]"); - - if (mLogFile.is_open()) { - log(mLogFile, msg, "[ERR]"); - } - } - else { - log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[ERR]"); - } + // by default the streams do not throw any exception + // let std::ios::failbit and std::ios::badbit throw exceptions. + mLogFile.exceptions(std::ios::failbit | std::ios::badbit); } } - -/** - * Log a fatal error message. - */ -void -Logger::fatal(const std::string& msg, unsigned short atVerbosity) +void Logger::output(std::string const& msg, Level atVerbosity) { - if (mTeeMode) { - log(std::cerr, msg, "[FTL]"); - - if (mLogFile.is_open()) { - log(mLogFile, msg, "[FTL]"); - } - } - else { - log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[FTL]"); - } -} + static char const *prefixes[] = + { "[FTL]", "[ERR]", "[WRN]", "[INF]", "[DBG]" }; + if (mVerbosity >= atVerbosity) + { + bool open = mLogFile.is_open(); -/** - * Log a generic message. - */ -void -Logger::log(std::ostream& os, - const std::string& msg, - const std::string& prefix) -{ - if (mHasTimestamp) { - os << getCurrentTime() << " "; - } + if (open) + { + output(mLogFile, msg, prefixes[atVerbosity]); + } - if (prefix != "") { - os << prefix << " "; + if (!open || mTeeMode) + { + output(atVerbosity <= WARN ? std::cerr : std::cout, + msg, prefixes[atVerbosity]); + } } - - os << msg << std::endl; } - -/** - * Get the current time. - */ -std::string -Logger::getCurrentTime(void) -{ - time_t now; - tm local; - - // get current time_t value - time(&now); - - // convert time_t to tm struct to break the time into individual - // constituents - local = *(localtime(&now)); - - // stringify the time, the format is: [hh:mm:ss] - using namespace std; - ostringstream os; - os << "[" << setw(2) << setfill('0') << local.tm_hour - << ":" << setw(2) << setfill('0') << local.tm_min - << ":" << setw(2) << setfill('0') << local.tm_sec - << "]"; - - return os.str(); -} - - } // namespace utils diff --git a/src/utils/logger.h b/src/utils/logger.h index 2eeaded8..0669577b 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -25,11 +25,8 @@ #define _TMWSERV_LOGGER_H_ #include <iosfwd> -#include <fstream> #include <sstream> -#include "singleton.h" - namespace utils { @@ -43,9 +40,6 @@ namespace utils * Limitations: * - not thread-safe. * - * Notes: - * - this class implements the Meyer's singleton design pattern. - * * Example of use: * * <pre> @@ -55,11 +49,9 @@ namespace utils * { * using namespace utils; * - * Logger& logger = Logger::instance(); - * logger.setLogFile("/path/to/logfile"); + * Logger::setLogFile("/path/to/logfile"); * * // log messages using helper macros. - * LOG("hello world") * LOG_DEBUG("level: " << 3) * LOG_INFO("init sound") * LOG_WARN("not implemented") @@ -67,29 +59,33 @@ namespace utils * LOG_FATAL("unable to init graphics") * * // log messages using APIs. - * logger.log("hello world"); - * * std::ostringstream os; * os << "level: " << 3; - * logger.debug(os.str()); + * Logger::output(os.str(), Logger::DEBUG); * - * logger.info("init sound"); - * logger.warn("not implemented"); - * logger.error("resource not found"); - * logger.fatal("unable to init graphics"); + * Logger::output("init sound", Logger::INFO); + * Logger::output("not implemented", Logger::WARN); + * Logger::output("resource not found", Logger::ERROR); + * Logger::output("unable to init graphics", Logger::FATAL); * * return 0; * } * </pre> */ -class Logger: public Singleton<Logger> +class Logger { - // friend so that Singleton can call the constructor. - friend class Singleton<Logger>; - public: + enum Level + { + FATAL = 0, + ERROR, + WARN, + INFO, + DEBUG + }; + /** - * Set the log file. + * Sets the log file. * * This method will open the log file for writing, the former file * contents are removed. @@ -98,116 +94,36 @@ class Logger: public Singleton<Logger> * * @exception std::ios::failure if the log file could not be opened. */ - void - setLogFile(const std::string& logFile); + static void setLogFile(std::string const &logFile); /** - * Add/remove the timestamp. + * Add/removes the timestamp. * * @param flag if true, a log message will always be timestamped * (default = true). */ - void - setTimestamp(bool flag = true) - throw(); + static void setTimestamp(bool flag = true) + { mHasTimestamp = flag; } /** - * Set tee mode. + * Sets tee mode. * * @param flag if true, write messages to both the standard (or error) * output and the log file (if set) (default = true). */ - void - setTeeMode(bool flag = true) - throw(); + static void setTeeMode(bool flag = true) + { mTeeMode = flag; } /** - * Set the verbosity level of the logger. + * Sets the verbosity level of the logger. * * @param verbosity is the level of verbosity. - * 0 = Standard infos - * 1 = + Infos on loading/unloading/reloading resources - * 2 = + Packets names and messages sent. - */ - void - setVerbosity(unsigned short verbosity = 0) { mVerbosity = verbosity; } - - /** - * Set tee mode. - * - * @param flag if true, write messages to both the standard (or error) - * output and the log file (if set) (default = true). - */ - unsigned short - getVerbosity() { return mVerbosity; } - - /** - * Log a generic message. - * - * @param msg the message to log. - * - * @param atVerbosity the minimum verbosity level - * to log this - * - * @exception std::ios::failure. - */ - void - log(const std::string &msg, unsigned short atVerbosity = 0); - - /** - * Log a debug message. - * - * @param msg the message to log. - * - * @param atVerbosity the minimum verbosity level - * to log this - * - * @exception std::ios::failure. - */ - void - debug(const std::string &msg, unsigned short atVerbosity = 0); - - /** - * Log an info message. - * - * @param msg the message to log. - * - * @param atVerbosity the minimum verbosity level - * to log this - * - * @exception std::ios::failure. - */ - void - info(const std::string &msg, unsigned short atVerbosity = 0); - - /** - * Log a warn message. - * - * @param msg the message to log. - * - * @param atVerbosity the minimum verbosity level - * to log this - * - * @exception std::ios::failure. - */ - void - warn(const std::string &msg, unsigned short atVerbosity = 0); - - /** - * Log an error message. - * - * @param msg the message to log. - * - * @param atVerbosity the minimum verbosity level - * to log this - * - * @exception std::ios::failure. */ - void - error(const std::string &msg, unsigned short atVerbosity = 0); + static void setVerbosity(Level verbosity) + { mVerbosity = verbosity; } /** - * Log a fatal error message. + * Logs a generic message. * * @param msg the message to log. * @@ -216,60 +132,23 @@ class Logger: public Singleton<Logger> * * @exception std::ios::failure. */ - void - fatal(const std::string &msg, unsigned short atVerbosity = 0); + static void output(std::string const &msg, Level atVerbosity); + static Level mVerbosity; /**< Verbosity level. */ private: - /** - * Default constructor. - */ - Logger(void) - throw(); - - /** - * Destructor. - */ - ~Logger(void) - throw(); - - /** - * Copy constructor. - */ - Logger(const Logger& rhs); - - /** - * Assignment operator. - */ - Logger& - operator=(const Logger& rhs); + static bool mHasTimestamp; /**< Timestamp flag. */ + static bool mTeeMode; /**< Tee mode flag. */ /** - * Log a generic message. + * Logs a generic message. * * @param os the output stream. * @param msg the message to log. - * @param prefix the message prefix (default = ""). + * @param prefix the message prefix. * * @exception std::ios::failure. */ - void - log(std::ostream& os, - const std::string& msg, - const std::string& prefix = ""); - - /** - * Get the current time. - * - * @return the current time as string. - */ - std::string - getCurrentTime(void); - - - std::ofstream mLogFile; /**< the log file */ - bool mHasTimestamp; /**< the timestamp flag */ - bool mTeeMode; /**< the tee mode flag */ - unsigned short mVerbosity; /**< keeps the verbosity level */ + static void output(std::ostream &os, std::string const &msg, char const *prefix); }; @@ -277,47 +156,39 @@ class Logger: public Singleton<Logger> // HELPER MACROS - -#define LOG(msg, atVerbosity) \ - do { \ - std::ostringstream os; \ - os << msg; \ - ::utils::Logger::instance().log(os.str(), atVerbosity); \ - } while(0) - -#define LOG_DEBUG(msg, atVerbosity) \ - do { \ - std::ostringstream os; \ - os << msg; \ - ::utils::Logger::instance().debug(os.str(), atVerbosity); \ +#define LOG_DEBUG(msg) \ + do if (::utils::Logger::mVerbosity >= ::utils::Logger::DEBUG) { \ + std::ostringstream os; \ + os << msg; \ + ::utils::Logger::output(os.str(), ::utils::Logger::DEBUG); \ } while (0) -#define LOG_INFO(msg, atVerbosity) \ - do { \ - std::ostringstream os; \ - os << msg; \ - ::utils::Logger::instance().info(os.str(), atVerbosity); \ +#define LOG_INFO(msg) \ + do if (::utils::Logger::mVerbosity >= ::utils::Logger::INFO) { \ + std::ostringstream os; \ + os << msg; \ + ::utils::Logger::output(os.str(), ::utils::Logger::INFO); \ } while (0) -#define LOG_WARN(msg, atVerbosity) \ - do { \ - std::ostringstream os; \ - os << msg; \ - ::utils::Logger::instance().warn(os.str(), atVerbosity); \ +#define LOG_WARN(msg) \ + do if (::utils::Logger::mVerbosity >= ::utils::Logger::WARN) { \ + std::ostringstream os; \ + os << msg; \ + ::utils::Logger::output(os.str(), ::utils::Logger::WARN); \ } while (0) -#define LOG_ERROR(msg, atVerbosity) \ - do { \ - std::ostringstream os; \ - os << msg; \ - ::utils::Logger::instance().error(os.str(), atVerbosity); \ +#define LOG_ERROR(msg) \ + do if (::utils::Logger::mVerbosity >= ::utils::Logger::ERROR) { \ + std::ostringstream os; \ + os << msg; \ + ::utils::Logger::output(os.str(), ::utils::Logger::ERROR); \ } while (0) -#define LOG_FATAL(msg) \ - do { \ - std::ostringstream os; \ - os << msg; \ - ::utils::Logger::instance().fatal(os.str(), 0); \ +#define LOG_FATAL(msg) \ + do if (::utils::Logger::mVerbosity >= ::utils::Logger::FATAL) { \ + std::ostringstream os; \ + os << msg; \ + ::utils::Logger::output(os.str(), ::utils::Logger::FATAL); \ } while (0) #endif // _TMWSERV_LOGGER_H_ diff --git a/src/utils/stringfilter.cpp b/src/utils/stringfilter.cpp index 79480838..ba1f1b99 100644 --- a/src/utils/stringfilter.cpp +++ b/src/utils/stringfilter.cpp @@ -77,7 +77,7 @@ void StringFilter::writeSlangFilterList() bool StringFilter::filterContent(const std::string& text) { if (!mInitialized) { - LOG_INFO("Slangs List is not initialized.", 2); + LOG_DEBUG("Slangs List is not initialized."); return true; } @@ -109,7 +109,6 @@ bool StringFilter::isEmailValid(const std::string& email) if ((email.length() < MIN_EMAIL_LENGTH) || (email.length() > MAX_EMAIL_LENGTH)) { - LOG_INFO(email << ": Email too short or too long.", 1); return false; } diff --git a/src/utils/zlib.cpp b/src/utils/zlib.cpp index b72c70f5..48e01c5f 100644 --- a/src/utils/zlib.cpp +++ b/src/utils/zlib.cpp @@ -31,16 +31,16 @@ static void logZlibError(int error) switch (error) { case Z_MEM_ERROR: - LOG_ERROR("Out of memory while decompressing data!", 0); + LOG_ERROR("Out of memory while decompressing data!"); break; case Z_VERSION_ERROR: - LOG_ERROR("Incompatible zlib version!", 0); + LOG_ERROR("Incompatible zlib version!"); break; case Z_DATA_ERROR: - LOG_ERROR("Incorrect zlib compressed data!", 0); + LOG_ERROR("Incorrect zlib compressed data!"); break; default: - LOG_ERROR("Unknown error while decompressing data!", 0); + LOG_ERROR("Unknown error while decompressing data!"); } } |