/* * ===================================================================================== * * Filename: log.c * * Description: Implementations for logging framework * * Version: 1.0 * Created: 10/27/2022 10:02:48 AM * Revision: none * Compiler: gcc * * Author: Cara Salter (cara@devcara.com) * Organization: * * ===================================================================================== */ #define _GNU_SOURCE /* ##### HEADER FILE INCLUDES ################################################### */ #include #include #include #include /* ##### PROTOTYPES - LOCAL TO THIS SOURCE FILE ############################### */ void process_fmt(char* msg, va_list args); /* ##### FUNCTION DEFINITIONS - LOCAL TO THIS SOURCE FILE ##################### */ /* * === FUNCTION ====================================================================== * Name: print_date * Description: Prints the current date/time to stdout without a newline ===================================================================================== */ void print_date() { time_t cur_time = time(NULL); struct tm t = *localtime(&cur_time); printf("%d-%02d-%02d %02d:%02d:%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); } /* ----- end of function print_date ----- */ /* * === FUNCTION ====================================================================== * Name: debug * Description: Outputs debugging information to stdout * ===================================================================================== */ int debug(char* msg, ...) { va_list args; print_date(); va_start(args, msg); char *fmt_string; vasprintf(&fmt_string, msg, args); printf(" \033[34mDEBUG\033[0m %s\n", fmt_string); free(fmt_string); va_end(args); return 0; } /* * === FUNCTION ====================================================================== * Name: info * Description: Outputs informational messages to stdout * ===================================================================================== */ int info(char* msg, ...) { va_list args; print_date(); va_start(args, msg); char *fmt_string; vasprintf(&fmt_string, msg, args); printf(" \033[32mINFO\033[0m %s\n", fmt_string); free(fmt_string); va_end(args); return 0; } /* ----- end of function info ----- */ /* * === FUNCTION ====================================================================== * Name: warning * Description: Outputs a warning message to stdout * ===================================================================================== */ int warning(char* msg, ...) { va_list args; print_date(); va_start(args, msg); char *fmt_string; vasprintf(&fmt_string, msg, args); printf(" \033[33mWARNING\033[0m %s\n", fmt_string); free(fmt_string); va_end(args); return 0; } /* ----- end of function warning ----- */ /* * === FUNCTION ====================================================================== * Name: error * Description: Outputs an error to stdout and stderr * ===================================================================================== */ int error(char* msg, ...) { va_list args; print_date(); va_start(args, msg); char *fmt_string; vasprintf(&fmt_string, msg, args); printf(" \033[31mERROR\033[0m %s\n", fmt_string); fprintf(stderr, " \033[31mERROR\033[0m %s\n", fmt_string); free(fmt_string); va_end(args); return 0; } /* ----- end of function error ----- */