LAMPCAE/src/Common/DebugLogger.cpp

112 lines
2.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "DebugLogger.h"
#include <cstdio>
#include <cstring>
#include <ctime>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define filename(x) strrchr(x, '\\') ? strrchr(x, '\\') + 1 : x
#else
#define filename(x) strrchr(x, '/') ? strrchr(x, '/') + 1 : x
#endif
namespace Common {
void DebugLogger::info(const char* file, int line, const char* format, ...)
{
va_list args;
va_start(args, format);
output(file, line, format, 0, args);
va_end(args);
}
void DebugLogger::warning(const char* file, int line, const char* format, ...)
{
va_list args;
va_start(args, format);
output(file, line, format, 1, args);
va_end(args);
}
void DebugLogger::error(const char* file, int line, const char* format, ...)
{
va_list args;
va_start(args, format);
output(file, line, format, 2, args);
va_end(args);
}
char* DebugLogger::currentTime()
{
time_t rawtime;
struct tm* timeS;
char* buffer = new char[80];
time(&rawtime);
timeS = localtime(&rawtime);
sprintf(buffer, "%d-%s%d-%s%d %s%d:%s%d:%s%d", timeS->tm_year + 1900,
timeS->tm_mon < 10 ? "0" : "", timeS->tm_mon, timeS->tm_mday < 10 ? "0" : "",
timeS->tm_mday, timeS->tm_hour < 10 ? "0" : "", timeS->tm_hour,
timeS->tm_min < 10 ? "0" : "", timeS->tm_min, timeS->tm_sec < 10 ? "0" : "",
timeS->tm_sec);
return buffer;
}
/**
* 控制台显示控制格式ESC[{attr1};{attr2};…;{attrn}m
* 显示方式:
* 0默认值
* 1高亮显示顺便加粗不确定
* 2暗淡
* 22非粗体不确定
* 4下划线
* 5闪烁但是我没看出有什么效果。。
* 25非闪烁
* 7反显我也没看出效果
* 27非反显
* 8隐藏
*
* 字体颜色:
* 30黑色
* 31红色
* 32绿色
* 33黄色
* 34蓝色
* 35品红
* 36青色
* 37白色
* 背景色:
* 40黑色
* 41红色
* 42绿色
* 43黄色
* 44蓝色
* 45品红
* 46青色
* 47白色
*/
void DebugLogger::output(const char* file, int line, const char* format, int level,
va_list args)
{
char type[15] = "";
switch(level) {
case 1:
printf("\033[33m"); // 红色
strcpy(type, "WARNING");
break;
case 2:
printf("\033[31m"); // 黄色
strcpy(type, "ERROR");
break;
default:
strcpy(type, "INFO");
break;
}
printf("[%s][%s:%d][%s]: ", currentTime(), filename(file), line, type);
vprintf(format, args);
printf("\033[0m");
}
} // namespace Common