Download
ftech
/Logger
/logger.c
(View History)
Francois Techene Log to NONE if the log file cannot be created. | Latest amendment: 4 on 2025-04-08 |
1 | /* logger.c |
2 | * |
3 | * Copyright 2025 Francois Techene |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | * |
18 | * SPDX-License-Identifier: GPL-3.0-or-later |
19 | */ |
20 | |
21 | #include <stdio.h> |
22 | #include <stdlib.h> |
23 | #include <stdarg.h> |
24 | |
25 | #include "logger.h" |
26 | |
27 | Logger logger; |
28 | |
29 | void |
30 | init_logger(LogLevel level, |
31 | LogOutputMethod out, |
32 | char* file_path) |
33 | { |
34 | logger.debug = log_debug; |
35 | logger.info = log_info; |
36 | logger.warn = log_warn; |
37 | logger.error = log_error; |
38 | |
39 | logger.log_level = level; |
40 | logger.output_method = out; |
41 | logger.file_path = file_path; |
42 | |
43 | if (out == CONSOLE_OUT) { |
44 | logger.log = log_to_console; |
45 | } |
46 | else if (out == FILE_OUT) { |
47 | FILE* file; |
48 | |
49 | logger.log = log_to_file; |
50 | |
51 | // Empty log file. If the file cannot be created, the log output |
52 | // is set to NONE_OUT; |
53 | // |
54 | file = fopen(logger.file_path, "w"); |
55 | if(!file) { |
56 | logger.output_method = NONE_OUT; |
57 | logger.log = log_to_none; |
58 | return; |
59 | } |
60 | fprintf(file, ""); |
61 | fclose(file); |
62 | } |
63 | else { |
64 | logger.log = log_to_none; |
65 | } |
66 | } |
67 | |
68 | void |
69 | log_to_none(char* format, ...) |
70 | { |
71 | return; |
72 | } |
73 | |
74 | void |
75 | log_to_console(char* format, ...) |
76 | { |
77 | va_list ap; |
78 | |
79 | va_start(ap, format); |
80 | vprintf(format, ap); |
81 | va_end(ap); |
82 | |
83 | printf("\n"); |
84 | } |
85 | void |
86 | log_to_file(char* format, ...) |
87 | { |
88 | FILE* file; |
89 | va_list ap; |
90 | |
91 | // Open file in Append Binary mode so the |
92 | // new line character is not converted. |
93 | // |
94 | file = fopen(logger.file_path, "ab"); |
95 | if(!file) { |
96 | return; |
97 | } |
98 | |
99 | va_start(ap, format); |
100 | vfprintf(file, format, ap); |
101 | va_end(ap); |
102 | |
103 | // Using Unix New Line character for tail to |
104 | // read correctly on Linux. |
105 | // |
106 | fprintf(file, " \x0A\r"); |
107 | |
108 | fclose(file); |
109 | } |
110 | |
111 | void |
112 | log_debug(char* format, ...) |
113 | { |
114 | va_list ap; |
115 | char log[256] = ""; |
116 | |
117 | if (logger.log_level < DEBUG_LEVEL) { |
118 | return; |
119 | } |
120 | |
121 | va_start(ap, format); |
122 | vsprintf(log, format, ap); |
123 | va_end(ap); |
124 | |
125 | logger.log("DEBUG: %s", log); |
126 | } |
127 | |
128 | void |
129 | log_info(char* format, ...) |
130 | { |
131 | va_list ap; |
132 | char log[256] = ""; |
133 | |
134 | if (logger.log_level < INFO_LEVEL) { |
135 | return; |
136 | } |
137 | |
138 | va_start(ap, format); |
139 | vsprintf(log, format, ap); |
140 | va_end(ap); |
141 | |
142 | logger.log("INFO: %s", log); |
143 | } |
144 | |
145 | void |
146 | log_warn(char* format, ...) |
147 | { |
148 | va_list ap; |
149 | char log[256] = ""; |
150 | |
151 | if (logger.log_level < WARNING_LEVEL) { |
152 | return; |
153 | } |
154 | |
155 | va_start(ap, format); |
156 | vsprintf(log, format, ap); |
157 | va_end(ap); |
158 | |
159 | logger.log("WARNING: %s", log); |
160 | } |
161 | |
162 | void |
163 | log_error(char* format, ...) |
164 | { |
165 | va_list ap; |
166 | char log[256] = ""; |
167 | |
168 | if (logger.log_level < ERROR_LEVEL) { |
169 | return; |
170 | } |
171 | |
172 | va_start(ap, format); |
173 | vsprintf(log, format, ap); |
174 | va_end(ap); |
175 | |
176 | logger.log("ERROR: %s", log); |
177 | } |