aboutsummaryrefslogtreecommitdiff
path: root/src/logger.c
blob: a54e0b88bd74bfdb7b53e12efe2f6cedb1fa1082 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* This file is part of SLB
   Copyright (C) 2011, 2019 Sergey Poznyakoff

   SLB 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 3, or (at your option)
   any later version.

   SLB 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 SLB.  If not, see <http://www.gnu.org/licenses/>. */

#include "slb.h"
#include "grecs/locus.h"

int log_to_stderr = -1; /* -1 means autodetect */
int syslog_facility = LOG_DAEMON;
const char *syslog_tag = NULL;
int syslog_include_prio; /* syslog messages include priority */

struct log_trans {
	char *kw;
	int tok;
};

static struct log_trans facility_trans[] = {
	{ "USER",    LOG_USER },   
	{ "DAEMON",  LOG_DAEMON },
	{ "AUTH",    LOG_AUTH },
	{ "AUTHPRIV",LOG_AUTHPRIV },
	{ "MAIL",    LOG_MAIL },
	{ "CRON",    LOG_CRON },
	{ "LOCAL0",  LOG_LOCAL0 },
	{ "LOCAL1",  LOG_LOCAL1 },
	{ "LOCAL2",  LOG_LOCAL2 },
	{ "LOCAL3",  LOG_LOCAL3 },
	{ "LOCAL4",  LOG_LOCAL4 },
	{ "LOCAL5",  LOG_LOCAL5 },
	{ "LOCAL6",  LOG_LOCAL6 },
	{ "LOCAL7",  LOG_LOCAL7 },
	{ NULL }
};

static int
syslog_to_n(struct log_trans *trans, const char *str, int *pint)
{
	int i;

	for (i = 0; trans[i].kw; i++) {
		if (strcasecmp(trans[i].kw, str) == 0) {
			*pint = trans[i].tok;
			return 0;
		}
	}
	return 1;
}

static int
n_to_syslog(struct log_trans *trans, int tok, const char **pstr)
{
	int i;

	for (i = 0; trans[i].kw; i++) {
		if (trans[i].tok == tok) {
			*pstr = trans[i].kw;
			return 0;
		}
	}
	return 1;
}

static struct log_trans prio_trans[] = {
	{ "EMERG", LOG_EMERG },
	{ "ALERT", LOG_ALERT },
	{ "CRIT", LOG_CRIT },
	{ "ERR", LOG_ERR },
	{ "WARNING", LOG_WARNING },
	{ "NOTICE", LOG_NOTICE },
	{ "INFO", LOG_INFO },
	{ "DEBUG", LOG_DEBUG },
	{ NULL }
};

int
string_to_syslog_facility(const char *str, int *pfacility)
{
	return syslog_to_n(facility_trans, str, pfacility);
}

/* Logging */
void
syslog_printer(int prio, grecs_locus_t const *locus, const char *fmt,
	       va_list ap)
{
	static char *fmtbuf;
	static size_t fmtsize;
	char *locstr = NULL;
	char *pfxbuf = NULL;
	const char *p;
	
	if (locus) {
		size_t size = 0;

		if (locus->beg.col == 0)
			grecs_asprintf(&locstr, &size, "%s:%u",
				       locus->beg.file,
				       locus->beg.line);
		else if (strcmp(locus->beg.file, locus->end.file))
			grecs_asprintf(&locstr, &size, "%s:%u.%u-%s:%u.%u",
				       locus->beg.file,
				       locus->beg.line, locus->beg.col,
				       locus->end.file,
				       locus->end.line, locus->end.col);
		else if (locus->beg.line != locus->end.line)
			grecs_asprintf(&locstr, &size, "%s:%u.%u-%u.%u",
				       locus->beg.file,
				       locus->beg.line, locus->beg.col,
				       locus->end.line, locus->end.col);
		else
			grecs_asprintf(&locstr, &size, "%s:%u.%u-%u",
				       locus->beg.file,
				       locus->beg.line, locus->beg.col,
				       locus->end.col);
	}

	if (syslog_include_prio && n_to_syslog(prio_trans, prio, &p) == 0) {
		size_t size = 0;

		if (locstr) {
			grecs_asprintf(&pfxbuf, &size, "%s: [%s]",
				       locstr, p);
			free(locstr);
			locstr = NULL;
		} else
			grecs_asprintf(&pfxbuf, &size, "[%s]",
				       p);
	} else
		pfxbuf = locstr;

	grecs_vasprintf(&fmtbuf, &fmtsize, fmt, ap); 

	if (pfxbuf) {
		syslog(prio, "%s: %s", pfxbuf, fmtbuf);
		free(pfxbuf);
	} else
		syslog(prio, "%s", fmtbuf);
}

void
stderr_printer(int prio, grecs_locus_t const *locus, const char *fmt,
	       va_list ap)
{
	const char *p;
	fprintf (stderr, "%s: ", syslog_tag);

	if (locus) {
		YY_LOCATION_PRINT(stderr, *locus);
		fprintf(stderr, ": ");
	}
	if (n_to_syslog(prio_trans, prio, &p) == 0)
		fprintf(stderr, "[%s] ", p);
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
}

static void (*log_printer)(int prio, grecs_locus_t const *locus,
			   const char *fmt, va_list ap) = stderr_printer;

void
logger_setup()
{
	if (!syslog_tag) {
		syslog_tag = strrchr(program_name, '/');
		if (syslog_tag)
			syslog_tag++;
		else
			syslog_tag = program_name;
	}
	log_printer = log_to_stderr ? stderr_printer : syslog_printer;
	if (log_to_stderr)
		log_printer = stderr_printer;
	else {
		log_printer = syslog_printer;
		openlog(syslog_tag, LOG_PID, syslog_facility);
	}
	grecs_log_to_stderr = log_to_stderr;
}

void
vlogmsg(int prio, const char *fmt, va_list ap)
{
	log_printer(prio, NULL, fmt, ap);
}

void
logmsg(int prio, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	log_printer(prio, NULL, fmt, ap);
	va_end(ap);
}

void
logmsg_at_locus(int prio, grecs_locus_t const *locus, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	log_printer(prio, locus, fmt, ap);
	va_end(ap);
}

void
debug_printf(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	log_printer(LOG_DEBUG, NULL, fmt, ap);
	va_end(ap);
}

Return to:

Send suggestions and report system problems to the System administrator.