From 64b58832a1596f88345c42e6b1859a8f9b90a4c8 Mon Sep 17 00:00:00 2001 From: jdhxyy Date: Fri, 28 May 2021 08:55:44 +0800 Subject: [PATCH] =?UTF-8?q?REQ032.005=20Modify:zhouxin=20Desc:=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=9C=AC=E5=9C=B0us=E5=87=BD=E6=95=B0=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lagan.c | 23 +++++++++++++++++++---- lagan.h | 7 ++++++- test/qt/main.cpp | 10 +++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lagan.c b/lagan.c index 5732fa0..202f6e4 100644 --- a/lagan.c +++ b/lagan.c @@ -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; } @@ -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)); // 正文 diff --git a/lagan.h b/lagan.h index 02034e1..96a2049 100644 --- a/lagan.h +++ b/lagan.h @@ -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); diff --git a/test/qt/main.cpp b/test/qt/main.cpp index 0e0d481..e3bd901 100644 --- a/test/qt/main.cpp +++ b/test/qt/main.cpp @@ -3,10 +3,12 @@ extern "C" { #include "lagan.h" + #include } 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); @@ -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(); @@ -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);