aboutsummaryrefslogblamecommitdiff
path: root/src/log.c
blob: f4ff6183ae1430950a319353687594b669782d92 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                                                        
                   

                                                                                        
                   
                   
                  







                                                                                        

                                                                          

                                                                                        
                   
                                 
                                        
 














                                                                                                                    
                     
                 









                                                                                        






                                                    
                     
                 

                                                        















                                                                                        
                     


















                                                                                        
                     


                                                         
/*
 * =====================================================================================
 *
 *       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 <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>


/* #####   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; 
    asprintf(&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;
    asprintf(&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;
    asprintf(&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;
    asprintf(&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  ----- */