加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
log.hpp 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
hairrrrr 提交于 2023-09-18 17:19 . 2023-9-18
#pragma once
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdarg>
#define DEBUG 0
#define INFO 1
#define WARNING 2
#define ERROR 3
#define FATAL 4
#define DEBUG_MODE 1
#define RELEASE_MODE 0
#define LOG_FILE "./.log"
#define LOGFILE_SHOW 1
#define LOG(level, debug_mode, error_number)\
log(__LINE__, __FILE__, #level, debug_mode, error_number)
#define LOGMESSAGE(level, debug_mode, format, args...)\
logMessage(__LINE__, __FILE__, #level, debug_mode, format, ##args)
static inline
void log(int line, const std::string file, const std::string level,
int debugMode, int error_number)
{
if( debugMode == RELEASE_MODE ) return;
std::string str;
str = "[" + level + "] " + "{" + file + ":"
+ std::to_string(line) + "} " + "(" + strerror(error_number) + ")";
std::cout << str << "\n";
#ifdef LOGFILE_SHOW
FILE* fp = fopen(LOG_FILE, "a");
fprintf(fp, "%s\n", str.c_str());
#endif
}
static inline
void logMessage(int line, const char* file, const char* level,
int debugMode, const char* format, ...)
{
if( debugMode == RELEASE_MODE ) return;
char stdBuffer[1024] = {0};
snprintf(stdBuffer, sizeof stdBuffer, "[%s] {%d:%s}", level, line, file);
char logBuffer[1024];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, sizeof logBuffer, format, args);
va_end(args);
#ifdef LOGFILE_SHOW
FILE* fp = fopen(LOG_FILE, "a");
fprintf(fp, "%s %s\n", stdBuffer, logBuffer);
#endif
printf("%s %s\n", stdBuffer, logBuffer);
}
inline
void log_quit(int error_code, int debug_mode, int error_number, int exit_number)
{
LOG(error_code, debug_mode, error_number);
exit(exit_number);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化