C/C++ 打印信息控制,包括日志
2015-05-19 17:04
344 查看
#ifndef _MACRO_H_ #define _MACRO_H_ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <time.h> #include <string.h> #include <unistd.h> #define MAX_LOG_SIZE (5*1024*1024) // 5M extern FILE *plog; extern const char *log_path; int InitLogFile(const char *path); void CloseLog(); #define PRF(format, ...) do\ {\ fflush(stdout);\ const char *find_ = rindex(__FILE__, '/');\ if (NULL != find_)\ find_++;\ else\ find_ = __FILE__;\ time_t t = time(NULL);\ struct tm *time_info = localtime(&t);\ char buf_t[20];\ memset(buf_t, 0, sizeof(buf_t));\ strftime(buf_t, sizeof(buf_t), "%Y-%m-%d %H:%M:%S", time_info);\ fprintf(stdout, "DEBUG | %s|%-20.20s: %-5d| %-20.20s| "format"", buf_t,\ find_,\ __LINE__,\ __FUNCTION__,\ ##__VA_ARGS__);\ }while(0) #define LOG(format, ...) do\ {\ ASSERT(plog != NULL);\ ASSERT(log_path != NULL);\ time_t t = time(NULL);\ struct tm *time_info = localtime(&t);\ char buf_t[20];\ memset(buf_t, 0, sizeof(buf_t));\ strftime(buf_t, sizeof(buf_t), "%Y-%m-%d %H:%M:%S", time_info);\ if (ftell(plog) > MAX_LOG_SIZE)\ {\ fclose(plog);\ fclose(fopen(log_path, "w+"));\ plog = fopen(log_path, "a+");\ }\ const char *find_ = rindex(__FILE__, '/');\ if (NULL != find_)\ find_++;\ else\ find_ = __FILE__;\ fprintf(plog, "LOG | %s|%-20.20s: %-5d| %-20.20s| "format"", buf_t,\ find_,\ __LINE__,\ __FUNCTION__,\ ##__VA_ARGS__);\ }while(0) #ifdef _DEBUG_STDOUT_ #define DBG(format, ...) do\ {\ PRF(""format"", ##__VA_ARGS__);\ }while(0) #define ERR(format, ...) do\ {\ char err[50];bzero(err, sizeof(err));\ strncpy(err, strerror(errno), sizeof(err));\ PRF("%s, "format"", err, ##__VA_ARGS__);\ }while(0) #elif defined _DEBUG_LOG_ #define DBG(format, ...) do\ {\ LOG(""format"", ##__VA_ARGS__);\ }while(0) #define ERR(format, ...) do\ {\ char err[50];bzero(err, sizeof(err));\ strncpy(err, strerror(errno), sizeof(err));\ LOG("%s, "format"", err, ##__VA_ARGS__);\ }while(0) #elif defined _DEBUG_STDOUT_LOG_ #define DBG(format, ...) do\ {\ PRF(""format"", ##__VA_ARGS__);\ LOG(""format"", ##__VA_ARGS__);\ }while(0) #define ERR(format, ...) do\ {\ char err[50];bzero(err, sizeof(err));\ strncpy(err, strerror(errno), sizeof(err));\ PRF("%s, "format"", err, ##__VA_ARGS__);\ LOG("%s, "format"", err, ##__VA_ARGS__);\ }while(0) #else #define DBG(format, ...) #define ERR(format, ...) #endif #define ASSERT(expr) do\ {\ if (!(expr))\ {\ fflush(stderr);\ const char *find_ = rindex(__FILE__, '/');\ if (NULL != find_)\ find_++;\ else\ find_ = __FILE__;\ fprintf(stderr, "%-20.20s: %-5d, ASSERT %s failed\n",\ find_,\ __LINE__,\ #expr);\ abort();\ }\ }while(0) #define FATAL(format, ...) do\ {\ fflush(stderr);\ const char *find_ = rindex(__FILE__, '/');\ if (NULL != find_)\ find_++;\ else\ find_ = __FILE__;\ fprintf(stderr, "FATAL, %-20.20s: %-5d: error: %-20.20s, "format"",\ find_,\ __LINE__,\ strerror(errno),\ ##__VA_ARGS__);\ abort();\ }while(0) #define PROTO_DEBUG #ifdef PROTO_DEBUG #define CALL(func) do\ {\ (func);\ }while(0) #else #define CALL(func) #endif typedef unsigned int u_int32_t; typedef unsigned long ul_int32_t; typedef unsigned long long ull_int64_t; #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) #define SUCCESS 0 #define FAILURE -1 #endif
#include "macro.h" FILE *plog = NULL; const char *log_path = NULL; int InitLogFile(const char *path) { int ret = 0; log_path = path; plog = fopen(path, "a+"); if (!plog) ret = -1; DBG("init log file completed.\n"); return ret; } void CloseLog() { fclose(plog); DBG("close log file completed.\n"); }
相关文章推荐
- Android JNI打印c\c++日志信息
- 2015年C++第三周,任务一:打印个人信息,学生,老师,在职教师
- 在C/C++程序里打印调用栈信息
- ROS+SLAM学习日志(4)获取控制与位移信息
- c++ 打印堆栈信息
- --local function cclog(...)的变种(打印日志信息)
- python 在控制台中用不同颜色打印信息
- linux c 及 c++打印调用者函数caller function的方法,包括arm c平台
- java Exception 出错的栈信息打印到日志中 打印堆栈信息
- 关于android studio log日志打印信息不全的原因以及解决方法
- (转)log4j(四)——如何控制不同风格的日志信息的输出?
- 宏控制打印信息输出
- 我使用过的Linux命令之dmesg - 查看开机信息/打印或控制内核环形缓冲区
- 真机调试NSLog不打印日志信息
- log4j 将控制台的完整异常信息记录到日志文件
- C++控制打印格式
- 检查 android.mk条件如果执行,在android.mk中打印日志信息
- 日志管理【想要在控制台打印出更加详细的信息吗?】
- tomcat打印详细日志信息
- 自由控制是否打印日志(Log)