summaryrefslogtreecommitdiff
path: root/src/log.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2011-01-02 01:48:38 +0200
committerAndrei Karas <akaras@inbox.ru>2011-01-02 02:41:24 +0200
commit3eeae12c498d1a4dbe969462d2ba841f77ee3ccb (patch)
treeff8eab35e732bc0749fc11677c8873a7b3a58704 /src/log.cpp
downloadManaVerse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.tar.gz
ManaVerse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.tar.bz2
ManaVerse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.tar.xz
ManaVerse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.zip
Initial commit.
This code based on mana client http://www.gitorious.org/mana/mana and my private repository.
Diffstat (limited to 'src/log.cpp')
-rw-r--r--src/log.cpp213
1 files changed, 213 insertions, 0 deletions
diff --git a/src/log.cpp b/src/log.cpp
new file mode 100644
index 000000000..bf8519443
--- /dev/null
+++ b/src/log.cpp
@@ -0,0 +1,213 @@
+/*
+ * The Mana Client
+ * Copyright (C) 2004-2009 The Mana World Development Team
+ * Copyright (C) 2009-2010 The Mana Developers
+ *
+ * This file is part of The Mana Client.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <iostream>
+#include <sstream>
+
+#include "log.h"
+
+#include "configuration.h"
+
+#include "gui/widgets/chattab.h"
+
+#ifdef WIN32
+#include <windows.h>
+#elif defined __APPLE__
+#include <Carbon/Carbon.h>
+#elif __linux__ || __linux
+#include <stdlib.h>
+#endif
+
+#include <sys/time.h>
+
+Logger::Logger():
+ mLogToStandardOut(true),
+ mChatWindow(NULL),
+ mDebugLog(false)
+{
+}
+
+Logger::~Logger()
+{
+ if (mLogFile.is_open())
+ {
+ mLogFile.close();
+ }
+}
+
+void Logger::setLogFile(const std::string &logFilename)
+{
+ if (mLogFile.is_open())
+ mLogFile.close();
+
+ mLogFile.open(logFilename.c_str(), std::ios_base::trunc);
+
+ if (!mLogFile.is_open())
+ {
+ std::cout << "Warning: error while opening " << logFilename <<
+ " for writing.\n";
+ }
+}
+
+void Logger::log(std::string str)
+{
+ log("%s", str.c_str());
+}
+
+void Logger::dlog(std::string str)
+{
+ if (!mDebugLog)
+ return;
+
+ // Get the current system time
+ timeval tv;
+ gettimeofday(&tv, NULL);
+
+ // Print the log entry
+ std::stringstream timeStr;
+ timeStr << "["
+ << ((((tv.tv_sec / 60) / 60) % 24 < 10) ? "0" : "")
+ << (int)(((tv.tv_sec / 60) / 60) % 24)
+ << ":"
+ << (((tv.tv_sec / 60) % 60 < 10) ? "0" : "")
+ << (int)((tv.tv_sec / 60) % 60)
+ << ":"
+ << ((tv.tv_sec % 60 < 10) ? "0" : "")
+ << (int)(tv.tv_sec % 60)
+ << "."
+ << (((tv.tv_usec / 10000) % 100) < 10 ? "0" : "")
+ << (int)((tv.tv_usec / 10000) % 100)
+ << "] ";
+
+ if (mLogFile.is_open())
+ mLogFile << timeStr.str() << str << std::endl;
+
+ if (mLogToStandardOut)
+ std::cout << timeStr.str() << str << std::endl;
+
+ if (mChatWindow && debugChatTab)
+ debugChatTab->chatLog(str, BY_LOGGER);
+}
+
+void Logger::log1(const char *buf)
+{
+ // Get the current system time
+ timeval tv;
+ gettimeofday(&tv, NULL);
+
+ // Print the log entry
+ std::stringstream timeStr;
+ timeStr << "["
+ << ((((tv.tv_sec / 60) / 60) % 24 < 10) ? "0" : "")
+ << (int)(((tv.tv_sec / 60) / 60) % 24)
+ << ":"
+ << (((tv.tv_sec / 60) % 60 < 10) ? "0" : "")
+ << (int)((tv.tv_sec / 60) % 60)
+ << ":"
+ << ((tv.tv_sec % 60 < 10) ? "0" : "")
+ << (int)(tv.tv_sec % 60)
+ << "."
+ << (((tv.tv_usec / 10000) % 100) < 10 ? "0" : "")
+ << (int)((tv.tv_usec / 10000) % 100)
+ << "] ";
+
+ if (mLogFile.is_open())
+ mLogFile << timeStr.str() << buf << std::endl;
+
+ if (mLogToStandardOut)
+ std::cout << timeStr.str() << buf << std::endl;
+
+ if (mChatWindow && debugChatTab)
+ debugChatTab->chatLog(buf, BY_LOGGER);
+}
+
+void Logger::log(const char *log_text, ...)
+{
+ unsigned int size = 1024;
+ char* buf = 0;
+ if (strlen(log_text) * 3 > size)
+ size = (unsigned)strlen(log_text) * 3;
+
+ buf = new char[size];
+ va_list ap;
+
+ // 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
+ timeval tv;
+ gettimeofday(&tv, NULL);
+
+ // Print the log entry
+ std::stringstream timeStr;
+ timeStr << "["
+ << ((((tv.tv_sec / 60) / 60) % 24 < 10) ? "0" : "")
+ << (int)(((tv.tv_sec / 60) / 60) % 24)
+ << ":"
+ << (((tv.tv_sec / 60) % 60 < 10) ? "0" : "")
+ << (int)((tv.tv_sec / 60) % 60)
+ << ":"
+ << ((tv.tv_sec % 60 < 10) ? "0" : "")
+ << (int)(tv.tv_sec % 60)
+ << "."
+ << (((tv.tv_usec / 10000) % 100) < 10 ? "0" : "")
+ << (int)((tv.tv_usec / 10000) % 100)
+ << "] ";
+
+ if (mLogFile.is_open())
+ mLogFile << timeStr.str() << buf << std::endl;
+
+ if (mLogToStandardOut)
+ std::cout << timeStr.str() << buf << std::endl;
+
+ if (mChatWindow && debugChatTab)
+ debugChatTab->chatLog(buf, BY_LOGGER);
+
+ // 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);
+#elif defined __APPLE__
+ Str255 msg;
+ CFStringRef error;
+ error = CFStringCreateWithCString(NULL,
+ error_text.c_str(),
+ kCFStringEncodingMacRoman);
+ CFStringGetPascalString(error, msg, 255, kCFStringEncodingMacRoman);
+ StandardAlert(kAlertStopAlert,
+ "\pError",
+ (ConstStr255Param) msg, NULL, NULL);
+#elif defined __linux__ || __linux
+ std::cerr << "Error: " << error_text << std::endl;
+ std::string msg = "xmessage \"" + error_text + "\"";
+ system(msg.c_str());
+#else
+ std::cerr << "Error: " << error_text << std::endl;
+#endif
+ exit(1);
+}