Skip to content

Commit

Permalink
Updated slog version function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Kalatozishvili committed Aug 26, 2023
1 parent 7cd0b3d commit 4769191
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down
15 changes: 6 additions & 9 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down
46 changes: 31 additions & 15 deletions src/slog.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#endif

typedef struct slog_file {
uint8_t nStartDay;
uint8_t nCurrDay;
FILE *pHandle;
} slog_file_t;

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -519,14 +534,15 @@ 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;

const char *pFileName = (pName != NULL) ? pName : SLOG_NAME_DEFAULT;
snprintf(pCfg->sFileName, sizeof(pCfg->sFileName), "%s", pFileName);

pFile->pHandle = NULL;
pFile->nStartDay = 0;
pFile->nCurrDay = 0;

#ifdef WIN32
/* Enable color support */
Expand Down
5 changes: 3 additions & 2 deletions src/slog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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);

Expand Down

0 comments on commit 4769191

Please sign in to comment.