1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
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>
25 #include <SDL/SDL_thread.h>
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()
43 instance_
= new Logger
;
50 if (!logMutex_
) logMutex_
= SDL_CreateMutex();
57 void Logger::addLogger(LoggerI
*logger
)
60 SDL_LockMutex(logMutex_
);
61 Logger::instance()->loggers_
.push_back(logger
);
62 SDL_UnlockMutex(logMutex_
);
65 void Logger::remLogger(LoggerI
*logger
)
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
);
78 SDL_UnlockMutex(logMutex_
);
81 void Logger::log(const std::string
&text
)
86 void Logger::log(const char *text
)
92 SDL_LockMutex(logMutex_
);
95 info
.setMessage(text
);
98 SDL_UnlockMutex(logMutex_
);
100 if (SDL_ThreadID() == threadId
)
106 void Logger::log(const LoggerInfo
&info
)
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
120 // Add single or multiple lines
121 char *found
= (char *) strchr(info
.getMessage(), '\n');
122 char *start
= (char *) info
.getMessage();
128 LoggerInfo
*newInfo
= new LoggerInfo(info
);
129 newInfo
->setMessage(start
);
130 instance_
->entries_
.push_back(newInfo
);
134 found
= strchr(start
, '\n');
136 /*if (SDL_ThreadID() == threadId)
141 if (start
[0] != '\0')
143 LoggerInfo
*newInfo
= new LoggerInfo(info
);
144 newInfo
->setMessage(start
);
145 instance_
->entries_
.push_back(newInfo
);
150 instance_
->entries_
.push_back(
151 new LoggerInfo(info
));
155 void Logger::processLogEntries()
157 if (threadId
== (Uint32
) -1)
159 threadId
= SDL_ThreadID();
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();
176 LoggerI
*log
= (*logItor
);
177 log
->logMessage(*firstEntry
);
183 SDL_UnlockMutex(logMutex_
);