| 1 |
/* |
| 2 |
* The Mana World |
| 3 |
* Copyright (C) 2004 The Mana World Development Team |
| 4 |
* |
| 5 |
* This file is part of The Mana World. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <iostream> |
| 23 |
#include <sstream> |
| 24 |
|
| 25 |
#include <sys/time.h> |
| 26 |
|
| 27 |
#ifdef WIN32 |
| 28 |
#include <windows.h> |
| 29 |
#elif __APPLE__ |
| 30 |
#include <Carbon/Carbon.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "log.h" |
| 34 |
|
| 35 |
#include "gui/widgets/chattab.h" |
| 36 |
|
| 37 |
Logger::Logger(): |
| 38 |
mLogToStandardOut(false), |
| 39 |
mChatWindow(NULL) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
Logger::~Logger() |
| 44 |
{ |
| 45 |
if (mLogFile.is_open()) |
| 46 |
{ |
| 47 |
mLogFile.close(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void Logger::setLogFile(const std::string &logFilename) |
| 52 |
{ |
| 53 |
mLogFile.open(logFilename.c_str(), std::ios_base::trunc); |
| 54 |
|
| 55 |
if (!mLogFile.is_open()) |
| 56 |
{ |
| 57 |
std::cout << "Warning: error while opening " << logFilename << |
| 58 |
" for writing.\n"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void Logger::log(const char *log_text, ...) |
| 63 |
{ |
| 64 |
if (!mLogFile.is_open()) |
| 65 |
{ |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
char* buf = new char[1024]; |
| 70 |
va_list ap; |
| 71 |
|
| 72 |
// Use a temporary buffer to fill in the variables |
| 73 |
va_start(ap, log_text); |
| 74 |
vsprintf(buf, log_text, ap); |
| 75 |
va_end(ap); |
| 76 |
|
| 77 |
// Get the current system time |
| 78 |
timeval tv; |
| 79 |
gettimeofday(&tv, NULL); |
| 80 |
|
| 81 |
// Print the log entry |
| 82 |
std::stringstream timeStr; |
| 83 |
timeStr << "[" |
| 84 |
<< ((((tv.tv_sec / 60) / 60) % 24 < 10) ? "0" : "") |
| 85 |
<< (int)(((tv.tv_sec / 60) / 60) % 24) |
| 86 |
<< ":" |
| 87 |
<< (((tv.tv_sec / 60) % 60 < 10) ? "0" : "") |
| 88 |
<< (int)((tv.tv_sec / 60) % 60) |
| 89 |
<< ":" |
| 90 |
<< ((tv.tv_sec % 60 < 10) ? "0" : "") |
| 91 |
<< (int)(tv.tv_sec % 60) |
| 92 |
<< "." |
| 93 |
<< (((tv.tv_usec / 10000) % 100) < 10 ? "0" : "") |
| 94 |
<< (int)((tv.tv_usec / 10000) % 100) |
| 95 |
<< "] "; |
| 96 |
|
| 97 |
mLogFile << timeStr.str() << buf << std::endl; |
| 98 |
|
| 99 |
if (mLogToStandardOut) |
| 100 |
{ |
| 101 |
std::cout << timeStr.str() << buf << std::endl; |
| 102 |
} |
| 103 |
|
| 104 |
if (mChatWindow) |
| 105 |
{ |
| 106 |
localChatTab->chatLog(buf, BY_LOGGER); |
| 107 |
} |
| 108 |
|
| 109 |
// Delete temporary buffer |
| 110 |
delete[] buf; |
| 111 |
} |
| 112 |
|
| 113 |
void Logger::error(const std::string &error_text) |
| 114 |
{ |
| 115 |
log("Error: %s", error_text.c_str()); |
| 116 |
#ifdef WIN32 |
| 117 |
MessageBox(NULL, error_text.c_str(), "Error", MB_ICONERROR | MB_OK); |
| 118 |
#elif defined __APPLE__ |
| 119 |
Str255 msg; |
| 120 |
CFStringRef error; |
| 121 |
error = CFStringCreateWithCString(NULL, |
| 122 |
error_text.c_str(), |
| 123 |
kCFStringEncodingMacRoman); |
| 124 |
CFStringGetPascalString(error, msg, 255, kCFStringEncodingMacRoman); |
| 125 |
StandardAlert(kAlertStopAlert, |
| 126 |
"\pError", |
| 127 |
(ConstStr255Param) msg, NULL, NULL); |
| 128 |
#else |
| 129 |
std::cerr << "Error: " << error_text << std::endl; |
| 130 |
#endif |
| 131 |
exit(1); |
| 132 |
} |