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;
}
相关推荐
paterWang2 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
东方佑2 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
mit6.8242 小时前
[实现Rpc] 通信类抽象层 | function | using | 解耦合设计思想
c++·网络协议·rpc
我真不会起名字啊2 小时前
“深入浅出”系列之杂谈篇:(3)Qt5和Qt6该学哪个?
开发语言·qt
laimaxgg2 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
水瓶丫头站住3 小时前
Qt的QStackedWidget样式设置
开发语言·qt
小钊(求职中)4 小时前
Java开发实习面试笔试题(含答案)
java·开发语言·spring boot·spring·面试·tomcat·maven
ox00805 小时前
C++ 设计模式-命令模式
c++·设计模式·命令模式
慕诗客5 小时前
QT基于Gstreamer采集的简单示例
开发语言·qt
Blasit5 小时前
C++ Qt建立一个HTTP服务器
服务器·开发语言·c++·qt·http