]> git.sesse.net Git - vlc/blob - modules/logger/journal.c
qt4: fix COM leak and handle errors
[vlc] / modules / logger / journal.c
1 /*****************************************************************************
2  * journal.c: SystemD journal logger plugin
3  *****************************************************************************
4  * Copyright © 2015 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdarg.h>
26 #include <inttypes.h>
27 #include <syslog.h>
28 #include <systemd/sd-journal.h>
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32
33 static const int priorities[4] = {
34     [VLC_MSG_INFO] = LOG_INFO,
35     [VLC_MSG_ERR]  = LOG_ERR,
36     [VLC_MSG_WARN] = LOG_WARNING,
37     [VLC_MSG_DBG]  = LOG_DEBUG,
38 };
39
40 static void Log(void *opaque, int type, const vlc_log_t *meta,
41                 const char *format, va_list ap)
42 {
43     static const char default_msg[] = "message lost";
44     char *msg;
45     int canc = vlc_savecancel();
46
47     if (vasprintf(&msg, format, ap) == -1)
48         msg = (char *)default_msg;
49
50     sd_journal_send("MESSAGE=%s", msg,
51         "PRIORITY=%d", priorities[type],
52         "CODE_FILE=%s", (meta->file != NULL) ? meta->file : "",
53         "CODE_LINE=%u", meta->line,
54         "CODE_FUNC=%s", (meta->func != NULL) ? meta->func : "",
55         //"ERRNO=%d"
56         "VLC_OBJECT_ID=%"PRIxPTR, meta->i_object_id,
57         "VLC_OBJECT_TYPE=%s", meta->psz_object_type,
58         "VLC_MODULE=%s", meta->psz_module,
59         "VLC_HEADER=%s", (meta->psz_header != NULL) ? meta->psz_header : "",
60         NULL);
61
62     vlc_restorecancel(canc);
63
64     if (msg != default_msg)
65         free(msg);
66     (void) opaque;
67 }
68
69 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
70 {
71     if (!var_InheritBool(obj, "syslog"))
72         return NULL;
73
74     (void) sysp;
75     return Log;
76 }
77
78 vlc_module_begin()
79     set_shortname(N_("Journal"))
80     set_description(N_("SystemD journal logger"))
81     set_category(CAT_ADVANCED)
82     set_subcategory(SUBCAT_ADVANCED_MISC)
83     set_capability("logger", 30)
84     set_callbacks(Open, NULL)
85     add_shortcut("journal")
86 vlc_module_end()