summaryrefslogtreecommitdiffstats
path: root/src/lib/logging.c
blob: 815efb5ec9e6b769f3bdc3ecc84e398939bc7184 (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
/*
    Copyright (C) 2010  ABRT team
    Copyright (C) 2010  RedHat Inc

    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.
*/

/*
 * Utility routines.
 *
 */
#include "abrtlib.h"

void (*g_custom_logger)(const char*);
const char *msg_prefix = "";
const char *msg_eol = "\n";
int logmode = LOGMODE_STDIO;
int xfunc_error_retval = EXIT_FAILURE;
int g_verbose;

void xfunc_die(void)
{
    exit(xfunc_error_retval);
}

static void verror_msg_helper(const char *s,
                              va_list p,
                              const char* strerr,
                              int flags)
{
    char *msg;
    int prefix_len, strerr_len, msgeol_len, used;

    if (!logmode)
        return;

    used = vasprintf(&msg, s, p);
    if (used < 0)
        return;

    /* This is ugly and costs +60 bytes compared to multiple
     * fprintf's, but is guaranteed to do a single write.
     * This is needed for e.g. when multiple children
     * can produce log messages simultaneously. */

    prefix_len = msg_prefix[0] ? strlen(msg_prefix) + 2 : 0;
    strerr_len = strerr ? strlen(strerr) : 0;
    msgeol_len = strlen(msg_eol);
    /* +3 is for ": " before strerr and for terminating NUL */
    msg = (char*) xrealloc(msg, prefix_len + used + strerr_len + msgeol_len + 3);
    /* TODO: maybe use writev instead of memmoving? Need full_writev? */
    if (prefix_len) {
        char *p;
        memmove(msg + prefix_len, msg, used);
        used += prefix_len;
        p = stpcpy(msg, msg_prefix);
        p[0] = ':';
        p[1] = ' ';
    }
    if (strerr) {
        if (s[0]) {
            msg[used++] = ':';
            msg[used++] = ' ';
        }
        strcpy(&msg[used], strerr);
        used += strerr_len;
    }
    strcpy(&msg[used], msg_eol);

    if (flags & LOGMODE_STDIO) {
        fflush(stdout);
        full_write(STDERR_FILENO, msg, used + msgeol_len);
    }
    msg[used] = '\0'; /* remove msg_eol (usually "\n") */
    if (flags & LOGMODE_SYSLOG) {
        syslog(LOG_ERR, "%s", msg + prefix_len);
    }
    if ((flags & LOGMODE_CUSTOM) && g_custom_logger) {
        g_custom_logger(msg + prefix_len);
    }
    free(msg);
}

void log_msg(const char *s, ...)
{
    va_list p;

    va_start(p, s);
    verror_msg_helper(s, p, NULL, logmode);
    va_end(p);
}

void error_msg(const char *s, ...)
{
    va_list p;

    va_start(p, s);
    verror_msg_helper(s, p, NULL, (logmode | LOGMODE_CUSTOM));
    va_end(p);
}

void error_msg_and_die(const char *s, ...)
{
    va_list p;

    va_start(p, s);
    verror_msg_helper(s, p, NULL, (logmode | LOGMODE_CUSTOM));
    va_end(p);
    xfunc_die();
}

void perror_msg(const char *s, ...)
{
    va_list p;

    va_start(p, s);
    /* Guard against "<error message>: Success" */
    verror_msg_helper(s, p, errno ? strerror(errno) : NULL, (logmode | LOGMODE_CUSTOM));
    va_end(p);
}

void perror_msg_and_die(const char *s, ...)
{
    va_list p;

    va_start(p, s);
    /* Guard against "<error message>: Success" */
    verror_msg_helper(s, p, errno ? strerror(errno) : NULL, (logmode | LOGMODE_CUSTOM));
    va_end(p);
    xfunc_die();
}

void die_out_of_memory(void)
{
    error_msg_and_die("Out of memory, exiting");
}