c++打印log宏定义

c语言demo,简单日志实现:

仿android 自动添加系统时间,进程号,线程号。 记录,以备不时之需.

#define LOG_D(...) do {printf("[%s %d %ld D ]",getCurtimeMsStr().c_str(),getpid(),gettid());printf(VA_ARGS);printf("\n");} while(0)// 定义LOGD类型

cpp 复制代码
#include <sys/time.h>
#include <time.h>

#include <string>
#include<string.h>

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//    struct timeval {
//        time_t      tv_sec;     /* seconds */
//        suseconds_t tv_usec;    /* microseconds */
//    };

// 如果报错 gettid(), 加上下面这个定义
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)

inline long long getCurtTimeMs(void)
{
   struct timeval tv;
   gettimeofday(&tv,NULL);
   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}


time_t getCurSecond(void){
    // 返回秒数,精度低于gettimeofday()
    return time(NULL);
}


std::string getCurtimeStr(void){
    struct timeval tv;
    gettimeofday(&tv,NULL);
    // gmtime不带时区信息,将time_t 转换为struct tm.      localtime作用相同,但是待带时区信息。 两者都不是线程安全的。
    // struct tm *st_tm = gmtime(&tv.tv_sec);
    // struct tm *st_tm = (struct tm*)calloc(1,sizeof(struct tm));
    struct tm st_tm;
    localtime_r(&tv.tv_sec,&st_tm);

    char timebuf[128]={0};
    strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S",&st_tm);
    return std::string(timebuf);
}

std::string getCurtimeMsStr(void){
    struct timeval tv;
    gettimeofday(&tv,NULL);
    // gmtime不带时区信息,将time_t 转换为struct tm.      localtime作用相同,但是待带时区信息。 两者都不是线程安全的。
    // struct tm *st_tm = gmtime(&tv.tv_sec);
    // struct tm *st_tm = (struct tm*)calloc(1,sizeof(struct tm));
    struct tm st_tm;
    localtime_r(&tv.tv_sec,&st_tm);

    char timebuf[128]={0};
    strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S",&st_tm);
    snprintf(timebuf+strlen(timebuf),sizeof(timebuf),".%03d",(int)(tv.tv_usec%1000));

    return std::string(timebuf);
}


#if 1
#define LOG_D(...)  do {printf("[%s %d %ld D ]",getCurtimeMsStr().c_str(),getpid(),gettid());printf(__VA_ARGS__);printf("\n");} while(0)// 定义LOGD类型
#define LOG_I(...)  do {printf("[%s %d %ld I ]",getCurtimeMsStr().c_str(),getpid(),gettid());printf(__VA_ARGS__);printf("\n");} while(0) // 定义LOGI类型
#define LOG_W(...)  do {printf("[%s %d %ld W ]",getCurtimeMsStr().c_str(),getpid(),gettid());printf(__VA_ARGS__);printf("\n");} while(0) // 定义LOGW类型
#define LOG_E(...)  do {printf("[%s %d %ld E ]",getCurtimeMsStr().c_str(),getpid(),gettid());printf(__VA_ARGS__);printf("\n");} while(0) // 定义LOGE类型
#define LOG_F(...)  do {printf("[%s %d %ld F ]",getCurtimeMsStr().c_str(),getpid(),gettid());printf(__VA_ARGS__);printf("\n");} while(0) // 定义LOGF类型
#else
#define LOG_D(...)
#define LOG_I(...)
#define LOG_W(...)
#define LOG_E(...)
#define LOG_F(...)
#endif


#define LOG_RED(...)  do {printf("\033[31m"); printf(__VA_ARGS__);printf("\n \033[0m"); } while(0)
#define LOG_GREEN(...)  do {printf("\033[32m"); printf(__VA_ARGS__);printf("\n \033[0m"); } while(0)


int main(int argc, const char*argv[]){
    printf("[%s]\n",getCurtimeStr().c_str());
    printf("[%s]\n",getCurtimeMsStr().c_str());
    LOG_D("%s",getCurtimeMsStr().c_str());
    return 0;
}
相关推荐
Fairy_sevenseven6 分钟前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
_GR12 分钟前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
蜡笔小新星14 分钟前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
凯子坚持 c14 分钟前
C语言复习概要(三)
c语言·开发语言
无限大.25 分钟前
c语言200例 067
java·c语言·开发语言
余炜yw27 分钟前
【Java序列化器】Java 中常用序列化器的探索与实践
java·开发语言
篝火悟者28 分钟前
问题-python-运行报错-SyntaxError: Non-UTF-8 code starting with ‘\xd5‘ in file 汉字编码问题
开发语言·python
Death20031 分钟前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
六点半88832 分钟前
【C++】速通涉及 “vector” 的经典OJ编程题
开发语言·c++·算法·青少年编程·推荐算法
惜.己32 分钟前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5