Debian Codegraph @

src/log.c

raw symbols SCIP
1 /*
2 * HTTPDirFS - HTTP Directory Filesystem
3 *
4 * Copyright (C) 2020-2026 Fufu Fang <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the OpenSSL
21 * library.
22 */
23
24 /**
25 * \file log.c
26 * \brief Logging system implementation
27 */
28
29 #include "log.h"
30
31 #include "config.h"
32 #include "util.h"
33
34 #include <curl/curl.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 int log_level_init(void)
41 {
42 char *env = getenv("HTTPDIRFS_LOG_LEVEL");
43 if (env) {
44 return (int)strtol(env, NULL, 10);
45 }
46 #ifdef DEBUG
47 return DEFAULT_LOG_LEVEL | debug;
48 #else
49 return DEFAULT_LOG_LEVEL;
50 #endif
51 }
52
53 static void __attribute__((format(printf, 5, 0)))
54 vlog_printf(LogType type, const char *file, const char *func, int line,
55 const char *format, va_list args)
56 {
57 switch (type) {
58 case fatal:
59 fprintf(stderr, "Fatal:");
60 break;
61 case error:
62 fprintf(stderr, "Error:");
63 break;
64 case warning:
65 fprintf(stderr, "Warning:");
66 break;
67 case info:
68 goto print_actual_message;
69 default:
70 fprintf(stderr, "Debug");
71 if (type != debug) {
72 fprintf(stderr, "(%x)", (unsigned int)type);
73 }
74 fprintf(stderr, ":");
75 break;
76 }
77
78 fprintf(stderr, "%s:%d:", file, line);
79
80 print_actual_message: {
81 }
82 fprintf(stderr, "%s: ", func);
83 vfprintf(stderr, format, args);
84 }
85
86 void log_printf(LogType type, const char *file, const char *func, int line,
87 const char *format, ...)
88 {
89 if (type & CONFIG.log_type) {
90 va_list args;
91 va_start(args, format);
92 vlog_printf(type, file, func, line, format, args);
93 va_end(args);
94 }
95 }
96
97 void fatal_log_printf(const char *file, const char *func, int line,
98 const char *format, ...)
99 {
100 va_list args;
101 va_start(args, format);
102 vlog_printf(fatal, file, func, line, format, args);
103 va_end(args);
104 exit_failure();
105 }
106
107 void print_version(void)
108 {
109 /* FUSE prints its help to stderr */
110 fprintf(stderr, "HTTPDirFS version " VERSION "\n");
111 /*
112 * --------- Print off SSL engine version ---------
113 */
114 curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
115 fprintf(stderr, "libcurl SSL engine: %s\n", data->ssl_version);
116 }