summaryrefslogtreecommitdiffstats
path: root/lib/Utils/logging.cpp
blob: cae609bc289517fa371b16021a356813c9d767f8 (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
/*
 * Utility routines.
 *
 * Licensed under GPLv2, see file COPYING in this tarball for details.
 */
#include "abrtlib.h"
#include <syslog.h>

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

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 = strlen(msg_prefix);
	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) {
		memmove(msg + prefix_len, msg, used);
		used += prefix_len;
		memcpy(msg, msg_prefix, prefix_len);
	}
	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);
	}
	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_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 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 simple_perror_msg_and_die(const char *s)
{
	perror_msg_and_die("%s", s);
}

void simple_perror_msg(const char *s)
{
	perror_msg("%s", s);
}

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