mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 05:53:26 +02:00
lua console and some changes
This commit is contained in:
@@ -5,17 +5,20 @@
|
||||
|
||||
Configs g_configs;
|
||||
|
||||
bool Configs::load(const std::string& fileName)
|
||||
bool Configs::load(const std::string& file)
|
||||
{
|
||||
m_fileName = fileName;
|
||||
m_fileName = file;
|
||||
|
||||
if(!g_resources.fileExists(file))
|
||||
return false;
|
||||
|
||||
try {
|
||||
OTMLDocumentPtr doc = OTMLDocument::parse(fileName);
|
||||
OTMLDocumentPtr doc = OTMLDocument::parse(file);
|
||||
for(const OTMLNodePtr& child : doc->children())
|
||||
m_confsMap[child->tag()] = child->value();
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: could not load configurations: ", e.what());
|
||||
logError("could not load configurations: ", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
class Configs
|
||||
{
|
||||
public:
|
||||
bool load(const std::string& fileName);
|
||||
bool load(const std::string& file);
|
||||
bool save();
|
||||
|
||||
void set(const std::string& key, const std::string& value) { m_confsMap[key] = value; }
|
||||
|
52
src/framework/core/logger.cpp
Normal file
52
src/framework/core/logger.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "logger.h"
|
||||
#include "eventdispatcher.h"
|
||||
|
||||
Logger g_logger;
|
||||
|
||||
Logger::Logger() : m_terminated(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Logger::log(LogLevel level, std::string message)
|
||||
{
|
||||
const static std::string logPrefixes[] = { "", "", "WARNING: ", "ERROR: ", "FATAL ERROR: " };
|
||||
|
||||
if(!m_terminated) {
|
||||
message.insert(0, logPrefixes[level]);
|
||||
std::cout << message << std::endl;
|
||||
|
||||
std::size_t now = std::time(NULL);
|
||||
m_logMessages.push_back(LogMessage(level, message, now));
|
||||
|
||||
if(m_onLog)
|
||||
g_dispatcher.addEvent(std::bind(m_onLog, level, message, now));
|
||||
}
|
||||
|
||||
if(level == LogFatal) {
|
||||
m_terminated = true;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::logFunc(LogLevel level, const std::string& message, std::string prettyFunction)
|
||||
{
|
||||
std::stringstream ss;
|
||||
prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('('));
|
||||
if(prettyFunction.find_last_of(' ') != std::string::npos)
|
||||
prettyFunction = prettyFunction.substr(prettyFunction.find_last_of(' ') + 1);
|
||||
|
||||
if(!prettyFunction.empty())
|
||||
ss << "[" << prettyFunction << "] ";
|
||||
|
||||
ss << message;
|
||||
log(level, ss.str());
|
||||
}
|
||||
|
||||
void Logger::fireOldMessages()
|
||||
{
|
||||
if(m_onLog) {
|
||||
for(const LogMessage& logMessage : m_logMessages)
|
||||
g_dispatcher.addEvent(std::bind(m_onLog, logMessage.level, logMessage.message, logMessage.when));
|
||||
}
|
||||
}
|
49
src/framework/core/logger.h
Normal file
49
src/framework/core/logger.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifndef LOGGER_H
|
||||
#define LOGGER_H
|
||||
|
||||
#include "../util/tools.h"
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
struct LogMessage {
|
||||
LogMessage(LogLevel level, const std::string& message, std::size_t when) : level(level), message(message), when(when) { }
|
||||
LogLevel level;
|
||||
std::string message;
|
||||
std::size_t when;
|
||||
};
|
||||
|
||||
class Logger
|
||||
{
|
||||
typedef std::function<void(LogLevel, std::string, std::size_t)> OnLogCallback;
|
||||
|
||||
public:
|
||||
Logger();
|
||||
|
||||
void log(LogLevel level, std::string message);
|
||||
void logFunc(LogLevel level, const std::string& message, std::string prettyFunction);
|
||||
|
||||
void fireOldMessages();
|
||||
void setOnLog(const OnLogCallback& onLog) { m_onLog = onLog; }
|
||||
|
||||
private:
|
||||
std::vector<LogMessage> m_logMessages;
|
||||
OnLogCallback m_onLog;
|
||||
bool m_terminated;
|
||||
};
|
||||
|
||||
extern Logger g_logger;
|
||||
|
||||
// specialized logging
|
||||
#define logDebug(...) g_logger.log(LogDebug, fw::mkstr(__VA_ARGS__))
|
||||
#define logInfo(...) g_logger.log(LogInfo, fw::mkstr(__VA_ARGS__))
|
||||
#define logWarning(...) g_logger.log(LogWarning, fw::mkstr(__VA_ARGS__))
|
||||
#define logError(...) g_logger.log(LogError, fw::mkstr(__VA_ARGS__))
|
||||
#define logFatal(...) g_logger.log(LogFatal, fw::mkstr(__VA_ARGS__))
|
||||
|
||||
#define logTrace() g_logger.logFunc(LogDebug, "", __PRETTY_FUNCTION__)
|
||||
#define logTraceDebug(...) g_logger.logFunc(LogDebug, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
|
||||
#define logTraceInfo(...) g_logger.logFunc(LogInfo, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
|
||||
#define logTraceWarning(...) g_logger.logFunc(LogWarning, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
|
||||
#define logTraceError(...) g_logger.logFunc(LogError, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
|
||||
|
||||
#endif
|
@@ -54,7 +54,7 @@ bool Module::load()
|
||||
logInfo("Loaded module '", m_name, "'");
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: failed to load module '", m_name, "': ", e.what());
|
||||
logError("failed to load module '", m_name, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -38,7 +38,7 @@ bool ModuleManager::discoverModule(const std::string& file)
|
||||
module->discover(moduleNode);
|
||||
m_modules.push_back(module);
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: failed to load module from '", file, "': ", e.what());
|
||||
logError("failed to load module from '", file, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@@ -30,14 +30,14 @@ void ResourceManager::init(const char* argv0)
|
||||
}
|
||||
|
||||
if(!found)
|
||||
logFatal("FATAL ERROR: could not find modules directory");
|
||||
logFatal("could not find modules directory");
|
||||
|
||||
// setup write directory
|
||||
std::string dir = g_platform.getAppUserDir();
|
||||
if(g_resources.setWriteDir(dir))
|
||||
g_resources.addToSearchPath(dir);
|
||||
else
|
||||
logError("ERROR: could not setup write directory");
|
||||
logError("could not setup write directory");
|
||||
}
|
||||
|
||||
void ResourceManager::terminate()
|
||||
|
Reference in New Issue
Block a user