From b718afce8c635d7b9eb46de0414448a60109ca4f Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Sun, 8 May 2005 17:32:30 +0000 Subject: Including logger files into the server project. --- src/Makefile.am | 4 ++- src/log.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 63 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/log.cpp create mode 100644 src/log.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index a6abf79f..2dfdbe45 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,7 +20,9 @@ tmwserv_SOURCES = main.cpp \ packet.h \ packet.cpp \ sqlite/SQLiteWrapper.cpp \ - sqlite/SQLiteWrapper.h + sqlite/SQLiteWrapper.h \ + log.h \ + log.cpp # set the include path found by configure INCLUDES= $(all_includes) $(SQLITE_CFLAGS) diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 00000000..6d5f4095 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,94 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "log.h" +#ifdef WIN32 +#include +#endif + +#include + + +Logger::Logger(const std::string &logFilename) +{ + logFile.open(logFilename.c_str(), std::ios_base::trunc); + + if (!logFile.is_open()) + { + std::cout << "Warning: error while opening " << logFilename << + " for writing.\n"; + } +} + +Logger::~Logger() +{ + if (logFile.is_open()) + { + logFile.close(); + } +} + +void Logger::log(const char *log_text, ...) +{ + if (logFile.is_open()) + { + char* buf = new char[1024]; + va_list ap; + time_t t; + + // Use a temporary buffer to fill in the variables + va_start(ap, log_text); + vsprintf(buf, log_text, ap); + va_end(ap); + + // Get the current system time + time(&t); + + // Print the log entry + std::stringstream timeStr; + timeStr << "["; + timeStr << ((((t / 60) / 60) % 24 < 10) ? "0" : ""); + timeStr << (int)(((t / 60) / 60) % 24); + timeStr << ":"; + timeStr << (((t / 60) % 60 < 10) ? "0" : ""); + timeStr << (int)((t / 60) % 60); + timeStr << ":"; + timeStr << ((t % 60 < 10) ? "0" : ""); + timeStr << (int)(t % 60); + timeStr << "] "; + + logFile << timeStr.str() << buf << std::endl; + + // Delete temporary buffer + delete[] buf; + } +} + +void Logger::error(const std::string &error_text) +{ + log("Error: %s", error_text.c_str()); +#ifdef WIN32 + MessageBox(NULL, error_text.c_str(), "Error", MB_ICONERROR | MB_OK); +#else + std::cerr << "Error: " << error_text << std::endl; +#endif + exit(1); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 00000000..f9bc3300 --- /dev/null +++ b/src/log.h @@ -0,0 +1,63 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; 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 +#include + +/** + * The Log Class : Useful to write debug or info messages + */ +class Logger +{ + public: + /** + * Constructor, opens log file for writing. + */ + Logger(const std::string &logFilename); + + /** + * Destructor, closes log file. + */ + ~Logger(); + + /** + * Enters a message in the log. The message will be timestamped. + */ + void log(const char *log_text, ...); + + /** + * Log an error and quit. The error will pop-up in Windows and will be + * printed to standard error everywhere else. + */ + void error(const std::string &error_text); + + private: + std::ofstream logFile; +}; + +#endif -- cgit v1.2.3-70-g09d2