libfuse
fuse_log.c
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2019 Red Hat, Inc.
4
5 Logging API.
6
7 This program can be distributed under the terms of the GNU LGPLv2.
8 See the file COPYING.LIB
9*/
10
11#include "fuse_log.h"
12
13#include <stdarg.h>
14#include <stdio.h>
15#include <stdbool.h>
16#include <syslog.h>
17
18#define MAX_SYSLOG_LINE_LEN 512
19
20static bool to_syslog = false;
21
22static void default_log_func(__attribute__((unused)) enum fuse_log_level level,
23 const char *fmt, va_list ap)
24{
25 if (to_syslog) {
26 int sys_log_level = LOG_ERR;
27
28 /*
29 * with glibc fuse_log_level has identical values as
30 * syslog levels, but we also support BSD - better we convert to
31 * be sure.
32 */
33 switch (level) {
34 case FUSE_LOG_DEBUG:
35 sys_log_level = LOG_DEBUG;
36 break;
37 case FUSE_LOG_INFO:
38 sys_log_level = LOG_INFO;
39 break;
40 case FUSE_LOG_NOTICE:
41 sys_log_level = LOG_NOTICE;
42 break;
43 case FUSE_LOG_WARNING:
44 sys_log_level = LOG_WARNING;
45 break;
46 case FUSE_LOG_ERR:
47 sys_log_level = LOG_ERR;
48 break;
49 case FUSE_LOG_CRIT:
50 sys_log_level = LOG_CRIT;
51 break;
52 case FUSE_LOG_ALERT:
53 sys_log_level = LOG_ALERT;
54 break;
55 case FUSE_LOG_EMERG:
56 sys_log_level = LOG_EMERG;
57 }
58
59 char log[MAX_SYSLOG_LINE_LEN];
60 vsnprintf(log, MAX_SYSLOG_LINE_LEN, fmt, ap);
61 syslog(sys_log_level, "%s", log);
62 } else {
63 vfprintf(stderr, fmt, ap);
64 }
65}
66
67static fuse_log_func_t log_func = default_log_func;
68
70{
71 if (!func)
72 func = default_log_func;
73
74 log_func = func;
75}
76
77void fuse_log(enum fuse_log_level level, const char *fmt, ...)
78{
79 va_list ap;
80
81 va_start(ap, fmt);
82 log_func(level, fmt, ap);
83 va_end(ap);
84}
85
86void fuse_log_enable_syslog(const char *ident, int option, int facility)
87{
88 to_syslog = true;
89
90 openlog(ident, option, facility);
91}
92
94{
95 closelog();
96}
void fuse_log_close_syslog(void)
Definition fuse_log.c:93
void fuse_log(enum fuse_log_level level, const char *fmt,...)
Definition fuse_log.c:77
void(* fuse_log_func_t)(enum fuse_log_level level, const char *fmt, va_list ap)
Definition fuse_log.h:52
void fuse_log_enable_syslog(const char *ident, int option, int facility)
Definition fuse_log.c:86
fuse_log_level
Definition fuse_log.h:28
void fuse_set_log_func(fuse_log_func_t func)
Definition fuse_log.c:69