change logger

This commit is contained in:
Eduardo Bart
2012-06-01 17:39:23 -03:00
parent bd2faabe99
commit 14db1066fc
48 changed files with 237 additions and 287 deletions

View File

@@ -45,7 +45,7 @@ bool ConfigManager::load(const std::string& file)
m_confsDoc = confsDoc;
return true;
} catch(stdext::exception& e) {
logError("Unable to load configuration file: %s", e.what());
g_logger.error(stdext::format("Unable to load configuration file: %s", e.what()));
return false;
}
}

View File

@@ -56,7 +56,7 @@ void EventDispatcher::poll()
if(count > 50) {
static Timer reportTimer;
if(reportTimer.running() && reportTimer.ticksElapsed() > 250) {
logError("ATTENTION the event list is not getting empty, this could be caused by some bad code");
g_logger.error("ATTENTION the event list is not getting empty, this could be caused by some bad code");
reportTimer.restart();
}
break;

View File

@@ -39,7 +39,7 @@ FileStream::~FileStream()
void FileStream::cache()
{
if(!m_fileHandle)
logTraceError("no file handle to cache");
g_logger.traceError("no file handle to cache");
// cache entire file into cache buffer
m_cacheReadPos = PHYSFS_tell(m_fileHandle);
@@ -48,7 +48,7 @@ void FileStream::cache()
m_cacheBuffer.resize(size);
int res = PHYSFS_read(m_fileHandle, &m_cacheBuffer[0], size, 1);
if(res == -1)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
PHYSFS_close(m_fileHandle);
m_fileHandle = nullptr;
@@ -58,7 +58,7 @@ bool FileStream::close()
{
if(m_fileHandle) {
if(PHYSFS_isInit() && PHYSFS_close(m_fileHandle) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
m_fileHandle = nullptr;
return true;
@@ -75,7 +75,7 @@ bool FileStream::flush()
return false;
if(PHYSFS_flush(m_fileHandle) == 0) {
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
return false;
}
return true;
@@ -86,7 +86,7 @@ int FileStream::read(void *buffer, int size, int nmemb)
if(m_fileHandle) {
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
if(res == -1) {
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
return 0;
}
return res;
@@ -111,7 +111,7 @@ bool FileStream::write(void *buffer, int count)
return false;
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count) {
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
return false;
}
@@ -122,12 +122,12 @@ bool FileStream::seek(int pos)
{
if(m_fileHandle) {
if(PHYSFS_seek(m_fileHandle, pos) == 0) {
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
return false;
}
} else {
if(pos > (int)m_cacheBuffer.size() || pos < 0) {
logTraceError("operation failed on '%s': seek pos cannot be greater than file length", m_name);
g_logger.traceError(stdext::format("operation failed on '%s': seek pos cannot be greater than file length", m_name));
return false;
}
m_cacheReadPos = pos;
@@ -156,10 +156,10 @@ uint8 FileStream::getU8()
uint8 v = 0;
if(m_fileHandle) {
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
} else {
if(m_cacheReadPos+1 > m_cacheBuffer.size()) {
logTraceError("operation failed on '%s': reached file eof", m_name);
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
return 0;
}
@@ -174,10 +174,10 @@ uint16 FileStream::getU16()
uint16 v = 0;
if(m_fileHandle) {
if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
} else {
if(m_cacheReadPos+2 > m_cacheBuffer.size()) {
logTraceError("operation failed on '%s': reached file eof", m_name);
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
return 0;
}
@@ -192,10 +192,10 @@ uint32 FileStream::getU32()
uint32 v = 0;
if(m_fileHandle) {
if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
} else {
if(m_cacheReadPos+4 > m_cacheBuffer.size()) {
logTraceError("operation failed on '%s': reached file eof", m_name);
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
return 0;
}
@@ -210,10 +210,10 @@ uint64 FileStream::getU64()
uint64 v = 0;
if(m_fileHandle) {
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
} else {
if(m_cacheReadPos+8 > m_cacheBuffer.size()) {
logTraceError("operation failed on '%s': reached file eof", m_name);
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
return 0;
}
@@ -231,12 +231,12 @@ std::string FileStream::getString()
char buffer[8192];
if(m_fileHandle) {
if(PHYSFS_read(m_fileHandle, buffer, 1, len) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
else
str = std::string(buffer, len);
} else {
if(m_cacheReadPos+len > m_cacheBuffer.size()) {
logTraceError("operation failed on '%s': reached file eof", m_name);
g_logger.traceError(stdext::format("operation failed on '%s': reached file eof", m_name));
return 0;
}
@@ -244,7 +244,7 @@ std::string FileStream::getString()
m_cacheReadPos += len;
}
} else {
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
}
return str;
}
@@ -252,23 +252,23 @@ std::string FileStream::getString()
void FileStream::addU8(uint8 v)
{
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
}
void FileStream::addU16(uint8 v)
{
if(PHYSFS_writeULE16(m_fileHandle, v) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
}
void FileStream::addU32(uint8 v)
{
if(PHYSFS_writeULE32(m_fileHandle, v) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
}
void FileStream::addU64(uint8 v)
{
if(PHYSFS_writeULE64(m_fileHandle, v) == 0)
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
g_logger.traceError(stdext::format("operation failed on '%s': %s", m_name, PHYSFS_getLastError()));
}

View File

@@ -89,7 +89,7 @@ void Logger::setLogFile(const std::string& file)
{
m_outFile.open(file.c_str(), std::ios::out | std::ios::app);
if(!m_outFile.is_open() || !m_outFile.good()) {
logError("Unable to save log to '%s'", file);
g_logger.error(stdext::format("Unable to save log to '%s'", file));
return;
}

View File

@@ -44,6 +44,12 @@ public:
void log(Fw::LogLevel level, const std::string& message);
void logFunc(Fw::LogLevel level, const std::string& message, std::string prettyFunction);
void debug(const std::string& what) { log(Fw::LogDebug, what); }
void info(const std::string& what) { log(Fw::LogInfo, what); }
void warning(const std::string& what) { log(Fw::LogWarning, what); }
void error(const std::string& what) { log(Fw::LogError, what); }
void fatal(const std::string& what) { log(Fw::LogError, what); }
void fireOldMessages();
void setLogFile(const std::string& file);
void setOnLog(const OnLogCallback& onLog) { m_onLog = onLog; }
@@ -56,18 +62,11 @@ private:
extern Logger g_logger;
// specialized logging
#define logDebug(...) g_logger.log(Fw::LogDebug, stdext::format(__VA_ARGS__))
#define logInfo(...) g_logger.log(Fw::LogInfo, stdext::format(__VA_ARGS__))
#define logWarning(...) g_logger.log(Fw::LogWarning, stdext::format(__VA_ARGS__))
#define logError(...) g_logger.log(Fw::LogError, stdext::format(__VA_ARGS__))
#define logFatal(...) g_logger.log(Fw::LogFatal, stdext::format(__VA_ARGS__))
#define logTrace() g_logger.logFunc(Fw::LogDebug, "", __PRETTY_FUNCTION__)
#define logTraceDebug(...) g_logger.logFunc(Fw::LogDebug, stdext::format(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) g_logger.logFunc(Fw::LogInfo, stdext::format(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) g_logger.logFunc(Fw::LogWarning, stdext::format(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) g_logger.logFunc(Fw::LogError, stdext::format(__VA_ARGS__), __PRETTY_FUNCTION__)
#define trace() logFunc(Fw::LogDebug, "", __PRETTY_FUNCTION__)
#define traceDebug(a) logFunc(Fw::LogDebug, a, __PRETTY_FUNCTION__)
#define traceInfo(a) logFunc(Fw::LogInfo, a, __PRETTY_FUNCTION__)
#define traceWarning(a) logFunc(Fw::LogWarning, a, __PRETTY_FUNCTION__)
#define traceError(a) logFunc(Fw::LogError, a, __PRETTY_FUNCTION__)
#define logTraceCounter() { \
static int __count = 0; \

View File

@@ -39,12 +39,12 @@ bool Module::load()
for(const std::string& depName : m_dependencies) {
ModulePtr dep = g_modules.getModule(depName);
if(!dep) {
logError("Unable to load module '%s' because dependency '%s' was not found", m_name, depName);
g_logger.error(stdext::format("Unable to load module '%s' because dependency '%s' was not found", m_name, depName));
return false;
}
if(!dep->isLoaded() && !dep->load()) {
logError("Unable to load module '%s' because dependency '%s' has failed to load", m_name, depName);
g_logger.error(stdext::format("Unable to load module '%s' because dependency '%s' has failed to load", m_name, depName));
return false;
}
}
@@ -53,13 +53,13 @@ bool Module::load()
m_loadCallback();
m_loaded = true;
logInfo("Loaded module '%s'", m_name);
g_logger.info(stdext::format("Loaded module '%s'", m_name));
g_modules.updateModuleLoadOrder(asModule());
for(const std::string& modName : m_loadLaterModules) {
ModulePtr dep = g_modules.getModule(modName);
if(!dep)
logError("Unable to find module '%s' required by '%s'", modName, m_name);
g_logger.error(stdext::format("Unable to find module '%s' required by '%s'", modName, m_name));
else if(!dep->isLoaded())
dep->load();
}
@@ -73,7 +73,7 @@ void Module::unload()
if(m_unloadCallback)
m_unloadCallback();
m_loaded = false;
logInfo("Unloaded module '%s'", m_name);
g_logger.info(stdext::format("Unloaded module '%s'", m_name));
g_modules.updateModuleLoadOrder(asModule());
}
}

View File

@@ -54,7 +54,7 @@ void ModuleManager::autoLoadModules(int maxPriority)
break;
ModulePtr module = pair.second;
if(!module->isLoaded() && !module->load())
logFatal("A required module has failed to load, cannot continue to run.");
g_logger.fatal("A required module has failed to load, cannot continue to run.");
}
}
@@ -70,14 +70,14 @@ void ModuleManager::discoverModulesPath()
for(const std::string& dir : possibleModulesDirs) {
// try to add module directory
if(g_resources.addToSearchPath(dir, false)) {
logInfo("Using modules directory '%s'", dir.c_str());
g_logger.info(stdext::format("Using modules directory '%s'", dir.c_str()));
found = true;
break;
}
}
if(!found)
logFatal("Could not find modules directory");
g_logger.fatal("Could not find modules directory");
// search for addons directory
std::string possibleAddonsDirs[] = { "addons",
@@ -88,7 +88,7 @@ void ModuleManager::discoverModulesPath()
for(const std::string& dir : possibleAddonsDirs) {
// try to add module directory
if(g_resources.addToSearchPath(dir, true)) {
logInfo("Using addons directory '%s'", dir.c_str());
g_logger.info(stdext::format("Using addons directory '%s'", dir.c_str()));
found = true;
break;
}
@@ -116,7 +116,7 @@ ModulePtr ModuleManager::discoverModule(const std::string& moduleFile)
if(push)
m_modules.push_back(module);
} catch(stdext::exception& e) {
logError("Unable to discover module from file '%s': %s", moduleFile, e.what());
g_logger.error(stdext::format("Unable to discover module from file '%s': %s", moduleFile, e.what()));
}
return module;
}
@@ -125,7 +125,7 @@ void ModuleManager::ensureModuleLoaded(const std::string& moduleName)
{
ModulePtr module = g_modules.getModule(moduleName);
if(!module || !module->load())
logFatal("Unable to load '%s' module", moduleName);
g_logger.fatal(stdext::format("Unable to load '%s' module", moduleName));
}
void ModuleManager::unloadModules()

View File

@@ -118,7 +118,7 @@ bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, u
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
if(!file)
{
logError(PHYSFS_getLastError());
g_logger.error(PHYSFS_getLastError());
return false;
}
@@ -150,7 +150,7 @@ FileStreamPtr ResourceManager::openFile(const std::string& fileName)
std::string fullPath = resolvePath(fileName);
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file) {
logTraceError("unable to open file '%s': %s", fullPath, PHYSFS_getLastError());
g_logger.error(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
return nullptr;
}
return FileStreamPtr(new FileStream(fullPath, file));
@@ -160,7 +160,7 @@ FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
{
PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str());
if(!file) {
logTraceError("failed to append file '%s': %s", fileName, PHYSFS_getLastError());
g_logger.error(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError()));
return nullptr;
}
return FileStreamPtr(new FileStream(fileName, file));
@@ -170,7 +170,7 @@ FileStreamPtr ResourceManager::createFile(const std::string& fileName)
{
PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str());
if(!file) {
logTraceError("failed to create file '%s': %s", fileName, PHYSFS_getLastError());
g_logger.error(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError()));
return nullptr;
}
return FileStreamPtr(new FileStream(fileName, file));
@@ -211,7 +211,7 @@ std::string ResourceManager::resolvePath(const std::string& path)
fullPath += path;
}
if(!(boost::starts_with(fullPath, "/")))
logTraceWarning("the following file path is not fully resolved: %s", path);
g_logger.traceWarning(stdext::format("the following file path is not fully resolved: %s", path));
boost::replace_all(fullPath, "//", "/");
return fullPath;
}