git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / common / Logger.cpp
blob00504d115bdb9eee042a2337e6355902617284af
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <common/Defines.h>
22 #include <common/Logger.h>
23 #include <common/LoggerI.h>
24 #include <SDL/SDL.h>
25 #include <SDL/SDL_thread.h>
26 #include <time.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <algorithm>
31 // ************************************************
32 // NOTE: This logger is and needs to be thread safe
33 // ************************************************
35 static SDL_mutex *logMutex_ = 0;
36 static Uint32 threadId = (Uint32) -1;
37 Logger * Logger::instance_ = 0;
39 Logger * Logger::instance()
41 if (!instance_)
43 instance_ = new Logger;
45 return instance_;
48 Logger::Logger()
50 if (!logMutex_) logMutex_ = SDL_CreateMutex();
53 Logger::~Logger()
57 void Logger::addLogger(LoggerI *logger)
59 Logger::instance();
60 SDL_LockMutex(logMutex_);
61 Logger::instance()->loggers_.push_back(logger);
62 SDL_UnlockMutex(logMutex_);
65 void Logger::remLogger(LoggerI *logger)
67 Logger::instance();
68 std::list<LoggerI *>::iterator itor;
69 SDL_LockMutex(logMutex_);
70 itor = Logger::instance()->loggers_.begin();
71 while(itor != Logger::instance()->loggers_.end()) {
72 if (*itor == logger) {
73 Logger::instance()->loggers_.erase(itor);
74 break;
76 itor++;
78 SDL_UnlockMutex(logMutex_);
81 void Logger::log(const std::string &text)
83 log(text.c_str());
86 void Logger::log(const char *text)
88 if (!text) return;
90 Logger::instance();
92 SDL_LockMutex(logMutex_);
94 LoggerInfo info;
95 info.setMessage(text);
96 addLog(info);
98 SDL_UnlockMutex(logMutex_);
100 if (SDL_ThreadID() == threadId)
102 processLogEntries();
106 void Logger::log(const LoggerInfo &info)
108 Logger::instance();
110 SDL_LockMutex(logMutex_);
111 addLog((LoggerInfo &) info);
112 SDL_UnlockMutex(logMutex_);
115 void Logger::addLog(LoggerInfo &info)
117 // Add the time to the beginning of the log message
118 info.setTime();
120 // Add single or multiple lines
121 char *found = (char *) strchr(info.getMessage(), '\n');
122 char *start = (char *) info.getMessage();
123 if (found)
125 while (found)
127 *found = '\0';
128 LoggerInfo *newInfo = new LoggerInfo(info);
129 newInfo->setMessage(start);
130 instance_->entries_.push_back(newInfo);
131 start = found;
132 start++;
134 found = strchr(start, '\n');
136 /*if (SDL_ThreadID() == threadId)
138 processLogEntries();
141 if (start[0] != '\0')
143 LoggerInfo *newInfo = new LoggerInfo(info);
144 newInfo->setMessage(start);
145 instance_->entries_.push_back(newInfo);
148 else
150 instance_->entries_.push_back(
151 new LoggerInfo(info));
155 void Logger::processLogEntries()
157 if (threadId == (Uint32) -1)
159 threadId = SDL_ThreadID();
162 Logger::instance();
164 SDL_LockMutex(logMutex_);
165 std::list<LoggerInfo *> &entries = Logger::instance()->entries_;
166 while (!entries.empty())
168 LoggerInfo *firstEntry = entries.front();
170 std::list<LoggerI *> &loggers = Logger::instance()->loggers_;
171 std::list<LoggerI *>::iterator logItor;
172 for (logItor = loggers.begin();
173 logItor != loggers.end();
174 logItor++)
176 LoggerI *log = (*logItor);
177 log->logMessage(*firstEntry);
180 entries.pop_front();
181 delete firstEntry;
183 SDL_UnlockMutex(logMutex_);