jklog/JkLog.cpp

82 lines
2.2 KiB
C++

#include <iostream>
#include "JkLog.h"
std::string getFileName(const std::string& s) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
if (i != std::string::npos) {
return(s.substr(i+1, s.length() - i));
}
return("");
}
JkLog::JkLog() {}
JkLog::~JkLog() {}
JkLog* JkLog::instance;
JkLog *JkLog::getInstance() {
if (0 == JkLog::instance) {
JkLog::instance = new JkLog();
}
return JkLog::instance;
}
void JkLog::setInstance(JkLog* instance) {
JkLog::instance = instance;
}
void JkLog::error(const std::string& file, int line, const std::string &message) {
log(JK_ERROR, file, line, message);
}
void JkLog::warn(const std::string& file, int line, const std::string &message) {
log(JK_WARN, file, line, message);
}
void JkLog::info(const std::string& file, int line, const std::string &message) {
log(JK_INFO, file, line, message);
}
void JkLog::debug(const std::string& file, int line, const std::string &message) {
log(JK_DEBUG, file, line, message);
}
void JkLog::verbose(const std::string& file, int line, const std::string &message) {
log(JK_VERBOSE, file, line, message);
}
void JkLog::log(int logLevel, const std::string& file, int line, const std::string &message) {
JkLog::getInstance()->log_impl(logLevel, file, line, message);
}
void JkLog::log_impl(int logLevel, const std::string& file, int line, const std::string &message) const {
std::string fileOnly = getFileName(file);
switch (logLevel) {
case JK_ERROR:
std::cerr << "[Error] " << fileOnly << ":" << line << " " << message << std::endl;
break;
case JK_WARN:
std::cout << "[Warn] " << fileOnly << ":" << line << " " << message << std::endl;
break;
case JK_INFO:
std::cout << "[Info] " << fileOnly << ":" << line << " " << message << std::endl;
break;
case JK_DEBUG:
std::cout << "[Debug] " << fileOnly << ":" << line << " " << message << std::endl;
break;
case JK_VERBOSE:
std::cout << "[Verbose] " << fileOnly << ":" << line << " " << message << std::endl;
break;
default:
break;
}
}