summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYohann Ferreira <bertram@cegetel.net>2005-05-08 17:32:30 +0000
committerYohann Ferreira <bertram@cegetel.net>2005-05-08 17:32:30 +0000
commitb718afce8c635d7b9eb46de0414448a60109ca4f (patch)
tree42b5749a94b8b2c561bedb90ba176e84d6792de5 /src
parentd914b694c3c167450fdc0c04bec082a36eb5c963 (diff)
downloadmanaserv-b718afce8c635d7b9eb46de0414448a60109ca4f.tar.gz
manaserv-b718afce8c635d7b9eb46de0414448a60109ca4f.tar.bz2
manaserv-b718afce8c635d7b9eb46de0414448a60109ca4f.tar.xz
manaserv-b718afce8c635d7b9eb46de0414448a60109ca4f.zip
Including logger files into the server project.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am4
-rw-r--r--src/log.cpp94
-rw-r--r--src/log.h63
3 files changed, 160 insertions, 1 deletions
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 <windows.h>
+#endif
+
+#include <sstream>
+
+
+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 <stdlib.h>
+#include <stdarg.h>
+#include <time.h>
+#include <string>
+#include <iostream>
+#include <fstream>
+
+/**
+ * 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