Skip to content

Commit

Permalink
Properly open log file handle on Windows OS
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Aug 23, 2023
1 parent c3f4db7 commit 25d58cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,29 @@ Without indentations enabled:
### Version
There are two ways to get and print slog version with this library. Function `slog_version()` returns char pointer of static array where slog version string is located. If argument is more than zero function returns string with only version and build number. Otherwise it returns full version format with build date.
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
Usage:
```c
printf("slog version: %s", slog_version(0));
char version[128];
slog_version(version, sizeof(version), 0);
printf("slog version: %s", version);
```

Output will be something like that:
```
slog version: 1.8 build 22 (Dec 14 2020)
```

There are also definitions that can be used to check the version without using the function.

- `SLOG_VERSION_MAJOR` - Major version of the library.
- `SLOG_VERSION_MINOR` - minor version of the library.
- `SLOG_BUILD_NUM` - Build number.

### Output
Here is en example of the log file context created by slog:
```
Expand Down
5 changes: 5 additions & 0 deletions src/slog.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ static uint8_t slog_open_file(slog_file_t *pFile, const slog_config_t *pCfg, con
snprintf(sFilePath, sizeof(sFilePath), "%s/%s-%04d-%02d-%02d.log",
pCfg->sFilePath, pCfg->sFileName, pDate->nYear, pDate->nMonth, pDate->nDay);

#ifdef _WIN32
if (fopen_s(&pFile->pHandle, sFilePath, "a")) pFile->pHandle = NULL;
#else
pFile->pHandle = fopen(sFilePath, "a");
#endif

if (pFile->pHandle == NULL)
{
printf("<%s:%d> %s: [ERROR] Failed to open file: %s (%s)\n",
Expand Down
2 changes: 1 addition & 1 deletion 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 35
#define SLOG_BUILD_NUM 36

/* Supported colors */
#define SLOG_COLOR_NORMAL "\x1B[0m"
Expand Down

0 comments on commit 25d58cb

Please sign in to comment.