File: syslog.c

package info (click to toggle)
smcroute 2.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 636 kB
  • ctags: 221
  • sloc: ansic: 2,296; sh: 1,310; perl: 161; makefile: 61
file content (81 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download | duplicates (2)
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
/* System logging API
 *
 * Copyright (C) 2001-2005  Carsten Schill <carsten@cschill.de>
 * Copyright (C) 2006-2009  Julien BLACHE <jb@jblache.org>
 * Copyright (C) 2009       Todd Hayton <todd.hayton@gmail.com>
 * Copyright (C) 2009-2011  Micha Lenk <micha@debian.org>
 * Copyright (C) 2011-2013  Joachim Nilsson <troglobit@gmail.com>
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "mclab.h"

int  log_stderr = LOG_WARNING;
int  log_last_severity;
int  log_last_error;
char log_last_message[128];

/**
 * smclog - Log message to syslog and stderr
 * @severity: Standard syslog() severity levels or %LOG_INIT
 * @code:     Error code, @errno, or zero if unused
 * @fmt:      Standard printf() formatted message to log
 *
 * Logs a standard printf() formatted message to syslog and stderr when
 * @severity is greater than the @log_stderr threshold.  When @code is
 * set it is appended to the log, along with the error message.
 *
 * When @severity is %LOG_ERR or worse this function will call exit().
 */
void smclog(int severity, int code, const char *fmt, ...)
{
	int len = 0;
	va_list args;
	const char *err = (code <= 0) ? NULL : (const char *)strerror(code);

	/* Skip logging for severities 'DEBUG' if do_debug_logging is false */
	if (severity == LOG_DEBUG && !do_debug_logging)
		return;

	va_start(args, fmt);
	len += vsnprintf(log_last_message + len, sizeof(log_last_message) - len, fmt, args);
	if (err)
		snprintf(log_last_message + len, sizeof(log_last_message) - len, ". Error %d: %s", code, err);
	va_end(args);

	/* update our global Last... variables */
	log_last_severity = severity;
	log_last_error = code;

	/* control logging to stderr */
	if (severity < log_stderr || severity == LOG_INIT)
		fprintf(stderr, "%s\n", log_last_message);

	/* always to syslog */
	syslog((severity == LOG_INIT) ? LOG_ERR : severity, "%s", log_last_message);

	/* LOG_INIT doesn't trigger that */
	if (severity <= LOG_ERR)
		exit(255);
}

/**
 * Local Variables:
 *  version-control: t
 *  indent-tabs-mode: t
 *  c-file-style: "linux"
 * End:
 */