aboutsummaryrefslogtreecommitdiff
path: root/src/log.c
blob: f4ff6183ae1430950a319353687594b669782d92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * =====================================================================================
 *
 *       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  ----- */