1 |
|
|
/* $OpenBSD: log.c,v 1.12 2015/12/11 18:49:39 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* log.c |
5 |
|
|
* |
6 |
|
|
* Based on err.c, which was adapted from OpenBSD libc *err* *warn* code. |
7 |
|
|
* |
8 |
|
|
* Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net> |
9 |
|
|
* |
10 |
|
|
* Copyright (c) 2000 Dug Song <dugsong@monkey.org> |
11 |
|
|
* |
12 |
|
|
* Copyright (c) 1993 |
13 |
|
|
* The Regents of the University of California. All rights reserved. |
14 |
|
|
* |
15 |
|
|
* Redistribution and use in source and binary forms, with or without |
16 |
|
|
* modification, are permitted provided that the following conditions |
17 |
|
|
* are met: |
18 |
|
|
* 1. Redistributions of source code must retain the above copyright |
19 |
|
|
* notice, this list of conditions and the following disclaimer. |
20 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
21 |
|
|
* notice, this list of conditions and the following disclaimer in the |
22 |
|
|
* documentation and/or other materials provided with the distribution. |
23 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
24 |
|
|
* may be used to endorse or promote products derived from this software |
25 |
|
|
* without specific prior written permission. |
26 |
|
|
* |
27 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
28 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
31 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
32 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
33 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
34 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
35 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
36 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
37 |
|
|
* SUCH DAMAGE. |
38 |
|
|
*/ |
39 |
|
|
|
40 |
|
|
#include <sys/types.h> |
41 |
|
|
#include <sys/time.h> |
42 |
|
|
|
43 |
|
|
#include <stdio.h> |
44 |
|
|
#include <stdlib.h> |
45 |
|
|
#include <stdarg.h> |
46 |
|
|
#include <string.h> |
47 |
|
|
#include <errno.h> |
48 |
|
|
|
49 |
|
|
#include "event.h" |
50 |
|
|
#include "log.h" |
51 |
|
|
|
52 |
|
|
static void _warn_helper(int severity, int log_errno, const char *fmt, |
53 |
|
|
va_list ap); |
54 |
|
|
static void event_log(int severity, const char *msg); |
55 |
|
|
|
56 |
|
|
void |
57 |
|
|
event_err(int eval, const char *fmt, ...) |
58 |
|
|
{ |
59 |
|
|
va_list ap; |
60 |
|
|
|
61 |
|
|
va_start(ap, fmt); |
62 |
|
|
_warn_helper(_EVENT_LOG_ERR, errno, fmt, ap); |
63 |
|
|
va_end(ap); |
64 |
|
|
exit(eval); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
void |
68 |
|
|
event_warn(const char *fmt, ...) |
69 |
|
|
{ |
70 |
|
|
va_list ap; |
71 |
|
|
|
72 |
|
|
va_start(ap, fmt); |
73 |
|
|
_warn_helper(_EVENT_LOG_WARN, errno, fmt, ap); |
74 |
|
|
va_end(ap); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
void |
78 |
|
|
event_errx(int eval, const char *fmt, ...) |
79 |
|
|
{ |
80 |
|
|
va_list ap; |
81 |
|
|
|
82 |
|
|
va_start(ap, fmt); |
83 |
|
|
_warn_helper(_EVENT_LOG_ERR, -1, fmt, ap); |
84 |
|
|
va_end(ap); |
85 |
|
|
exit(eval); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
void |
89 |
|
|
event_warnx(const char *fmt, ...) |
90 |
|
|
{ |
91 |
|
|
va_list ap; |
92 |
|
|
|
93 |
|
|
va_start(ap, fmt); |
94 |
|
|
_warn_helper(_EVENT_LOG_WARN, -1, fmt, ap); |
95 |
|
|
va_end(ap); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
void |
99 |
|
|
event_msgx(const char *fmt, ...) |
100 |
|
|
{ |
101 |
|
|
va_list ap; |
102 |
|
|
|
103 |
|
|
va_start(ap, fmt); |
104 |
|
|
_warn_helper(_EVENT_LOG_MSG, -1, fmt, ap); |
105 |
|
|
va_end(ap); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
void |
109 |
|
|
_event_debugx(const char *fmt, ...) |
110 |
|
|
{ |
111 |
|
|
va_list ap; |
112 |
|
|
|
113 |
|
|
va_start(ap, fmt); |
114 |
|
|
_warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap); |
115 |
|
|
va_end(ap); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
static void |
119 |
|
|
_warn_helper(int severity, int log_errno, const char *fmt, va_list ap) |
120 |
|
|
{ |
121 |
|
|
char buf[1024]; |
122 |
|
|
size_t len; |
123 |
|
|
|
124 |
|
|
if (fmt != NULL) |
125 |
|
|
vsnprintf(buf, sizeof(buf), fmt, ap); |
126 |
|
|
else |
127 |
|
|
buf[0] = '\0'; |
128 |
|
|
|
129 |
|
|
if (log_errno >= 0) { |
130 |
|
|
len = strlen(buf); |
131 |
|
|
if (len < sizeof(buf) - 3) { |
132 |
|
|
snprintf(buf + len, sizeof(buf) - len, ": %s", |
133 |
|
|
strerror(log_errno)); |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
event_log(severity, buf); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
static event_log_cb log_fn = NULL; |
141 |
|
|
|
142 |
|
|
void |
143 |
|
|
event_set_log_callback(event_log_cb cb) |
144 |
|
|
{ |
145 |
|
|
log_fn = cb; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
static void |
149 |
|
|
event_log(int severity, const char *msg) |
150 |
|
|
{ |
151 |
|
|
if (log_fn) |
152 |
|
|
log_fn(severity, msg); |
153 |
|
|
} |