使用方法是LOG_INFO("my string");
log开头自动包含的线程号 时间 文件 行号 函数名的打印。
同时还包含是否写文件日志的功能
使用效果图
头文件 log.h代码实现
#ifndef __BASE_LOG_H_
#define __BASE_LOG_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)
#define MAX_FILE_PATH_SIZE 256
typedef enum __LOG_LEVEL {
LOG_LEVEL_ERROR = 0,
LOG_LEVEL_WARNING,
LOG_LEVEL_INFO,
LOG_LEVEL_DETAIL,
LOG_LEVEL_DEBUG1,
LOG_LEVEL_DEBUG2,
} LOG_LEVEL;
static LOG_LEVEL log_level = LOG_LEVEL_INFO;
static bool log_write_file_flag = false;
static char log_file_path[MAX_FILE_PATH_SIZE] = "log";
void SetLogLevel(LOG_LEVEL level, bool write_file = false, const char* file_path = "");
void LevelCast2String(LOG_LEVEL level, char* result);
void log_printf_v(LOG_LEVEL level, const char* file_name, int line, const char* function, const char* fmt, ...);
#define LOG_ERROR(fmt, ...) log_printf_v(LOG_LEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define LOG_WARNING(fmt, ...) log_printf_v(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define LOG_INFO(fmt, ...) log_printf_v(LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define LOG_DETAIL(fmt, ...) log_printf_v(LOG_LEVEL_DETAIL, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define LOG_DEBUG1(fmt, ...) log_printf_v(LOG_LEVEL_DEBUG1, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define LOG_DEBUG2(fmt, ...) log_printf_v(LOG_LEVEL_DEBUG2, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif
log.cpp代码实现
#include "log.h"
void SetLogLevel(LOG_LEVEL level, bool write_file_flag, const char* file_path) {
log_level = level;
log_write_file_flag = write_file_flag;
strncpy(log_file_path, file_path, MAX_FILE_PATH_SIZE - 1);
if (log_write_file_flag) {
FILE* fp = fopen(log_file_path, "wb+");
if (fp) {
fclose(fp);
} else {
LOG_ERROR("fopen file %s fail", log_file_path);
log_write_file_flag = false;
}
}
}
void LevelCast2String(LOG_LEVEL level, char* result) {
if (result){
switch(level) {
case LOG_LEVEL_ERROR:
strncpy(result, "ERROR", 5);
break;
case LOG_LEVEL_WARNING:
strncpy(result, "WARNING", 7);
break;
case LOG_LEVEL_INFO:
strncpy(result, "INFO", 4);
break;
case LOG_LEVEL_DETAIL:
strncpy(result, "DETAIL", 6);
break;
case LOG_LEVEL_DEBUG1:
strncpy(result, "DEBUG1", 6);
break;
case LOG_LEVEL_DEBUG2:
strncpy(result, "DEBUG2", 6);
break;
default:
strncpy(result, "ERROR", 5);
break;
}
}
}
void log_printf_v(LOG_LEVEL level, const char* file_name, int line, const char* function, const char* fmt, ...) {
if (level > log_level) {
return;
}
char buf[1024];
char content[512];
char level_str[8];
memset(buf, 0, sizeof(buf));
memset(content, 0, sizeof(content));
memset(level_str, 0, sizeof(level_str));
LevelCast2String(level, level_str);
va_list args;
va_start(args, fmt);
vsnprintf(content, sizeof(content) - 1, fmt, args);
struct timeval tv;
gettimeofday(&tv, NULL);
sprintf(buf, "[%5ld] %ld:%3ld ###%s### %s[%d]:%s %s\n", gettid(), tv.tv_sec, tv.tv_usec / 1000, level_str, file_name, line, function, content);
printf("%s", buf);
if (log_write_file_flag) {
FILE* fp = fopen(log_file_path, "ab+");
if (fp) {
fwrite(buf, strlen(buf), 1, fp);
fclose(fp);
}
}
va_end(args);
}