17c62d872SWarner Losh
244099b7bSPaul Traina /*
344099b7bSPaul Traina * report() - calls syslog
444099b7bSPaul Traina */
544099b7bSPaul Traina
644099b7bSPaul Traina #include <stdarg.h>
744099b7bSPaul Traina
844099b7bSPaul Traina #include <stdio.h>
944099b7bSPaul Traina #include <syslog.h>
107c62d872SWarner Losh #include <string.h>
117c62d872SWarner Losh #include <errno.h>
1244099b7bSPaul Traina
1344099b7bSPaul Traina #include "report.h"
1444099b7bSPaul Traina
1544099b7bSPaul Traina #ifndef LOG_NDELAY
1644099b7bSPaul Traina #define LOG_NDELAY 0
1744099b7bSPaul Traina #endif
1844099b7bSPaul Traina #ifndef LOG_DAEMON
1944099b7bSPaul Traina #define LOG_DAEMON 0
2044099b7bSPaul Traina #endif
2144099b7bSPaul Traina #ifndef LOG_BOOTP
2244099b7bSPaul Traina #define LOG_BOOTP LOG_DAEMON
2344099b7bSPaul Traina #endif
2444099b7bSPaul Traina
2544099b7bSPaul Traina extern int debug;
2644099b7bSPaul Traina extern char *progname;
2744099b7bSPaul Traina
2844099b7bSPaul Traina /*
2944099b7bSPaul Traina * This is initialized so you get stderr until you call
3044099b7bSPaul Traina * report_init()
3144099b7bSPaul Traina */
3244099b7bSPaul Traina static int stderr_only = 1;
3344099b7bSPaul Traina
3444099b7bSPaul Traina void
report_init(int nolog)35*8b356c88SJohn Baldwin report_init(int nolog)
3644099b7bSPaul Traina {
3744099b7bSPaul Traina stderr_only = nolog;
3844099b7bSPaul Traina #ifdef SYSLOG
3944099b7bSPaul Traina if (!stderr_only) {
4044099b7bSPaul Traina openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
4144099b7bSPaul Traina }
4244099b7bSPaul Traina #endif
4344099b7bSPaul Traina }
4444099b7bSPaul Traina
4544099b7bSPaul Traina /*
4644099b7bSPaul Traina * This routine reports errors and such via stderr and syslog() if
4744099b7bSPaul Traina * appopriate. It just helps avoid a lot of "#ifdef SYSLOG" constructs
4844099b7bSPaul Traina * from being scattered throughout the code.
4944099b7bSPaul Traina *
5044099b7bSPaul Traina * The syntax is identical to syslog(3), but %m is not considered special
5144099b7bSPaul Traina * for output to stderr (i.e. you'll see "%m" in the output. . .). Also,
5244099b7bSPaul Traina * control strings should normally end with \n since newlines aren't
5344099b7bSPaul Traina * automatically generated for stderr output (whereas syslog strips out all
5444099b7bSPaul Traina * newlines and adds its own at the end).
5544099b7bSPaul Traina */
5644099b7bSPaul Traina
5744099b7bSPaul Traina static char *levelnames[] = {
5844099b7bSPaul Traina #ifdef LOG_SALERT
5944099b7bSPaul Traina "level(0): ",
6044099b7bSPaul Traina "alert(1): ",
6144099b7bSPaul Traina "alert(2): ",
6244099b7bSPaul Traina "emerg(3): ",
6344099b7bSPaul Traina "error(4): ",
6444099b7bSPaul Traina "crit(5): ",
6544099b7bSPaul Traina "warn(6): ",
6644099b7bSPaul Traina "note(7): ",
6744099b7bSPaul Traina "info(8): ",
6844099b7bSPaul Traina "debug(9): ",
6944099b7bSPaul Traina "level(?): "
7044099b7bSPaul Traina #else
7144099b7bSPaul Traina "emerg(0): ",
7244099b7bSPaul Traina "alert(1): ",
7344099b7bSPaul Traina "crit(2): ",
7444099b7bSPaul Traina "error(3): ",
7544099b7bSPaul Traina "warn(4): ",
7644099b7bSPaul Traina "note(5): ",
7744099b7bSPaul Traina "info(6): ",
7844099b7bSPaul Traina "debug(7): ",
7944099b7bSPaul Traina "level(?): "
8044099b7bSPaul Traina #endif
8144099b7bSPaul Traina };
8244099b7bSPaul Traina static int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
8344099b7bSPaul Traina
8444099b7bSPaul Traina
8544099b7bSPaul Traina /*
8644099b7bSPaul Traina * Print a log message using syslog(3) and/or stderr.
8744099b7bSPaul Traina * The message passed in should not include a newline.
8844099b7bSPaul Traina */
8944099b7bSPaul Traina void
report(int priority,const char * fmt,...)90a16f3123SKris Kennaway report(int priority, const char *fmt,...)
9144099b7bSPaul Traina {
9244099b7bSPaul Traina va_list ap;
9344099b7bSPaul Traina static char buf[128];
9444099b7bSPaul Traina
9544099b7bSPaul Traina if ((priority < 0) || (priority >= numlevels)) {
9644099b7bSPaul Traina priority = numlevels - 1;
9744099b7bSPaul Traina }
9844099b7bSPaul Traina va_start(ap, fmt);
999bc34f7cSWarner Losh vsnprintf(buf, sizeof(buf), fmt, ap);
10044099b7bSPaul Traina va_end(ap);
10144099b7bSPaul Traina
10244099b7bSPaul Traina /*
10344099b7bSPaul Traina * Print the message
10444099b7bSPaul Traina */
10544099b7bSPaul Traina if (stderr_only || (debug > 2)) {
10644099b7bSPaul Traina fprintf(stderr, "%s: %s %s\n",
10744099b7bSPaul Traina progname, levelnames[priority], buf);
10844099b7bSPaul Traina }
10944099b7bSPaul Traina #ifdef SYSLOG
11044099b7bSPaul Traina if (!stderr_only)
11144099b7bSPaul Traina syslog((priority | LOG_BOOTP), "%s", buf);
11244099b7bSPaul Traina #endif
11344099b7bSPaul Traina }
11444099b7bSPaul Traina
11544099b7bSPaul Traina
11644099b7bSPaul Traina
11744099b7bSPaul Traina /*
11844099b7bSPaul Traina * Return pointer to static string which gives full filesystem error message.
11944099b7bSPaul Traina */
120a16f3123SKris Kennaway const char *
get_errmsg(void)121*8b356c88SJohn Baldwin get_errmsg(void)
12244099b7bSPaul Traina {
12344099b7bSPaul Traina return strerror(errno);
12444099b7bSPaul Traina }
12544099b7bSPaul Traina
12644099b7bSPaul Traina /*
12744099b7bSPaul Traina * Local Variables:
12844099b7bSPaul Traina * tab-width: 4
12944099b7bSPaul Traina * c-indent-level: 4
13044099b7bSPaul Traina * c-argdecl-indent: 4
13144099b7bSPaul Traina * c-continued-statement-offset: 4
13244099b7bSPaul Traina * c-continued-brace-offset: -4
13344099b7bSPaul Traina * c-label-offset: -4
13444099b7bSPaul Traina * c-brace-offset: 0
13544099b7bSPaul Traina * End:
13644099b7bSPaul Traina */
137