mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 05:53:26 +02:00
new logger
scripts are now more error prone
This commit is contained in:
@@ -26,17 +26,10 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <stdarg.h>
|
||||
|
||||
void Logger::_log(int level, const char *trace, const char *format, ...)
|
||||
void Logger::log(int level, const std::string& text, const char *trace)
|
||||
{
|
||||
va_list args;
|
||||
std::string strace;
|
||||
|
||||
va_start(args, format);
|
||||
std::string text = vformat(format, args);
|
||||
va_end(args);
|
||||
|
||||
if(trace) {
|
||||
strace = trace;
|
||||
strace = strace.substr(0, strace.find_first_of('('));
|
||||
@@ -54,8 +47,6 @@ void Logger::_log(int level, const char *trace, const char *format, ...)
|
||||
if(!strace.empty())
|
||||
std::cout << "[" << strace << "] ";
|
||||
|
||||
static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
|
||||
std::cout << prefixes[level];
|
||||
std::cout << text;
|
||||
|
||||
#ifdef linux
|
||||
@@ -68,3 +59,4 @@ void Logger::_log(int level, const char *trace, const char *format, ...)
|
||||
if(level == LFATAL)
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
@@ -26,33 +26,39 @@
|
||||
#define LOGGER_H
|
||||
|
||||
#include <sstream>
|
||||
#include <boost/format.hpp>
|
||||
|
||||
namespace Logger {
|
||||
class Logger {
|
||||
public:
|
||||
enum ELogLevel {
|
||||
LFATAL = 0,
|
||||
LERROR,
|
||||
LWARNING,
|
||||
LINFO,
|
||||
LDEBUG
|
||||
};
|
||||
|
||||
enum ELogLevel {
|
||||
LFATAL = 0,
|
||||
LERROR,
|
||||
LWARNING,
|
||||
LINFO,
|
||||
LDEBUG
|
||||
static void log(int level, const std::string& text = "", const char *trace = NULL);
|
||||
};
|
||||
|
||||
void _log(int level, const char *trace, const char *format, ...);
|
||||
#define logFatal(a) Logger::log(Logger::LFATAL, a)
|
||||
#define logError(a) Logger::log(Logger::LERROR, a)
|
||||
#define logWarning(a) Logger::log(Logger::LWARNING, a)
|
||||
#define logDebug(a) Logger::log(Logger::LDEBUG, a)
|
||||
#define logInfo(a) Logger::log(Logger::LINFO, a)
|
||||
|
||||
}
|
||||
#define flogFatal(a,b) Logger::log(Logger::LFATAL, (boost::format(a) % b).str())
|
||||
#define flogError(a,b) Logger::log(Logger::LERROR, (boost::format(a) % b).str())
|
||||
#define flogWarning(a,b) Logger::log(Logger::LWARNING, (boost::format(a) % b).str())
|
||||
#define flogDebug(a,b) Logger::log(Logger::LDEBUG, (boost::format(a) % b).str())
|
||||
#define flogInfo(a,b) Logger::log(Logger::LINFO, (boost::format(a) % b).str())
|
||||
|
||||
#define logFatal(...) Logger::_log(Logger::LFATAL, NULL, __VA_ARGS__)
|
||||
#define logError(...) Logger::_log(Logger::LERROR, NULL, __VA_ARGS__)
|
||||
#define logWarning(...) Logger::_log(Logger::LWARNING, NULL, __VA_ARGS__)
|
||||
#define logDebug(...) Logger::_log(Logger::LDEBUG, NULL, __VA_ARGS__)
|
||||
#define logInfo(...) Logger::_log(Logger::LINFO, NULL, __VA_ARGS__)
|
||||
|
||||
#define logTrace() Logger::_log(Logger::LDEBUG, __PRETTY_FUNCTION__, "")
|
||||
#define logTraceFatal(...) Logger::_log(Logger::LFATAL, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#define logTraceError(...) Logger::_log(Logger::LERROR, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#define logTraceWarning(...) Logger::_log(Logger::LWARNING, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#define logTraceDebug(...) Logger::_log(Logger::LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#define logTraceInfo(...) Logger::_log(Logger::LINFO, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||
#define logTrace() Logger::log(Logger::LDEBUG, "", __PRETTY_FUNCTION__)
|
||||
#define logTraceFatal(a) Logger::log(Logger::LFATAL, a, __PRETTY_FUNCTION__)
|
||||
#define logTraceError(a) Logger::log(Logger::LERROR, a, __PRETTY_FUNCTION__)
|
||||
#define logTraceWarning(a) Logger::log(Logger::LWARNING, a, __PRETTY_FUNCTION__)
|
||||
#define logTraceDebug(a) Logger::log(Logger::LDEBUG, a, __PRETTY_FUNCTION__)
|
||||
#define logTraceInfo(a) Logger::log(Logger::LINFO, a, __PRETTY_FUNCTION__)
|
||||
|
||||
struct Dump {
|
||||
public:
|
||||
@@ -65,4 +71,4 @@ private:
|
||||
|
||||
#define dump Dump()
|
||||
|
||||
#endif
|
||||
#endif // LOGGER_H
|
||||
|
@@ -23,30 +23,3 @@
|
||||
|
||||
#include <util/util.h>
|
||||
#include <cstdio>
|
||||
|
||||
std::string vformat(const char *format, va_list args)
|
||||
{
|
||||
if(!format)
|
||||
return "";
|
||||
int result = -1, length = 256;
|
||||
char *buffer = 0;
|
||||
while(result == -1) {
|
||||
if(buffer)
|
||||
delete [] buffer;
|
||||
buffer = new char [length + 1];
|
||||
result = vsnprintf(buffer, length, format, args);
|
||||
length *= 2;
|
||||
}
|
||||
std::string s(buffer);
|
||||
delete [] buffer;
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string format(const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
std::string s = vformat(format, args);
|
||||
va_end(args);
|
||||
return s;
|
||||
}
|
||||
|
@@ -26,14 +26,11 @@
|
||||
#define UTIL_H
|
||||
|
||||
#include <util/logger.h>
|
||||
#include <stdarg.h>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/format.hpp>
|
||||
|
||||
/// Formatting like printf for std::string, va_list input version
|
||||
std::string vformat(const char *format, va_list args);
|
||||
|
||||
/// Formatting like printf for std::string
|
||||
std::string format(const char *format, ...);
|
||||
/// Easy/fast writting formater
|
||||
#define f(a, b) (boost::format(a) % b).str()
|
||||
|
||||
/// Convert any data type through boost::lexical_cast
|
||||
template<class R, class T>
|
||||
@@ -43,9 +40,9 @@ R convertType(T t)
|
||||
try {
|
||||
ret = boost::lexical_cast<R>(t);
|
||||
} catch(boost::bad_lexical_cast bad) {
|
||||
logError("Error converting type: %s", bad.what());
|
||||
flogError("Error converting type: %s", bad.what());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // UTIL_H
|
||||
|
Reference in New Issue
Block a user