Skip to content

Commit

Permalink
REQ032.005 Modify:zhouxin Desc:增加本地us函数时间戳接口
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhxyy committed May 28, 2021
1 parent 421a352 commit 64b5883
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
23 changes: 19 additions & 4 deletions lagan.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ static char gLevelCh[] = {'O', 'D', 'I', 'W', 'E'};

static LaganPrintFunc gOutput = NULL;
static LaganGetTimeFunc gGetTime = NULL;
static LaganGetLocalTimeFunc gGetLocalTime = NULL;
static bool gIsLoad = false;

// LaganLoad 模块载入
void LaganLoad(LaganPrintFunc print, LaganGetTimeFunc getTime) {
// getTime是读取北京时间,getLocalTime是读取本地时间,进度是us
// 如果不需要使用哪个时间,就将其设置为NULL.如果两个都有效,则使用的是北京时间
void LaganLoad(LaganPrintFunc print, LaganGetTimeFunc getTime, LaganGetLocalTimeFunc getLocalTime) {
if (gIsLoad) {
return;
}

gOutput = print;
gGetTime = getTime;
if (gGetTime == NULL) {
gGetLocalTime = getLocalTime;
}
gIsLoad = true;
}

Expand All @@ -50,9 +56,18 @@ void LaganPrint(char* tag, LaganLevel level, char *format, ...) {
char buf[LAGAN_RECORD_MAX_SIZE_DEFAULT] = {0};

// 前缀
LaganTime time = gGetTime();
sprintf(buf, "%02d/%02d/%02d %02d:%02d:%02d.%06d %c/%s ", time.Year, time.Month, time.Day, time.Hour, time.Minute,
time.Second, time.Us, gLevelCh[level], tag);
if (gGetTime != NULL) {
LaganTime time = gGetTime();
sprintf(buf, "%02d/%02d/%02d %02d:%02d:%02d.%06d %c/%s ", time.Year, time.Month, time.Day, time.Hour, time.Minute,
time.Second, time.Us, gLevelCh[level], tag);
} else {
uint64_t us = gGetLocalTime();
int second = (int)(us / 1000000);
us = us % 1000000;
int ms = (int)(us / 1000);
us = us % 1000;
sprintf(buf, "%06d/%03d/%03d %c/%s ", second, ms, (int)us, gLevelCh[level], tag);
}
gOutput((uint8_t*)buf, (int)strlen(buf));

// 正文
Expand Down
7 changes: 6 additions & 1 deletion lagan.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ typedef void (*LaganPrintFunc)(uint8_t* bytes, int size);
// LaganTime 读取时间
typedef LaganTime (*LaganGetTimeFunc)(void);

// LaganGetLocalTimeFunc 读取本地时间.返回值是us级精度的本地时间
typedef uint64_t (*LaganGetLocalTimeFunc)(void);

// LaganLoad 模块载入
void LaganLoad(LaganPrintFunc print, LaganGetTimeFunc getTime);
// getTime是读取北京时间,getLocalTime是读取本地时间,进度是us
// 如果不需要使用哪个时间,就将其设置为NULL.如果两个都有效,则使用的是北京时间
void LaganLoad(LaganPrintFunc print, LaganGetTimeFunc getTime, LaganGetLocalTimeFunc getLocalTime);

// LaganSetFilterLevel 设置过滤日志等级
void LaganSetFilterLevel(LaganLevel level);
Expand Down
10 changes: 9 additions & 1 deletion test/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

extern "C" {
#include "lagan.h"
#include <string.h>
}

static void print(uint8_t* bytes, int size);
static LaganTime getTime(void);
static uint64_t getLocalTime(void);

static void case1(void);
static void case2(void);
Expand All @@ -15,7 +17,8 @@ static void case3(void);
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);

LaganLoad(print, getTime);
// LaganLoad(print, getTime, NULL);
LaganLoad(print, NULL, getLocalTime);

case1();
case2();
Expand Down Expand Up @@ -43,6 +46,11 @@ static LaganTime getTime(void) {
return time;
}

static uint64_t getLocalTime(void) {
QTime now = QTime::currentTime();
return (uint64_t)(now.msecsSinceStartOfDay()) * 1000;
}

static void case1(void) {
LD("case1", "debug test print:%d", 1);
LI("case1", "info test print:%d", 2);
Expand Down

0 comments on commit 64b5883

Please sign in to comment.