summaryrefslogtreecommitdiff
path: root/common/gdm-log.c
blob: 22d8c7d68c0ea69b72d96a67d0a27fbf6210abc7 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

#include <syslog.h>
#ifdef WITH_SYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include <glib.h>
#include <glib/gstdio.h>

#include "gdm-log.h"

static gboolean initialized = FALSE;
static gboolean is_sd_booted = FALSE;
static gboolean debug_enabled = FALSE;

static gint
get_syslog_priority_from_log_level (GLogLevelFlags log_level)
{
        switch (log_level & G_LOG_LEVEL_MASK) {
        case G_LOG_FLAG_FATAL:
                return LOG_EMERG;
        case G_LOG_LEVEL_ERROR:
                /* fatal error - a critical error, in the syslog world */
                return LOG_CRIT;
        case G_LOG_LEVEL_CRITICAL:
                /* critical warning - an error, in the syslog world */
                return LOG_ERR;
        case G_LOG_LEVEL_WARNING:
        case G_LOG_LEVEL_MESSAGE:
                return LOG_NOTICE;
        case G_LOG_LEVEL_INFO:
                return LOG_INFO;
        case G_LOG_LEVEL_DEBUG:
        default:
                return LOG_DEBUG;
        }
}

static void
gdm_log_default_handler (const gchar    *log_domain,
                         GLogLevelFlags  log_level,
                         const gchar    *message,
                         gpointer        unused_data)
{
        int priority;

        gdm_log_init ();

        if ((log_level & G_LOG_LEVEL_MASK) == G_LOG_LEVEL_DEBUG &&
            !debug_enabled) {
                return;
        }

        /* Process the message prefix and priority */
        priority = get_syslog_priority_from_log_level (log_level);

        if (is_sd_booted) {
                fprintf (stderr,
                         "<%d>%s%s%s\n",
                         priority,
                         log_domain != NULL? log_domain : "",
                         log_domain != NULL? ": " : "",
                         message);
                fflush (stderr);
        } else {
                syslog (priority,
                        "%s%s%s\n",
                        log_domain != NULL? log_domain : "",
                        log_domain != NULL? ": " : "",
                        message);
        }
}

void
gdm_log_toggle_debug (void)
{
        gdm_log_set_debug (!debug_enabled);
}

void
gdm_log_set_debug (gboolean debug)
{
        g_assert (initialized);
        if (debug_enabled == debug) {
                return;
        }

        if (debug) {
                debug_enabled = debug;
                g_debug ("Enabling debugging");
        } else {
                g_debug ("Disabling debugging");
                debug_enabled = debug;
        }
}

void
gdm_log_init (void)
{
        const char *prg_name;
        int         options;

        if (initialized)
                return;

        initialized = TRUE;

#ifdef WITH_SYSTEMD
        is_sd_booted = sd_booted () > 0;
#endif

        g_log_set_default_handler (gdm_log_default_handler, NULL);

        /* Only set up syslog if !systemd, otherwise with systemd
         * enabled, we keep the default GLib log handler which goes to
         * stderr, which is routed to the appropriate place in the
         * systemd service file.
         */
        if (!is_sd_booted) {
                prg_name = g_get_prgname ();

                options = LOG_PID;
#ifdef LOG_PERROR
                options |= LOG_PERROR;
#endif

                openlog (prg_name, options, LOG_DAEMON);
        }
}

void
gdm_log_shutdown (void)
{
        if (!initialized)
                return;
        if (!is_sd_booted)
                closelog ();
        initialized = FALSE;
}