| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief OÇNX |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "LogManager.h" |
| 11 |
#include "LockGuard.h" |
| 12 |
#include <string> |
| 13 |
|
| 14 |
using namespace beego; |
| 15 |
|
| 16 |
|
| 17 |
struct LogManager::pImpl { |
| 18 |
std::string log_fname; |
| 19 |
ErrorLevel handle_level; |
| 20 |
SDL_mutex* mutex; |
| 21 |
FILE* fp; |
| 22 |
|
| 23 |
pImpl(void) |
| 24 |
: log_fname("errors.txt"), handle_level(Warning), |
| 25 |
mutex(SDL_CreateMutex()), fp(NULL) { |
| 26 |
} |
| 27 |
|
| 28 |
~pImpl(void) { |
| 29 |
SDL_DestroyMutex(mutex); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
LogManager::LogManager(void) : pimpl(new pImpl) { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
LogManager* LogManager::getObject(int id) { |
| 39 |
static LogManager* obj = new LogManager(); |
| 40 |
return obj; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
LogManager::~LogManager(void) { |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
void LogManager::setFileName(const char* fileName, int id) { |
| 49 |
LockGuard guard(pimpl->mutex); |
| 50 |
pimpl->log_fname = fileName; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
void LogManager::setLogMask(const ErrorLevel level, int id) { |
| 55 |
LockGuard guard(pimpl->mutex); |
| 56 |
pimpl->handle_level = level; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
void LogManager::write(const ErrorLevel level, const char* message) { |
| 61 |
if (level > pimpl->handle_level) { |
| 62 |
return; |
| 63 |
} |
| 64 |
LockGuard guard(pimpl->mutex); |
| 65 |
|
| 66 |
// Ot@CĚśŹ |
| 67 |
if (pimpl->fp == NULL) { |
| 68 |
pimpl->fp = fopen(pimpl->log_fname.c_str(), "w"); |
| 69 |
if (pimpl->fp == NULL) { |
| 70 |
perror("LogManager::write"); |
| 71 |
return; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// bZ[WĚŤÝ |
| 76 |
const char* level_message[] = { |
| 77 |
"Unknown", // NoWrite |
| 78 |
"Attack ", // Attack |
| 79 |
"Error ", // Error |
| 80 |
"Warning", // Warning |
| 81 |
"Notice ", // Notice |
| 82 |
}; |
| 83 |
int error_level = (level > Notice) ? 0 : level; |
| 84 |
fprintf(pimpl->fp, "%s: %s", level_message[error_level], message); |
| 85 |
size_t len = strlen(message); |
| 86 |
if (message[len - 1] != '\n') { |
| 87 |
// bZ[WĚĹăŞüsĹČŻęÎAüsđÇÁˇé |
| 88 |
fprintf(pimpl->fp, "%c", '\n'); |
| 89 |
} |
| 90 |
fflush(pimpl->fp); |
| 91 |
} |