chore(log): add milliseconds precision for log messages

This commit is contained in:
VDm 2024-02-21 00:52:08 +04:00
parent fb4b96af7c
commit 63476cf61e

View file

@ -9,12 +9,14 @@
#if defined(WHOA_SYSTEM_LINUX) #if defined(WHOA_SYSTEM_LINUX)
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <limits.h> #include <linux/limits.h>
#endif #endif
#if defined(WHOA_SYSTEM_MAC) #if defined(WHOA_SYSTEM_MAC)
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <mach/mach_time.h> #include <mach/mach_time.h>
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
@ -27,6 +29,22 @@
#define STORM_LOG_FLUSH_POINT 0xC000 #define STORM_LOG_FLUSH_POINT 0xC000
#if defined(WHOA_SYSTEM_WIN)
typedef SYSTEMTIME SLOGTIME;
#else
typedef struct _SLOGTIME {
uint16_t wYear;
uint16_t wMonth;
uint16_t wDayOfWeek;
uint16_t wDay;
uint16_t wHour;
uint16_t wMinute;
uint16_t wSecond;
uint16_t wMilliseconds;
} SLOGTIME;
#endif
typedef struct _LOG { typedef struct _LOG {
HSLOG log; HSLOG log;
_LOG* next; _LOG* next;
@ -41,10 +59,6 @@ typedef struct _LOG {
} LOG; } LOG;
static uint64_t lasttime = 0;
static size_t timestrlen = 0;
static char timestr[64] = { '\0' };
static SCritSect* s_critsect[STORM_LOG_MAX_CHANNELS] = { nullptr }; static SCritSect* s_critsect[STORM_LOG_MAX_CHANNELS] = { nullptr };
static SCritSect* s_defaultdir_critsect = nullptr; static SCritSect* s_defaultdir_critsect = nullptr;
@ -306,7 +320,7 @@ static bool OpenLogFile(const char* filename, FILE** file, uint32_t flags) {
char newfilename[STORM_MAX_PATH]; char newfilename[STORM_MAX_PATH];
PrependDefaultDir(newfilename, STORM_MAX_PATH, filename); PrependDefaultDir(newfilename, STORM_MAX_PATH, filename);
CreateFileDirectory(newfilename); CreateFileDirectory(newfilename);
*file = fopen(newfilename, (flags & SLOG_FLAG_APPEND) ? "a" : "w"); *file = fopen(newfilename, (flags & SLOG_FLAG_APPEND) ? "ab" : "wb");
return (*file != nullptr); return (*file != nullptr);
} }
return false; return false;
@ -325,15 +339,18 @@ static void OutputIndent(LOG* logptr) {
} }
static void OutputReturn(LOG* logptr) { static void OutputReturn(LOG* logptr) {
// strcpy(&logptr->buffer[logptr->bufferused], "\r\n"); #if defined(WHOA_SYSTEM_WIN)
// logptr->bufferused += 2; logptr->buffer[logptr->bufferused++] = '\r';
#endif
// No need to write "\r\n" with fopen() in text mode logptr->buffer[logptr->bufferused++] = '\n';
logptr->buffer[logptr->bufferused] = '\n'; logptr->buffer[logptr->bufferused] = '\0';
logptr->bufferused++;
} }
static void OutputTime(LOG* logptr, bool show) { static void OutputTime(LOG* logptr, bool show) {
static uint64_t lasttime = 0;
static size_t timestrlen = 0;
static char timestr[64] = { '\0' };
if (logptr->timeStamp == 0) { if (logptr->timeStamp == 0) {
return; return;
} }
@ -358,18 +375,35 @@ static void OutputTime(LOG* logptr, bool show) {
if (ticks != lasttime) { if (ticks != lasttime) {
lasttime = ticks; lasttime = ticks;
SLOGTIME systime;
#if defined(WHOA_SYSTEM_WIN)
GetLocalTime(&systime);
#else
time_t t = time(nullptr); time_t t = time(nullptr);
struct tm* ts = localtime(&t); struct tm* ts = localtime(&t);
// No milliseconds for now systime.wYear = ts->tm_year + 1900;
systime.wMonth = ts->tm_mon + 1;
systime.wDayOfWeek = ts->tm_wday;
systime.wDay = ts->tm_mday;
systime.wHour = ts->tm_hour;
systime.wMinute = ts->tm_min;
systime.wSecond = ts->tm_sec;
struct timeval tv = { 0 };
gettimeofday(&tv, 0);
systime.wMilliseconds = tv.tv_usec / 1000;
#endif
timestrlen = SStrPrintf(timestr, timestrlen = SStrPrintf(timestr,
sizeof(timestr), sizeof(timestr),
"%d/%d %02d:%02d:%02d.%03d ", "%u/%u %02u:%02u:%02u.%03u ",
ts->tm_mon + 1, systime.wMonth,
ts->tm_mday, systime.wDay,
ts->tm_hour, systime.wHour,
ts->tm_min, systime.wMinute,
ts->tm_sec, systime.wSecond,
0); systime.wMilliseconds);
} }
if (show) { if (show) {