Develop and Download Open Source Software

Browse Subversion Repository

Contents of /common/LogManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 272 - (show annotations) (download) (as text)
Sat Mar 8 20:51:59 2008 UTC (16 years, 1 month ago) by satofumi
File MIME type: text/x-c++src
File size: 1753 byte(s)
Visual Studio 2005 の警告を修正。
1 /*!
2 \file
3 \brief ƒƒOŠÇ—ƒNƒ‰ƒX
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 // ƒƒOƒtƒ@ƒ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 // ƒƒbƒZ[ƒ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 // ƒƒbƒZ[ƒW‚ĚĹŒă‚Ş‰üs‚Ĺ‚Č‚Ż‚ę‚΁A‰üs‚đ’ljÁ‚ˇ‚é
88 fprintf(pimpl->fp, "%c", '\n');
89 }
90 fflush(pimpl->fp);
91 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26