| 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 |
#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 |
|
| 110 |
fprintf(stderr, "HTTPDirFS version " VERSION "\n"); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); |
| 115 |
fprintf(stderr, "libcurl SSL engine: %s\n", data->ssl_version); |
| 116 |
} |