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;
}
相关推荐
wqfhenanxc7 分钟前
Mixing C++ and Rust for Fun and Profit 阅读笔记
c++·笔记·rust
R-G-B10 分钟前
【MFC】 VS2022打开低版本的MFC,双击.rc文件,DIalog加载失败,页面弹窗fatal error RC***:cannot open*****
c++·mfc·vs打开较早版本mfc·双击.rc文件·dialog加载失败·fatal error rc·cannot open
敲上瘾12 分钟前
基于Tcp协议的应用层协议定制
linux·运维·服务器·网络·c++·网络协议·tcp/ip
weixin_3077791313 分钟前
PySpark实现ABC_manage_channel逻辑
开发语言·python·spark
莹莹学编程—成长记1 小时前
string的模拟实现
服务器·c++·算法
??? Meggie1 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
酷爱码3 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼3 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!4 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ5 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d