From 476919181be2c39b7236c656d701513864a809c4 Mon Sep 17 00:00:00 2001 From: Sandro Kalatozishvili Date: Sun, 27 Aug 2023 00:56:04 +0400 Subject: [PATCH] Updated slog version function --- README.md | 10 +++------- example/example.c | 15 ++++++--------- src/slog.c | 46 +++++++++++++++++++++++++++++++--------------- src/slog.h | 5 +++-- 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 954f176..fa05464 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ nToScreen | uint8_t | 1 (enabled) | Enable or disable screen nUseHeap | uint8_t | 0 (disabled) | Use dynamic allocation for output. nToFile | uint8_t | 0 (disabled) | Enable or disable file logging. nIndent | uint8_t | 0 (disabled) | Enable or disable indentations. +nRotate | uint8_t | 1 (enabled) | Create new log file for each day. nFlush | uint8_t | 0 (disabled) | Flush output file after log. nFlags | uint16_t | 0 (no logs) | Allowed log level flags. @@ -362,16 +363,11 @@ int main() If you return `-1` from the callback function, the log will no longer be printed to the screen or written to a file by `slog`. If you return `0`, the log will not be written to the screen but still to a file (if nToFile > 1). If you return `1` the logger will normally continue its routine. ### Version -Get `slog` version with the function `slog_version(3)` -- First argument is destination buffer. -- Second argument is destination buffer size. -- Third argument is short/full version flag. +Get `slog` version with the function `slog_version()`. The argument `uint8_t nShort` is a flag to get short or full string of the version (1 short, 0 full). Usage: ```c -char version[128]; -slog_version(version, sizeof(version), 0); -printf("slog version: %s", version); +printf("slog version: %s", slog_version(0)); ``` Output will be something like that: diff --git a/example/example.c b/example/example.c index 77cb968..6e24fa9 100644 --- a/example/example.c +++ b/example/example.c @@ -23,12 +23,9 @@ int logCallback(const char *pLog, size_t nLength, slog_flag_t eFlag, void *pCtx) void greet() { /* Get and print slog version */ - char sVersion[128]; - slog_version(sVersion, sizeof(sVersion), 0); - - printf("=========================================\n"); - printf("SLog Version: %s\n", sVersion); - printf("=========================================\n"); + slog("========================================="); + slog("SLog Version: %s", slog_version(0)); + slog("========================================="); } int main() @@ -41,13 +38,13 @@ int main() strcpy(sBuffer, "test string"); //uint16_t nLogFlags = SLOG_ERROR | SLOG_NOTAG | SLOG_NOTE; - /* Greet users */ - greet(); - /* Initialize slog and allow only error and not tagged output */ slog_init("example", SLOG_FLAGS_ALL, 0); slog_config_get(&cfg); + /* Greet users */ + greet(); + /* Disable time and date in output */ cfg.eDateControl = SLOG_TIME_DISABLE; slog_config_set(&cfg); diff --git a/src/slog.c b/src/slog.c index e1e96f3..522b70a 100644 --- a/src/slog.c +++ b/src/slog.c @@ -51,7 +51,7 @@ #endif typedef struct slog_file { - uint8_t nStartDay; + uint8_t nCurrDay; FILE *pHandle; } slog_file_t; @@ -69,6 +69,11 @@ typedef struct slog_context { uint8_t nNewLine; } slog_context_t; +static volatile int g_nHaveSlogVerShort = 0; +static volatile int g_nHaveSlogVerLong = 0; +static char g_slogVerShort[128]; +static char g_slogVerLong[256]; + static slog_t g_slog; static void slog_sync_init(slog_t *pSlog) @@ -199,7 +204,7 @@ static uint8_t slog_open_file(slog_file_t *pFile, const slog_config_t *pCfg, con return 0; } - pFile->nStartDay = pDate->nDay; + pFile->nCurrDay = pDate->nDay; return 1; } @@ -304,9 +309,8 @@ static void slog_display_message(const slog_context_t *pCtx, const char *pInfo, if (!pCfg->nToFile || nCbVal < 0) return; const slog_date_t *pDate = &pCtx->date; - if ((pFile->pHandle == NULL || - pFile->nStartDay != pDate->nDay) && - !slog_open_file(pFile, pCfg, pDate)) return; + if (pFile->nCurrDay != pDate->nDay && pCfg->nRotate) slog_close_file(pFile); + if (pFile->pHandle == NULL && !slog_open_file(pFile, pCfg, pDate)) return; fprintf(pFile->pHandle, "%s%s%s%s%s", pInfo, pSeparator, pMessage, pReset, pNewLine); @@ -405,19 +409,30 @@ void slog_display(slog_flag_t eFlag, uint8_t nNewLine, char *pFormat, ...) slog_unlock(&g_slog); } -size_t slog_version(char *pDest, size_t nSize, uint8_t nMin) +const char* slog_version(uint8_t nShort) { - size_t nLength = 0; + if (nShort) + { + if (!g_nHaveSlogVerShort) + { + snprintf(g_slogVerShort, sizeof(g_slogVerShort), "%d.%d.%d", + SLOG_VERSION_MAJOR, SLOG_VERSION_MINOR, SLOG_BUILD_NUMBER); - /* Version short */ - if (nMin) nLength = snprintf(pDest, nSize, "%d.%d.%d", - SLOG_VERSION_MAJOR, SLOG_VERSION_MINOR, SLOG_BUILD_NUM); + g_nHaveSlogVerShort = 1; + } - /* Version long */ - else nLength = snprintf(pDest, nSize, "%d.%d build %d (%s)", - SLOG_VERSION_MAJOR, SLOG_VERSION_MINOR, SLOG_BUILD_NUM, __DATE__); + return g_slogVerShort; + } + + if (!g_nHaveSlogVerLong) + { + snprintf(g_slogVerLong, sizeof(g_slogVerLong), "%d.%d build %d (%s)", + SLOG_VERSION_MAJOR, SLOG_VERSION_MINOR, SLOG_BUILD_NUMBER, __DATE__); + + g_nHaveSlogVerLong = 1; + } - return nLength; + return g_slogVerLong; } void slog_config_get(slog_config_t *pCfg) @@ -519,6 +534,7 @@ void slog_init(const char* pName, uint16_t nFlags, uint8_t nTdSafe) pCfg->nUseHeap = 0; pCfg->nToFile = 0; pCfg->nIndent = 0; + pCfg->nRotate = 1; pCfg->nFlush = 0; pCfg->nFlags = nFlags; @@ -526,7 +542,7 @@ void slog_init(const char* pName, uint16_t nFlags, uint8_t nTdSafe) snprintf(pCfg->sFileName, sizeof(pCfg->sFileName), "%s", pFileName); pFile->pHandle = NULL; - pFile->nStartDay = 0; + pFile->nCurrDay = 0; #ifdef WIN32 /* Enable color support */ diff --git a/src/slog.h b/src/slog.h index e0c6057..8c7213f 100644 --- a/src/slog.h +++ b/src/slog.h @@ -36,7 +36,7 @@ extern "C" { /* SLog version information */ #define SLOG_VERSION_MAJOR 1 #define SLOG_VERSION_MINOR 8 -#define SLOG_BUILD_NUM 36 +#define SLOG_BUILD_NUMBER 37 /* Supported colors */ #define SLOG_COLOR_NORMAL "\x1B[0m" @@ -167,6 +167,7 @@ typedef struct SLogConfig { uint8_t nUseHeap; // Use dynamic allocation uint8_t nToFile; // Enable file logging uint8_t nIndent; // Enable indentations + uint8_t nRotate; // Enable log rotation uint8_t nFlush; // Flush stdout after screen log uint16_t nFlags; // Allowed log level flags @@ -175,7 +176,7 @@ typedef struct SLogConfig { char sFilePath[SLOG_PATH_MAX]; // Output file path for logs } slog_config_t; -size_t slog_version(char *pDest, size_t nSize, uint8_t nMin); +const char* slog_version(uint8_t nShort); void slog_config_get(slog_config_t *pCfg); void slog_config_set(slog_config_t *pCfg);