Skip to content

Commit

Permalink
Changed custom size types with standard types
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed May 10, 2023
1 parent ecbed8c commit d80e508
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
27 changes: 13 additions & 14 deletions src/slog.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#endif

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -162,11 +161,11 @@ static const char* slog_get_color(slog_flag_t eFlag)
return SLOG_EMPTY;
}

slog_u16_t slog_get_usec()
uint16_t slog_get_usec()
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) return 0;
return (slog_u16_t)(tv.tv_usec / 1000);
return (uint16_t)(tv.tv_usec / 1000);
}

void slog_get_date(slog_date_t *pDate)
Expand All @@ -188,12 +187,12 @@ void slog_get_date(slog_date_t *pDate)
pDate->nUsec = slog_get_usec();
}

static slog_uptr_t slog_get_tid()
static size_t slog_get_tid()
{
#ifdef __linux__
return (slog_uptr_t)syscall(__NR_gettid);
return (size_t)syscall(__NR_gettid);
#else
return (slog_uptr_t)pthread_self();
return (size_t)pthread_self();
#endif
}

Expand All @@ -215,18 +214,18 @@ static void slog_create_tag(char *pOut, size_t nSize, slog_flag_t eFlag, const c
else snprintf(pOut, nSize, "%s<%s>%s%s", pColor, pTag, SLOG_COLOR_RESET, pIndent);
}

static void slog_create_tid(char *pOut, int nSize, slog_u8_t nTraceTid)
static void slog_create_tid(char *pOut, int nSize, uint8_t nTraceTid)
{
if (!nTraceTid) pOut[0] = SLOG_NUL;
else snprintf(pOut, nSize, "(%" PRIuPTR ") ", slog_get_tid());
else snprintf(pOut, nSize, "(%zu) ", slog_get_tid());
}

static void slog_display_message(const slog_context_t *pCtx, const char *pInfo, int nInfoLen, const char *pInput)
{
slog_config_t *pCfg = &g_slog.config;
int nCbVal = 1;

slog_u8_t nFullColor = pCfg->eColorFormat == SLOG_COLORING_FULL ? 1 : 0;
uint8_t nFullColor = pCfg->eColorFormat == SLOG_COLORING_FULL ? 1 : 0;
const char *pSeparator = nInfoLen > 0 ? pCfg->sSeparator : SLOG_EMPTY;
const char *pNewLine = pCfg->nNewLine ? SLOG_NEWLINE : SLOG_EMPTY;
const char *pMessage = pInput != NULL ? pInput : SLOG_EMPTY;
Expand Down Expand Up @@ -286,7 +285,7 @@ static int slog_create_info(const slog_context_t *pCtx, char* pOut, size_t nSize
}

char sTid[SLOG_TAG_MAX], sTag[SLOG_TAG_MAX];
slog_u8_t nFullColor = pCfg->eColorFormat == SLOG_COLORING_FULL ? 1 : 0;
uint8_t nFullColor = pCfg->eColorFormat == SLOG_COLORING_FULL ? 1 : 0;

const char *pColorCode = slog_get_color(pCtx->eFlag);
const char *pColor = nFullColor ? pColorCode : SLOG_EMPTY;
Expand Down Expand Up @@ -354,7 +353,7 @@ void slog_display(slog_flag_t eFlag, const char *pFormat, ...)
slog_unlock(&g_slog);
}

size_t slog_version(char *pDest, size_t nSize, slog_u8_t nMin)
size_t slog_version(char *pDest, size_t nSize, uint8_t nMin)
{
size_t nLength = 0;

Expand Down Expand Up @@ -424,14 +423,14 @@ void slog_separator_set(const char *pFormat, ...)
slog_unlock(&g_slog);
}

void slog_indent(slog_u8_t nEnable)
void slog_indent(uint8_t nEnable)
{
slog_lock(&g_slog);
g_slog.config.nIndent = nEnable;
slog_unlock(&g_slog);
}

void slog_new_line(slog_u8_t nEnable)
void slog_new_line(uint8_t nEnable)
{
slog_lock(&g_slog);
g_slog.config.nNewLine = nEnable;
Expand All @@ -447,7 +446,7 @@ void slog_callback_set(slog_cb_t callback, void *pContext)
slog_unlock(&g_slog);
}

void slog_init(const char* pName, slog_u16_t nFlags, slog_u8_t nTdSafe)
void slog_init(const char* pName, uint16_t nFlags, uint8_t nTdSafe)
{
/* Set up default values */
slog_config_t *pCfg = &g_slog.config;
Expand Down
48 changes: 22 additions & 26 deletions src/slog.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ extern "C" {

#include <inttypes.h>
#include <pthread.h>

typedef uintptr_t slog_uptr_t;
typedef unsigned int slog_u32_t;
typedef unsigned short int slog_u16_t;
typedef unsigned char slog_u8_t;
#include <stdint.h>

/* SLog version information */
#define SLOG_VERSION_MAJOR 1
#define SLOG_VERSION_MINOR 8
#define SLOG_BUILD_NUM 29
#define SLOG_BUILD_NUM 30

/* Supported colors */
#define SLOG_COLOR_NORMAL "\x1B[0m"
Expand Down Expand Up @@ -79,16 +75,16 @@ typedef unsigned char slog_u8_t;
#define SLOG_NUL '\0'

typedef struct SLogDate {
slog_u16_t nYear;
slog_u8_t nMonth;
slog_u8_t nDay;
slog_u8_t nHour;
slog_u8_t nMin;
slog_u8_t nSec;
slog_u16_t nUsec;
uint16_t nYear;
uint8_t nMonth;
uint8_t nDay;
uint8_t nHour;
uint8_t nMin;
uint8_t nSec;
uint16_t nUsec;
} slog_date_t;

slog_u16_t slog_get_usec();
uint16_t slog_get_usec();
void slog_get_date(slog_date_t *pDate);

/* Log level flags */
Expand Down Expand Up @@ -160,33 +156,33 @@ typedef struct SLogConfig {
slog_cb_t logCallback; // Log callback to collect logs
void* pCallbackCtx; // Data pointer passed to log callback

slog_u8_t nTraceTid; // Trace thread ID and display in output
slog_u8_t nToScreen; // Enable screen logging
slog_u8_t nNewLine; // Enable new line ending
slog_u8_t nUseHeap; // Use dynamic allocation
slog_u8_t nToFile; // Enable file logging
slog_u8_t nIndent; // Enable indentations
slog_u8_t nFlush; // Flush stdout after screen log
slog_u16_t nFlags; // Allowed log level flags
uint8_t nTraceTid; // Trace thread ID and display in output
uint8_t nToScreen; // Enable screen logging
uint8_t nNewLine; // Enable new line ending
uint8_t nUseHeap; // Use dynamic allocation
uint8_t nToFile; // Enable file logging
uint8_t nIndent; // Enable indentations
uint8_t nFlush; // Flush stdout after screen log
uint16_t nFlags; // Allowed log level flags

char sSeparator[SLOG_NAME_MAX]; // Separator between info and log
char sFileName[SLOG_NAME_MAX]; // Output file name for logs
char sFilePath[SLOG_PATH_MAX]; // Output file path for logs
} slog_config_t;

size_t slog_version(char *pDest, size_t nSize, slog_u8_t nMin);
size_t slog_version(char *pDest, size_t nSize, uint8_t nMin);
void slog_config_get(slog_config_t *pCfg);
void slog_config_set(slog_config_t *pCfg);

void slog_separator_set(const char *pFormat, ...);
void slog_callback_set(slog_cb_t callback, void *pContext);
void slog_new_line(slog_u8_t nEnable);
void slog_indent(slog_u8_t nEnable);
void slog_new_line(uint8_t nEnable);
void slog_indent(uint8_t nEnable);

void slog_enable(slog_flag_t eFlag);
void slog_disable(slog_flag_t eFlag);

void slog_init(const char* pName, slog_u16_t nFlags, slog_u8_t nTdSafe);
void slog_init(const char* pName, uint16_t nFlags, uint8_t nTdSafe);
void slog_display(slog_flag_t eFlag, const char *pFormat, ...);
void slog_destroy(); // Required only if the slog_init() called with nTdSafe > 0

Expand Down

0 comments on commit d80e508

Please sign in to comment.