1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/***************************************************************************
* Copyright (C) 2005-2013 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
#include <algorithm>
#include <iomanip>
#include <fstream>
// 3rd party library includes
#include <SDL.h>
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "modules.h"
#include "logger.h"
#include "util/base/exception.h"
// define the module info relationships structure here, begin
struct ModuleInfo {
logmodule_t module;
logmodule_t parent;
std::string name;
};
MODULE_INFO_RELATIONSHIPS
// end
namespace FIFE {
LogManager* LogManager::m_instance = NULL;
Logger::Logger(logmodule_t module):
m_module(module) {
}
Logger::~Logger() {
}
void Logger::log(LogManager::LogLevel level, const std::string& msg) {
LogManager::instance()->log(level, m_module, msg);
}
void Logger::log(LogManager::LogLevel level, const LMsg& msg) {
LogManager::instance()->log(level, m_module, msg.str);
}
LogManager* LogManager::instance() {
if (!m_instance) {
m_instance = new LogManager();
}
return m_instance;
}
LogManager::~LogManager() {
delete m_instance;
}
void LogManager::log(LogLevel level, logmodule_t module, const std::string& msg) {
if (level < m_level) {
return;
}
if (!isVisible(module)) {
return;
}
std::string lvlstr = "";
switch (level) {
case LEVEL_DEBUG: lvlstr = "DEBUG";
break;
case LEVEL_LOG: lvlstr = "LOG";
break;
case LEVEL_WARN: lvlstr = "WARN";
break;
case LEVEL_ERROR: lvlstr = "ERROR";
break;
case LEVEL_PANIC: lvlstr = "PANIC";
break;
default: lvlstr = "ERROR";
break;
}
if (m_logtoprompt) {
std::cout << moduleInfos[module].name << ":" << lvlstr << ":" << msg << std::endl;
}
if (m_logtofile) {
*m_logfile << moduleInfos[module].name << ":" << lvlstr << ":" << msg << std::endl;
}
if (level == LEVEL_PANIC) {
abort();
}
}
void LogManager::setLevelFilter(LogLevel level) {
m_level = level;
}
LogManager::LogLevel LogManager::getLevelFilter() {
return m_level;
}
void LogManager::addVisibleModule(logmodule_t module) {
validateModule(module);
int32_t ind = static_cast<int32_t>(module);
m_modules[ind] = true;
if (moduleInfos[ind].parent != LM_CORE) {
addVisibleModule(moduleInfos[ind].parent);
}
}
void LogManager::removeVisibleModule(logmodule_t module) {
validateModule(module);
m_modules[module] = false;
}
void LogManager::clearVisibleModules() {
for (int32_t i = 0; i < LM_MODULE_MAX; i++) {
m_modules[i] = false;
}
}
void LogManager::setLogToPrompt(bool logtoprompt) {
m_logtoprompt = logtoprompt;
}
bool LogManager::isLogToPrompt() {
return m_logtoprompt;
}
void LogManager::setLogToFile(bool logtofile) {
if(logtofile){
m_logfile = new std::ofstream("fife.log");
}
else {
if (m_logfile){
delete m_logfile;
}
}
m_logtofile = logtofile;
}
bool LogManager::isLogToFile() {
return m_logtofile;
}
bool LogManager::isVisible(logmodule_t module) {
if (!m_modules[module]) {
return false;
}
if (moduleInfos[module].parent != LM_CORE) {
return isVisible(moduleInfos[module].parent);
}
return true;
}
LogManager::LogManager():
m_level(LEVEL_DEBUG),
module_check_stack(),
m_logtofile(false),
m_logtoprompt(false) {
validateModuleDescription(LM_CORE);
m_logfile = 0;
clearVisibleModules();
}
void LogManager::validateModule(logmodule_t m) {
if ((m <= LM_CORE) || (m >= LM_MODULE_MAX)) {
std::cout << "Invalid module received in LogManager: " << m << ", aborting\n";
abort();
}
}
void LogManager::validateModuleDescription(logmodule_t module) {
if (module == LM_CORE) {
for (int32_t m = static_cast<int32_t>(LM_CORE)+1; m < static_cast<int32_t>(LM_MODULE_MAX); m++) {
if (moduleInfos[m].module != static_cast<logmodule_t>(m)) {
std::ostringstream stream;
stream << m;
std::string msg = "Log module definition ids do not match in index ";
msg += stream.str();
std::cout << msg << std::endl;
throw InvalidFormat(msg);
}
module_check_stack.clear();
validateModuleDescription(static_cast<logmodule_t>(m));
}
} else {
module_check_stack.push_back(module);
if (count(module_check_stack.begin(), module_check_stack.end(), module) > 1) {
throw InvalidFormat("Log module definition hierarchy contains cycles");
}
}
}
std::string LogManager::getModuleName(logmodule_t module) {
return moduleInfos[module].name;
}
}
|