0
点赞
收藏
分享

微信扫一扫

Redis冲冲冲——事务支持,AOF和RDB持久化

兮城 03-01 16:30 阅读 2

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;
}
举报

相关推荐

0 条评论