LAMPCAE/src/Common/DebugLogger.cpp

128 lines
2.9 KiB
C++
Raw Normal View History

#include "DebugLogger.h"
2024-07-23 02:25:24 +00:00
#include <boost/stacktrace.hpp>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
2024-07-23 02:25:24 +00:00
#include <windows.h>
#include <dbghelp.h>
#include <stdio.h>
#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 {
2024-07-23 02:25:24 +00:00
void function_to_trace() {
std::cout << "Stack trace:" << std::endl;
std::cout << boost::stacktrace::stacktrace() << std::endl;
}
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