liblogax
Classes | Macros | Typedefs | Enumerations
logax.h File Reference
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
Include dependency graph for logax.h:

Go to the source code of this file.

Classes

struct  logax_logger_s
 

Macros

#define __LOGAX_FUNCTION__   "<unknown>"
 
#define LOGAX_NULL   0L
 
#define LOGAX_RESET_TERMINAL   "\x1B[0m"
 
#define LOGAX_FOREGROUND_TRACE   "\x1B[36m"
 
#define LOGAX_FOREGROUND_DEBUG   "\x1B[32m"
 
#define LOGAX_FOREGROUND_INFO   "\x1B[35m"
 
#define LOGAX_FOREGROUND_WARN   "\x1B[33m"
 
#define LOGAX_FOREGROUND_ERROR   "\x1B[31m"
 
#define LOGAX_FOREGROUND_FATAL   "\x1B[31m"
 
#define LOGAX_RESET_TERMINAL_ATTR()   fprintf(stream, "%s", LOGAX_RESET_TERMINAL)
 
#define LOGAX_MAX_CALLBACKS   5
 
#define LOGAX_OPTION_QUITE   LOGAX_OPTION_QUIET
 
#define GET_LEVEL_STRING(level)
 
#define logax_add_flag(flags, flag)   flags |= flag;
 
#define logax_set_flag   logax_add_flag
 
#define logax_remove_flag(flags, flag)   flags &= ~flag;
 
#define logax_clear_flag   logax_remove_flag
 
#define logax_set_formatter(logax_logger, new_formatter)
 
#define logax_set_quiet(logax_logger, make_quiet)
 
#define logax_set_quite   logax_set_quiet
 
#define LOGAX_Iinitialized_HCONSOLE()
 
#define LOGAX_DELEGATE_FPRINT_STR(foreground, text)   if (is_colored) { fprintf(stream, "%s%s%s", foreground, text, LOGAX_RESET_TERMINAL); } else { fprintf(stream, "%s", text); }
 
#define LOGAX_DELEGATE_FPRINT_SIZE_T(foreground, text)   if (is_colored) { fprintf(stream, "%s%zu%s", foreground, text, LOGAX_RESET_TERMINAL); } else { fprintf(stream, "%zu", text); }
 
#define LOGAX_WRITE_COLOR_CODE(color)   if (is_colored) { fprintf(stream, "%s", color); }
 
#define LOGAX_WRITE_LEVEL_COLOR__INTERNALL__(prefix_value, level, suffix_value)
 
#define logax_write_text_format_to_stream(stream, flags, fmt, ...)   logax_write_text_format_to_stream__internal__(stream, flags, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__)
 
#define logax_write_key_value_format_to_stream(stream, flags, fmt, ...)   logax_write_key_value_format_to_stream__internal__(stream, flags, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__)
 
#define LOGAX_INTERNAL_WRITE_JSON_STR(key, value)
 
#define LOGAX_INTERNAL_WRITE_JSON_SIZE_T(key, value)
 
#define logax_write_json_format_to_stream(stream, flags, fmt, ...)   logax_write_json_format_to_stream__internal__(stream, flags, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__)
 
#define logax_logger_report_to_callback(logax_logger, level, fmt, ...)
 
#define logax_logger_trace(logax_logger, fmt, ...)
 
#define logax_logger_debug(logax_logger, fmt, ...)
 
#define logax_logger_info(logax_logger, fmt, ...)
 
#define logax_logger_warn(logax_logger, fmt, ...)
 
#define logax_logger_error(logax_logger, fmt, ...)
 
#define logax_logger_fatal(logax_logger, fmt, ...)
 

Typedefs

typedef void(* logax_callback) (const char *date, const char *time, const int level, const char *file, const size_t line_number, const char *function_name, const char *fmt,...)
 
typedef struct logax_logger_s LogaxLogger
 

Enumerations

enum  LogaxOption {
  LOGAX_OPTION_QUIET = 1 << 1, LOGAX_OPTION_DATE = 1 << 2, LOGAX_OPTION_TIME = 1 << 3, LOGAX_OPTION_DATE_TIME = 1 << 4,
  LOGAX_OPTION_FILE_PATH = 1 << 5, LOGAX_OPTION_FILE_NAME_ONLY = 1 << 6, LOGAX_OPTION_LINE_NUMBER = 1 << 7, LOGAX_OPTION_COLORED = 1 << 8,
  LOGAX_OPTION_FUNCTION = 1 << 9, LOGAX_OPTION_ALL = 1 << 15
}
 
enum  LogaxLevel {
  LOGAX_LEVEL_TRACE = 1 << 16, LOGAX_LEVEL_DEBUG = 1 << 17, LOGAX_LEVEL_INFO = 1 << 18, LOGAX_LEVEL_WARN = 1 << 19,
  LOGAX_LEVEL_ERROR = 1 << 20, LOGAX_LEVEL_FATAL = 1 << 21
}
 
enum  LogaxFormatter { LOGAX_FORMATTER_TEXT = 1 << 25, LOGAX_FORMATTER_KEY_VALUE = 1 << 26, LOGAX_FORMATTER_JSON = 1 << 27 }
 

Detailed Description

Author
Adewale Azeez iamth.nosp@m.ecar.nosp@m.isma@.nosp@m.gmai.nosp@m.l.com
Date
18 Nov 2021

Macro Definition Documentation

◆ __LOGAX_FUNCTION__

#define __LOGAX_FUNCTION__   "<unknown>"

The inline keyword to optimize the function. In C89 and C90 the inline keyword semantic is different from current C standard semantic hence for compilation targeting C89 or C99 the inline keyword is ommited.

◆ GET_LEVEL_STRING

#define GET_LEVEL_STRING (   level)
Value:
((level == LOGAX_LEVEL_TRACE) ? "TRACE" :\
(level == LOGAX_LEVEL_DEBUG) ? "DEBUG" :\
(level == LOGAX_LEVEL_INFO) ? "INFO" :\
(level == LOGAX_LEVEL_WARN) ? "WARN" :\
(level == LOGAX_LEVEL_ERROR) ? "ERROR" :\
(level == LOGAX_LEVEL_FATAL) ? "FATAL" : "")

Get the string name of the log level.

◆ logax_add_flag

#define logax_add_flag (   flags,
  flag 
)    flags |= flag;

◆ logax_clear_flag

#define logax_clear_flag   logax_remove_flag

◆ LOGAX_DELEGATE_FPRINT_SIZE_T

#define LOGAX_DELEGATE_FPRINT_SIZE_T (   foreground,
  text 
)    if (is_colored) { fprintf(stream, "%s%zu%s", foreground, text, LOGAX_RESET_TERMINAL); } else { fprintf(stream, "%zu", text); }

◆ LOGAX_DELEGATE_FPRINT_STR

#define LOGAX_DELEGATE_FPRINT_STR (   foreground,
  text 
)    if (is_colored) { fprintf(stream, "%s%s%s", foreground, text, LOGAX_RESET_TERMINAL); } else { fprintf(stream, "%s", text); }

◆ LOGAX_FOREGROUND_DEBUG

#define LOGAX_FOREGROUND_DEBUG   "\x1B[32m"

debug terminal foreground color - green

◆ LOGAX_FOREGROUND_ERROR

#define LOGAX_FOREGROUND_ERROR   "\x1B[31m"

error terminal foreground color - red

◆ LOGAX_FOREGROUND_FATAL

#define LOGAX_FOREGROUND_FATAL   "\x1B[31m"

fatal terminal foreground color - red

◆ LOGAX_FOREGROUND_INFO

#define LOGAX_FOREGROUND_INFO   "\x1B[35m"

info terminal foreground color - magenta

◆ LOGAX_FOREGROUND_TRACE

#define LOGAX_FOREGROUND_TRACE   "\x1B[36m"

trace terminal foreground color - cyan

◆ LOGAX_FOREGROUND_WARN

#define LOGAX_FOREGROUND_WARN   "\x1B[33m"

warn terminal foreground color - yellow

◆ LOGAX_Iinitialized_HCONSOLE

#define LOGAX_Iinitialized_HCONSOLE ( )

◆ LOGAX_INTERNAL_WRITE_JSON_SIZE_T

#define LOGAX_INTERNAL_WRITE_JSON_SIZE_T (   key,
  value 
)
Value:
LOGAX_DELEGATE_FPRINT_STR(LOGAX_FOREGROUND_DEBUG, "\"" key "\""); fprintf(stream, ":");\
LOGAX_DELEGATE_FPRINT_SIZE_T(LOGAX_FOREGROUND_TRACE, value);

◆ LOGAX_INTERNAL_WRITE_JSON_STR

#define LOGAX_INTERNAL_WRITE_JSON_STR (   key,
  value 
)
Value:
LOGAX_DELEGATE_FPRINT_STR(LOGAX_FOREGROUND_DEBUG, "\"" key "\""); fprintf(stream, ":");\
LOGAX_WRITE_COLOR_CODE(LOGAX_FOREGROUND_TRACE); fprintf(stream, "\"%s\"", value); if (is_colored) { LOGAX_RESET_TERMINAL_ATTR(); }

◆ logax_logger_debug

#define logax_logger_debug (   logax_logger,
  fmt,
  ... 
)
Value:
logax_logger_write(logax_logger, LOGAX_LEVEL_DEBUG, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
logax_logger_report_to_callback(logax_logger, LOGAX_LEVEL_DEBUG, fmt, __VA_ARGS__)

◆ logax_logger_error

#define logax_logger_error (   logax_logger,
  fmt,
  ... 
)
Value:
logax_logger_write(logax_logger, LOGAX_LEVEL_ERROR, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
logax_logger_report_to_callback(logax_logger, LOGAX_LEVEL_ERROR, fmt, __VA_ARGS__)

◆ logax_logger_fatal

#define logax_logger_fatal (   logax_logger,
  fmt,
  ... 
)
Value:
logax_logger_write(logax_logger, LOGAX_LEVEL_FATAL, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
logax_logger_report_to_callback(logax_logger, LOGAX_LEVEL_FATAL, fmt, __VA_ARGS__)

◆ logax_logger_info

#define logax_logger_info (   logax_logger,
  fmt,
  ... 
)
Value:
logax_logger_write(logax_logger, LOGAX_LEVEL_INFO, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
logax_logger_report_to_callback(logax_logger, LOGAX_LEVEL_INFO, fmt, __VA_ARGS__)

◆ logax_logger_report_to_callback

#define logax_logger_report_to_callback (   logax_logger,
  level,
  fmt,
  ... 
)
Value:
{\
int index = 0;\
char time_buffer__logax_tmp_var__[16];\
char date_buffer__logax_tmp_var__[16];\
time_t time_raw__logax_tmp_var__ = time(LOGAX_NULL);\
struct tm *current_time__logax_tmp_var__ = localtime(&time_raw__logax_tmp_var__);\
date_buffer__logax_tmp_var__[strftime(date_buffer__logax_tmp_var__, sizeof(date_buffer__logax_tmp_var__), "%Y-%m-%d", current_time__logax_tmp_var__)] = '\0';\
time_buffer__logax_tmp_var__[strftime(time_buffer__logax_tmp_var__, sizeof(time_buffer__logax_tmp_var__), "%H:%M:%S", current_time__logax_tmp_var__)] = '\0';\
for (; index < LOGAX_MAX_CALLBACKS; index++) {\
logax_callback callback = (logax_logger)->callbacks[index];\
if (callback != LOGAX_NULL) {\
callback(date_buffer__logax_tmp_var__, time_buffer__logax_tmp_var__, level, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
}\
}\
}

◆ logax_logger_trace

#define logax_logger_trace (   logax_logger,
  fmt,
  ... 
)
Value:
logax_logger_write(logax_logger, LOGAX_LEVEL_TRACE, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
logax_logger_report_to_callback(logax_logger, LOGAX_LEVEL_TRACE, fmt, __VA_ARGS__)

◆ logax_logger_warn

#define logax_logger_warn (   logax_logger,
  fmt,
  ... 
)
Value:
logax_logger_write(logax_logger, LOGAX_LEVEL_WARN, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__);\
logax_logger_report_to_callback(logax_logger, LOGAX_LEVEL_WARN, fmt, __VA_ARGS__)

◆ LOGAX_MAX_CALLBACKS

#define LOGAX_MAX_CALLBACKS   5

◆ LOGAX_NULL

#define LOGAX_NULL   0L

◆ LOGAX_OPTION_QUITE

#define LOGAX_OPTION_QUITE   LOGAX_OPTION_QUIET

◆ logax_remove_flag

#define logax_remove_flag (   flags,
  flag 
)    flags &= ~flag;

◆ LOGAX_RESET_TERMINAL

#define LOGAX_RESET_TERMINAL   "\x1B[0m"

reset the terminal color

◆ LOGAX_RESET_TERMINAL_ATTR

#define LOGAX_RESET_TERMINAL_ATTR ( )    fprintf(stream, "%s", LOGAX_RESET_TERMINAL)

reset the terminal color

◆ logax_set_flag

#define logax_set_flag   logax_add_flag

◆ logax_set_formatter

#define logax_set_formatter (   logax_logger,
  new_formatter 
)
Value:
{\
if ((logax_logger)->flags & LOGAX_FORMATTER_TEXT) logax_remove_flag((logax_logger)->flags, LOGAX_FORMATTER_TEXT);\
if ((logax_logger)->flags & LOGAX_FORMATTER_KEY_VALUE) logax_remove_flag((logax_logger)->flags, LOGAX_FORMATTER_KEY_VALUE);\
if ((logax_logger)->flags & LOGAX_FORMATTER_JSON) logax_remove_flag((logax_logger)->flags, LOGAX_FORMATTER_JSON);\
logax_add_flag((logax_logger)->flags, new_formatter);\
}

◆ logax_set_quiet

#define logax_set_quiet (   logax_logger,
  make_quiet 
)
Value:
{\
if (!make_quiet && ((logax_logger)->flags & LOGAX_OPTION_QUIET)) { logax_remove_flag((logax_logger)->flags, LOGAX_OPTION_QUIET); }\
else if (make_quiet && !((logax_logger)->flags & LOGAX_OPTION_QUIET)) { logax_add_flag((logax_logger)->flags, LOGAX_OPTION_QUIET); }\
}

◆ logax_set_quite

#define logax_set_quite   logax_set_quiet

Alias for logax_set_quiet, for backward compatibility

◆ LOGAX_WRITE_COLOR_CODE

#define LOGAX_WRITE_COLOR_CODE (   color)    if (is_colored) { fprintf(stream, "%s", color); }

◆ logax_write_json_format_to_stream

#define logax_write_json_format_to_stream (   stream,
  flags,
  fmt,
  ... 
)    logax_write_json_format_to_stream__internal__(stream, flags, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__)

◆ logax_write_key_value_format_to_stream

#define logax_write_key_value_format_to_stream (   stream,
  flags,
  fmt,
  ... 
)    logax_write_key_value_format_to_stream__internal__(stream, flags, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__)

◆ LOGAX_WRITE_LEVEL_COLOR__INTERNALL__

#define LOGAX_WRITE_LEVEL_COLOR__INTERNALL__ (   prefix_value,
  level,
  suffix_value 
)
Value:
if (is_colored) { LOGAX_DELEGATE_FPRINT_STR(LOGAX_FOREGROUND_##level, #level); fprintf(stream, "%s", suffix_value); }\
else fprintf(stream, "%s%s%s", prefix_value, #level, suffix_value);

◆ logax_write_text_format_to_stream

#define logax_write_text_format_to_stream (   stream,
  flags,
  fmt,
  ... 
)    logax_write_text_format_to_stream__internal__(stream, flags, __FILE__, __LINE__, __LOGAX_FUNCTION__, fmt, __VA_ARGS__)

Typedef Documentation

◆ logax_callback

typedef void(* logax_callback) (const char *date, const char *time, const int level, const char *file, const size_t line_number, const char *function_name, const char *fmt,...)

This is the function signature of the callback, any function defined with same signature can be registered with the LogaxLogger as callback which will be invoked if a new logevent is sent.

◆ LogaxLogger

typedef struct logax_logger_s LogaxLogger

Enumeration Type Documentation

◆ LogaxFormatter

The options to specify the format of the log output

Enumerator
LOGAX_FORMATTER_TEXT 

Format the log output as plain string

LOGAX_FORMATTER_KEY_VALUE 

Format the log output as a key value entries

LOGAX_FORMATTER_JSON 

Format the log output as a valid JSON object

◆ LogaxLevel

enum LogaxLevel

The options to classify the log event by level

Enumerator
LOGAX_LEVEL_TRACE 

Mark the log event as trace

LOGAX_LEVEL_DEBUG 

Mark the log event as debug

LOGAX_LEVEL_INFO 

Mark the log event as an information

LOGAX_LEVEL_WARN 

Mark the log event as a warning

LOGAX_LEVEL_ERROR 

Mark the log event as an error

LOGAX_LEVEL_FATAL 

Mark the log event as a critical error

◆ LogaxOption

More bit location is reserved till 15 to accomodate new options without breaking backward compatibility

Enumerator
LOGAX_OPTION_QUIET 

the option to disable writing to output stream

LOGAX_OPTION_DATE 

option to write/set the date of the log event

LOGAX_OPTION_TIME 

option to set the time of the log event

LOGAX_OPTION_DATE_TIME 

the option to set both the date and time of the log event

LOGAX_OPTION_FILE_PATH 

option to set the full path to the file where the log event was sent from

LOGAX_OPTION_FILE_NAME_ONLY 

option to set only the name of the file where the log event was sent from

LOGAX_OPTION_LINE_NUMBER 

option to set the line number where the log even was sent

LOGAX_OPTION_COLORED 

this option is used to enable pretty print and colored outputs

LOGAX_OPTION_FUNCTION 

option to write/set the name of the function where the log was set from

LOGAX_OPTION_ALL 

this option enables all the other options above apart from LOGAX_OPTION_QUIET

LOGAX_LEVEL_ERROR
@ LOGAX_LEVEL_ERROR
Definition: logax.h:129
LOGAX_LEVEL_WARN
@ LOGAX_LEVEL_WARN
Definition: logax.h:128
LOGAX_FORMATTER_JSON
@ LOGAX_FORMATTER_JSON
Definition: logax.h:149
LOGAX_RESET_TERMINAL_ATTR
#define LOGAX_RESET_TERMINAL_ATTR()
Definition: logax.h:75
logax_add_flag
#define logax_add_flag(flags, flag)
Definition: logax.h:238
LOGAX_LEVEL_INFO
@ LOGAX_LEVEL_INFO
Definition: logax.h:127
LOGAX_NULL
#define LOGAX_NULL
Definition: logax.h:49
LOGAX_OPTION_QUIET
@ LOGAX_OPTION_QUIET
Definition: logax.h:100
LOGAX_DELEGATE_FPRINT_STR
#define LOGAX_DELEGATE_FPRINT_STR(foreground, text)
Definition: logax.h:372
LOGAX_LEVEL_TRACE
@ LOGAX_LEVEL_TRACE
Definition: logax.h:125
LOGAX_LEVEL_FATAL
@ LOGAX_LEVEL_FATAL
Definition: logax.h:130
LOGAX_MAX_CALLBACKS
#define LOGAX_MAX_CALLBACKS
Definition: logax.h:84
__LOGAX_FUNCTION__
#define __LOGAX_FUNCTION__
Definition: logax.h:47
logax_remove_flag
#define logax_remove_flag(flags, flag)
Definition: logax.h:248
LOGAX_FORMATTER_KEY_VALUE
@ LOGAX_FORMATTER_KEY_VALUE
Definition: logax.h:148
LOGAX_FORMATTER_TEXT
@ LOGAX_FORMATTER_TEXT
Definition: logax.h:147
LOGAX_LEVEL_DEBUG
@ LOGAX_LEVEL_DEBUG
Definition: logax.h:126
LOGAX_FOREGROUND_DEBUG
#define LOGAX_FOREGROUND_DEBUG
Definition: logax.h:70
LOGAX_FOREGROUND_TRACE
#define LOGAX_FOREGROUND_TRACE
Definition: logax.h:69