Bubba-3D  0.9.0
Awesome game engine!
Logger.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include "Logger.h"
18 #include "sstream"
19 #include <time.h>
20 
21 using namespace std;
22 
23 unsigned int Logger::currentLogLevel = DEBUG;
24 std::vector<LogHandler*> Logger::logHandlers;
25 
26 void Logger::logDebug(string msg) {
27  log(DEBUG, msg);
28 }
29 
30 void Logger::logWarning(string msg) {
31  log(WARNING, msg);
32 }
33 
34 void Logger::logError(string msg) {
35  log(SEVERE, msg);
36 }
37 
38 void Logger::logInfo(string msg) {
39  log(INFO, msg);
40 }
41 
42 void Logger::log(unsigned int level, string msg) {
43  if (level >= currentLogLevel) {
44  for (auto logHandler : logHandlers) {
45  std::stringstream ss;
46  ss << levelToString(level) << "\t" << getTime().c_str() << "\t" << msg.c_str() << "\n";
47  logHandler->log(ss.str());
48  }
49  }
50 }
51 
52 void Logger::addLogHandler(LogHandler *logHandler) {
53  logHandlers.push_back(logHandler);
54 }
55 
56 void Logger::setLogLevel(unsigned int level) {
57  currentLogLevel = level;
58 }
59 
60 string Logger::levelToString(unsigned int level) {
61  switch (level) {
62  case INFO: return "INFO";
63  case DEBUG: return "DEBUG";
64  case WARNING: return "WARNING";
65  case SEVERE: return "SEVERE";
66  default: return "BAD level";
67  }
68 }
69 
70 string Logger::getTime() {
71  time_t now = time(0);
72  struct tm tstruct;
73  char buf[80];
74  tstruct = *localtime(&now);
75  strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
76  return buf;
77 }