-
Notifications
You must be signed in to change notification settings - Fork 8
/
logging.c
124 lines (107 loc) · 3.15 KB
/
logging.c
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
119
120
121
122
123
124
/* This file is part of sailsd
*
* Copyright (C) 2014-2018 Louis Taylor <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "logging.h"
/* log level */
enum log_level current_log_level = INFO;
/* print a giant boat to the screen */
void put_boat(void)
{
puts(" \n"
" ,\x1b[31m~\x1b[0m \n"
" |\\ \n"
" /| \\ \n"
" \x1b[34m~ ^~ \x1b[0m/_|__\\\x1b[34m~^~ \x1b[0m\n"
" \x1b[34m~^~^ ~ \x1b[0m'======' \x1b[34m~^ ~^~ \x1b[0m\n"
" \x1b[34m~^ ^~ ~^ ~\x1b[0m_\x1b[34m~\x1b[0m_\x1b[34m~^~ ~^~\x1b[0m_\x1b[34m ~^~ \x1b[0m\n"
" \x1b[34m~^~ \x1b[0m___\x1b[34m^\x1b[0m___|_| |___\x1b[34m~\x1b[0m_| | \x1b[34m ^~ \x1b[0m\n"
" \x1b[34m~ \x1b[0m|_ -| .'| | |_ -| . |\x1b[34m ~ \x1b[0m\n"
" |___|__,|_|_|___|___| \n");
}
/* logging function in the vein of vprintf, taking a va_list of arguments */
static void vlog_msg(const enum log_level level,
const char *format,
va_list argp)
{
char *level_str = "";
switch(level) {
case ERROR:
level_str = COLOR_RED "error" COLOR_RESET;
break;
case WARNING:
level_str = COLOR_YELLOW "warning" COLOR_RESET;
break;
case INFO:
level_str = COLOR_BLUE "info" COLOR_RESET;
break;
case DEBUG:
level_str = "debug";
break;
}
char timestamp[32];
time_t t = time(NULL);
struct tm *p = localtime(&t);
strftime(timestamp, 32, "%c", p);
printf("[%s] %s:\t", timestamp, level_str);
vprintf(format, argp);
printf("\n");
}
/* static void log_msg(const enum log_level level, const char *format, ...) {
va_list arglist;
va_start(arglist, format);
vlog_msg(level, format, arglist);
va_end(arglist);
} */
void log_info(const char *format, ...)
{
if (current_log_level < INFO)
return;
va_list arglist;
va_start(arglist, format);
vlog_msg(INFO, format, arglist);
va_end(arglist);
}
void log_error(const char *format, ...)
{
if (current_log_level < ERROR)
return;
va_list arglist;
va_start(arglist, format);
vlog_msg(ERROR, format, arglist);
va_end(arglist);
}
void log_warning(const char *format, ...)
{
if (current_log_level < WARNING)
return;
va_list arglist;
va_start(arglist, format);
vlog_msg(WARNING, format, arglist);
va_end(arglist);
}
void log_debug(const char *format, ...)
{
if (current_log_level < DEBUG)
return;
va_list arglist;
va_start(arglist, format);
vlog_msg(DEBUG, format, arglist);
va_end(arglist);
}