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类型


#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;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值