ulogger
athreadsafeloggerwithcoloredoutput
logger.h File Reference

a thread safe logger with optional printf style logging More...

#include "colors.h"
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
Include dependency graph for logger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  thread_logger
 
struct  file_logger
 

Macros

#define __FILENAME__   (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
 strips leading path from FILE More...
 
#define LOG_INFO(thl, msg)   thl->log(thl, 0, msg, LOG_LEVELS_INFO, __FILENAME__, __LINE__);
 used to emit a standard INFO log More...
 
#define LOG_WARN(thl, msg)   thl->log(thl, 0, msg, LOG_LEVELS_WARN, __FILENAME__, __LINE__);
 used to emit a standard WARN log More...
 
#define LOG_ERROR(thl, msg)   thl->log(thl, 0, msg, LOG_LEVELS_ERROR, __FILENAME__, __LINE__);
 used to emit a standard ERROR log More...
 
#define LOG_DEBUG(thl, msg)   thl->log(thl, 0, msg, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__);
 used to emit a standard DEBUG log More...
 
#define LOGF_INFO(thl, msg, ...)   thl->logf(thl, 0, LOG_LEVELS_INFO, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 used to emit a printf INFO log More...
 
#define LOGF_WARN(thl, msg, ...)   thl->logf(thl, 0, LOG_LEVELS_WARN, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 used to emit a printf WARN log More...
 
#define LOGF_ERROR(thl, msg, ...)   thl->logf(thl, 0, LOG_LEVELS_ERROR, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 used to emit a printf ERROR log More...
 
#define LOGF_DEBUG(thl, msg, ...)   thl->logf(thl, 0, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 used to emit a printf DEBUG log More...
 
#define fLOG_INFO(fhl, msg)   fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_INFO, __FILENAME__, __LINE__);
 like LOG_INFO except for file logging More...
 
#define fLOG_WARN(fhl, msg)   fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_WARN, __FILENAME__, __LINE__);
 like LOG_WARN except for file logging More...
 
#define fLOG_ERROR(fhl, msg)   fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_ERROR, __FILENAME__, __LINE__);
 like LOG_ERROR except for file logging More...
 
#define fLOG_DEBUG(fhl, msg)   fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__);
 like LOG_DEBUG except for file logging More...
 
#define fLOGF_INFO(fhl, msg, ...)   fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_INFO, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 like LOGF_INFO except for file logging More...
 
#define fLOGF_WARN(fhl, msg, ...)   fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_WARN, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 like LOGF_WARN except for file logging More...
 
#define fLOGF_ERROR(fhl, msg, ...)   fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_ERROR, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 like LOGF_ERROR except for file logging More...
 
#define fLOGF_DEBUG(fhl, msg, ...)   fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__, msg, __VA_ARGS__);
 like LOGF_DEBUG except for file logging More...
 

Typedefs

typedef int(* mutex_fn) (pthread_mutex_t *mx)
 
typedef void(* log_fn) (struct thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level, char *file, int line)
 
typedef void(* log_fnf) (struct thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *file, int line, char *message,...)
 
typedef struct thread_logger thread_logger
 
typedef struct file_logger file_logger
 

Enumerations

enum  LOG_LEVELS { LOG_LEVELS_INFO, LOG_LEVELS_WARN, LOG_LEVELS_ERROR, LOG_LEVELS_DEBUG }
 

Functions

thread_loggernew_thread_logger (bool with_debug)
 returns a new thread safe logger if with_debug is false, then all debug_log calls will be ignored More...
 
file_loggernew_file_logger (char *output_file, bool with_debug)
 returns a new file_logger Calls new_thread_logger internally More...
 
void clear_thread_logger (thread_logger *thl)
 free resources for the threaded logger More...
 
void clear_file_logger (file_logger *fhl)
 free resources for the file ogger More...
 
void log_func (thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level, char *file, int line)
 main function you should call, which will delegate to the appopriate *_log function More...
 
void logf_func (thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *file, int line, char *message,...)
 like log_func but for formatted logs More...
 
void debug_log (thread_logger *thl, int file_descriptor, char *message)
 logs a debug styled message - called by log_fn More...
 
void warn_log (thread_logger *thl, int file_descriptor, char *message)
 logs a warned styled message - called by log_fn More...
 
void error_log (thread_logger *thl, int file_descriptor, char *message)
 logs an error styled message - called by log_fn More...
 
void info_log (thread_logger *thl, int file_descriptor, char *message)
 logs an info styled message - called by log_fn More...
 
int write_file_log (int file_descriptor, char *message)
 used to write a log message to file although this really means a file descriptor More...
 
void get_time_string (char *date_buffer, size_t date_buffer_len)
 returns a timestamp of format Jul 06 10:12:20 PM More...
 

Detailed Description

a thread safe logger with optional printf style logging

allows writing color coded logs to stdout, with optional file output as well. timestamps all logs, and provides optional printf style logging

Note
logf_func has a bug where some format is respected and others are not, consider the following from a ss format:
  • [error - Jul 06 10:01:07 PM] one<insert-tab-here>two
  • [warn - Jul 06 10:01:07 PM] one two
warn, and info appear to not respect format, while debug and error do
Todo:
  • buffer logs and use a dedicated thread for writing (avoid blocking locks)
  • handling system signals (exit, kill, etc...)

Definition in file logger.h.

Macro Definition Documentation

◆ __FILENAME__

#define __FILENAME__   (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

strips leading path from FILE

Definition at line 38 of file logger.h.

◆ fLOG_DEBUG

#define fLOG_DEBUG (   fhl,
  msg 
)    fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__);

like LOG_DEBUG except for file logging

Definition at line 142 of file logger.h.

◆ fLOG_ERROR

#define fLOG_ERROR (   fhl,
  msg 
)    fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_ERROR, __FILENAME__, __LINE__);

like LOG_ERROR except for file logging

Definition at line 136 of file logger.h.

◆ fLOG_INFO

#define fLOG_INFO (   fhl,
  msg 
)    fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_INFO, __FILENAME__, __LINE__);

like LOG_INFO except for file logging

Definition at line 124 of file logger.h.

◆ fLOG_WARN

#define fLOG_WARN (   fhl,
  msg 
)    fhl->thl->log(fhl->thl, fhl->fd, msg, LOG_LEVELS_WARN, __FILENAME__, __LINE__);

like LOG_WARN except for file logging

Definition at line 130 of file logger.h.

◆ fLOGF_DEBUG

#define fLOGF_DEBUG (   fhl,
  msg,
  ... 
)    fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__, msg, __VA_ARGS__);

like LOGF_DEBUG except for file logging

Definition at line 166 of file logger.h.

◆ fLOGF_ERROR

#define fLOGF_ERROR (   fhl,
  msg,
  ... 
)    fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_ERROR, __FILENAME__, __LINE__, msg, __VA_ARGS__);

like LOGF_ERROR except for file logging

Definition at line 160 of file logger.h.

◆ fLOGF_INFO

#define fLOGF_INFO (   fhl,
  msg,
  ... 
)    fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_INFO, __FILENAME__, __LINE__, msg, __VA_ARGS__);

like LOGF_INFO except for file logging

Definition at line 148 of file logger.h.

◆ fLOGF_WARN

#define fLOGF_WARN (   fhl,
  msg,
  ... 
)    fhl->thl->logf(fhl->thl, fhl->fd, LOG_LEVELS_WARN, __FILENAME__, __LINE__, msg, __VA_ARGS__);

like LOGF_WARN except for file logging

Definition at line 154 of file logger.h.

◆ LOG_DEBUG

#define LOG_DEBUG (   thl,
  msg 
)    thl->log(thl, 0, msg, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__);

used to emit a standard DEBUG log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log
Note
if logger is created without debug enabled, this is a noop

Definition at line 74 of file logger.h.

◆ LOG_ERROR

#define LOG_ERROR (   thl,
  msg 
)    thl->log(thl, 0, msg, LOG_LEVELS_ERROR, __FILENAME__, __LINE__);

used to emit a standard ERROR log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log

Definition at line 64 of file logger.h.

◆ LOG_INFO

#define LOG_INFO (   thl,
  msg 
)    thl->log(thl, 0, msg, LOG_LEVELS_INFO, __FILENAME__, __LINE__);

used to emit a standard INFO log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log

Definition at line 46 of file logger.h.

◆ LOG_WARN

#define LOG_WARN (   thl,
  msg 
)    thl->log(thl, 0, msg, LOG_LEVELS_WARN, __FILENAME__, __LINE__);

used to emit a standard WARN log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log

Definition at line 55 of file logger.h.

◆ LOGF_DEBUG

#define LOGF_DEBUG (   thl,
  msg,
  ... 
)    thl->logf(thl, 0, LOG_LEVELS_DEBUG, __FILENAME__, __LINE__, msg, __VA_ARGS__);

used to emit a printf DEBUG log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe printf styled message to format
...the arguments to use for formatting
Note
if logger is created without debug enabled, this is a noop

Definition at line 118 of file logger.h.

◆ LOGF_ERROR

#define LOGF_ERROR (   thl,
  msg,
  ... 
)    thl->logf(thl, 0, LOG_LEVELS_ERROR, __FILENAME__, __LINE__, msg, __VA_ARGS__);

used to emit a printf ERROR log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log
msgthe printf styled message to format
...the arguments to use for formatting

Definition at line 107 of file logger.h.

◆ LOGF_INFO

#define LOGF_INFO (   thl,
  msg,
  ... 
)    thl->logf(thl, 0, LOG_LEVELS_INFO, __FILENAME__, __LINE__, msg, __VA_ARGS__);

used to emit a printf INFO log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log
msgthe printf styled message to format
...the arguments to use for formatting

Definition at line 85 of file logger.h.

◆ LOGF_WARN

#define LOGF_WARN (   thl,
  msg,
  ... 
)    thl->logf(thl, 0, LOG_LEVELS_WARN, __FILENAME__, __LINE__, msg, __VA_ARGS__);

used to emit a printf WARN log

Parameters
thlan instance of thread_logger, passing anything other than an initialized thread_logger will result in undefined benhavior
msgthe actual message to log
msgthe printf styled message to format
...the arguments to use for formatting

Definition at line 96 of file logger.h.

Typedef Documentation

◆ file_logger

typedef struct file_logger file_logger

◆ log_fn

typedef void(* log_fn) (struct thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level, char *file, int line)

Definition at line 215 of file logger.h.

◆ log_fnf

typedef void(* log_fnf) (struct thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *file, int line, char *message,...)

Definition at line 240 of file logger.h.

◆ mutex_fn

typedef int(* mutex_fn) (pthread_mutex_t *mx)

Definition at line 194 of file logger.h.

◆ thread_logger

typedef struct thread_logger thread_logger

Enumeration Type Documentation

◆ LOG_LEVELS

enum LOG_LEVELS
Enumerator
LOG_LEVELS_INFO 

indicates the message we are logging is of type info (color green)

LOG_LEVELS_WARN 

indicates the message we are logging is of type warn (color yellow)

LOG_LEVELS_ERROR 

indicates the message we are logging is of type error (color red)

LOG_LEVELS_DEBUG 

indicates the message we are logging is of type debug (color soft red)

Definition at line 179 of file logger.h.

Function Documentation

◆ clear_file_logger()

void clear_file_logger ( file_logger fhl)

free resources for the file ogger

Parameters
fhlthe file_logger instance to free memory for. also frees memory for the embedded thread_logger and closes the open file

Definition at line 333 of file logger.c.

Here is the call graph for this function:

◆ clear_thread_logger()

void clear_thread_logger ( thread_logger thl)

free resources for the threaded logger

Parameters
thlthe thread_logger instance to free memory for

Definition at line 322 of file logger.c.

Here is the caller graph for this function:

◆ debug_log()

void debug_log ( thread_logger thl,
int  file_descriptor,
char *  message 
)

logs a debug styled message - called by log_fn

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to in addition to stdout logging. if 0 only stdout is used
messagethe actuall message to log

Definition at line 294 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ error_log()

void error_log ( thread_logger thl,
int  file_descriptor,
char *  message 
)

logs an error styled message - called by log_fn

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to in addition to stdout logging. if 0 only stdout is used
messagethe actuall message to log

Definition at line 267 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_time_string()

void get_time_string ( char *  date_buffer,
size_t  date_buffer_len 
)

returns a timestamp of format Jul 06 10:12:20 PM

Warning
providing an input buffer whose length isnt at least 76 bytes will result in undefined behavior
Parameters
date_bufferthe buffer to write the timestamp into
date_buffer_lenthe size of the buffer

Definition at line 346 of file logger.c.

Here is the caller graph for this function:

◆ info_log()

void info_log ( thread_logger thl,
int  file_descriptor,
char *  message 
)

logs an info styled message - called by log_fn

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to in addition to stdout logging. if 0 only stdout is used
messagethe actuall message to log

Definition at line 213 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_func()

void log_func ( thread_logger thl,
int  file_descriptor,
char *  message,
LOG_LEVELS  level,
char *  file,
int  line 
)

main function you should call, which will delegate to the appopriate *_log function

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to, if 0 then only stdout is used
messagethe actual message we want to log
levelthe log level to use (effects color used)

Definition at line 168 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ logf_func()

void logf_func ( thread_logger thl,
int  file_descriptor,
LOG_LEVELS  level,
char *  file,
int  line,
char *  message,
  ... 
)

like log_func but for formatted logs

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to, if 0 then only stdout is used
levelthe log level to use (effects color used)
messageformat string like <percent-sign>sFOO<percent-sign>sBAR
...values to supply to message

Definition at line 142 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_file_logger()

file_logger* new_file_logger ( char *  output_file,
bool  with_debug 
)

returns a new file_logger Calls new_thread_logger internally

Parameters
output_filethe file we will dump logs to. created if not exists and is appended to

Definition at line 70 of file logger.c.

Here is the call graph for this function:

◆ new_thread_logger()

thread_logger* new_thread_logger ( bool  with_debug)

returns a new thread safe logger if with_debug is false, then all debug_log calls will be ignored

Parameters
with_debugwhether to enable debug logging, if false debug log calls will be ignored

Definition at line 47 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_log()

void warn_log ( thread_logger thl,
int  file_descriptor,
char *  message 
)

logs a warned styled message - called by log_fn

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to in addition to stdout logging. if 0 only stdout is used
messagethe actuall message to log

Definition at line 240 of file logger.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_file_log()

int write_file_log ( int  file_descriptor,
char *  message 
)

used to write a log message to file although this really means a file descriptor

Parameters
thlpointer to an instance of thread_logger
file_descriptorfile descriptor to write log messages to in addition to stdout logging. if 0 only stdout is used
messagethe actuall message to log

Definition at line 112 of file logger.c.

Here is the caller graph for this function: