c-template
logger.c File Reference

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

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "../../include/utils/logger.h"
Include dependency graph for logger.c:

Go to the source code of this file.

Functions

char * get_time_string ()
 private function that returns a timestamp of format Jul 06 10:12:20 PM More...
 
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...
 
int write_file_log (int file_descriptor, char *message)
 used to write a log message to file More...
 
void logf_func (thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *message,...)
 like log_func but for formatted logs More...
 
void log_func (thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level)
 main function you should call, which will delegate to the appopriate *_log function More...
 
void info_log (thread_logger *thl, int file_descriptor, char *message)
 logs an info 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 debug_log (thread_logger *thl, int file_descriptor, char *message)
 logs a debug styled message - called by log_fn 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...
 

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.c.

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 228 of file logger.c.

Here is the call graph for this function:
Here is the caller 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 224 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 201 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 183 of file logger.c.

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

◆ get_time_string()

char * get_time_string ( )

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

Definition at line 234 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 143 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 
)

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 112 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 *  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 95 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 43 of file logger.c.

Here is the call graph for this function:
Here is the caller 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 28 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 161 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

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 73 of file logger.c.

Here is the caller graph for this function: