rework log function and protocol

* remove some protocol ifdefs, replace with game features system
This commit is contained in:
Eduardo Bart
2012-05-28 19:04:44 -03:00
parent 4c80d783d6
commit c01b32b032
51 changed files with 391 additions and 280 deletions

View File

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

View File

@@ -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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
return false;
}
} else {
if(pos > (int)m_cacheBuffer.size() || pos < 0) {
logTraceError("operation failed on '", m_name, "': seek pos cannot be greater than file length");
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
} else {
if(m_cacheReadPos+1 > m_cacheBuffer.size()) {
logTraceError("operation failed on '", m_name, "': reached file eof");
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
} else {
if(m_cacheReadPos+2 > m_cacheBuffer.size()) {
logTraceError("operation failed on '", m_name, "': reached file eof");
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
} else {
if(m_cacheReadPos+4 > m_cacheBuffer.size()) {
logTraceError("operation failed on '", m_name, "': reached file eof");
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("operation failed on '%s': %s", m_name, PHYSFS_getLastError());
} else {
if(m_cacheReadPos+8 > m_cacheBuffer.size()) {
logTraceError("operation failed on '", m_name, "': reached file eof");
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': reached file eof");
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", m_name, "': ", PHYSFS_getLastError());
logTraceError("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 '", file, "'");
logError("Unable to save log to '%s'", file);
return;
}

View File

@@ -57,17 +57,17 @@ private:
extern Logger g_logger;
// specialized logging
#define logDebug(...) g_logger.log(Fw::LogDebug, stdext::mkstr(__VA_ARGS__))
#define logInfo(...) g_logger.log(Fw::LogInfo, stdext::mkstr(__VA_ARGS__))
#define logWarning(...) g_logger.log(Fw::LogWarning, stdext::mkstr(__VA_ARGS__))
#define logError(...) g_logger.log(Fw::LogError, stdext::mkstr(__VA_ARGS__))
#define logFatal(...) g_logger.log(Fw::LogFatal, stdext::mkstr(__VA_ARGS__))
#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::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) g_logger.logFunc(Fw::LogInfo, stdext::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) g_logger.logFunc(Fw::LogWarning, stdext::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) g_logger.logFunc(Fw::LogError, stdext::mkstr(__VA_ARGS__), __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 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 '", m_name, "' because dependency '", depName, "' was not found");
logError("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 '", m_name, "' because dependency '", depName, "' has failed to laod");
logError("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 '", m_name, "'");
logInfo("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 '", modName, "' required by '", m_name, "'");
logError("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 '", m_name, "'");
logInfo("Unloaded module '%s'", m_name);
g_modules.updateModuleLoadOrder(asModule());
}
}

View File

@@ -70,7 +70,7 @@ void ModuleManager::discoverModulesPath()
for(const std::string& dir : possibleModulesDirs) {
// try to add module directory
if(g_resources.addToSearchPath(dir, false)) {
logInfo("Using modules directory '", dir.c_str(), "'");
logInfo("Using modules directory '%s'", dir.c_str());
found = true;
break;
}
@@ -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 '", dir.c_str(), "'");
logInfo("Using addons directory '%s'", dir.c_str());
found = true;
break;
}
@@ -103,8 +103,6 @@ ModulePtr ModuleManager::discoverModule(const std::string& moduleFile)
OTMLNodePtr moduleNode = doc->at("Module");
std::string name = moduleNode->valueAt("name");
//if(getModule(name))
// stdext::throw_exception("module '", name, "' already exists, cannot have duplicate module names");
bool push = false;
module = getModule(name);
@@ -118,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 '", moduleFile, "': ", e.what());
logError("Unable to discover module from file '%s': %s", moduleFile, e.what());
}
return module;
}
@@ -127,7 +125,7 @@ void ModuleManager::ensureModuleLoaded(const std::string& moduleName)
{
ModulePtr module = g_modules.getModule(moduleName);
if(!module || !module->load())
logFatal("Unable to load '", moduleName, "' module");
logFatal("Unable to load '%s' module", moduleName);
}
void ModuleManager::unloadModules()

View File

@@ -43,7 +43,7 @@ void ResourceManager::terminate()
bool ResourceManager::setupWriteDir(const std::string& appWriteDirName)
{
std::string userDir = PHYSFS_getUserDir();
std::string dirName = stdext::mkstr(".", appWriteDirName);
std::string dirName = stdext::format(".%s", appWriteDirName);
std::string writeDir = userDir + dirName;
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
if(!PHYSFS_setWriteDir(userDir.c_str()))
@@ -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 '", fullPath, "': ", PHYSFS_getLastError());
logTraceError("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 '", fileName, "': ", PHYSFS_getLastError());
logTraceError("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 '", fileName, "': ", PHYSFS_getLastError());
logTraceError("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: ", path);
logTraceWarning("the following file path is not fully resolved: %s", path);
boost::replace_all(fullPath, "//", "/");
return fullPath;
}