]>
Commit | Line | Data |
---|---|---|
9e246ac3 CD |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license from the top-level | |
9 | * OPENSOLARIS.LICENSE or <http://opensource.org/licenses/CDDL-1.0>. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each file | |
14 | * and include the License file from the top-level OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049). | |
24 | * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC. | |
25 | */ | |
26 | ||
27 | #include <assert.h> | |
28 | #include <stdarg.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <syslog.h> | |
33 | #include "zed_log.h" | |
34 | ||
35 | #define ZED_LOG_MAX_ID_LEN 64 | |
36 | #define ZED_LOG_MAX_LOG_LEN 1024 | |
37 | ||
38 | static struct { | |
39 | unsigned do_stderr:1; | |
40 | unsigned do_syslog:1; | |
41 | int level; | |
42 | char id[ZED_LOG_MAX_ID_LEN]; | |
43 | } _ctx; | |
44 | ||
45 | void | |
46 | zed_log_init(const char *identity) | |
47 | { | |
48 | const char *p; | |
49 | ||
50 | if (identity) { | |
51 | p = (p = strrchr(identity, '/')) ? p + 1 : identity; | |
52 | strlcpy(_ctx.id, p, sizeof (_ctx.id)); | |
53 | } else { | |
54 | _ctx.id[0] = '\0'; | |
55 | } | |
56 | } | |
57 | ||
58 | void | |
59 | zed_log_fini() | |
60 | { | |
61 | if (_ctx.do_syslog) { | |
62 | closelog(); | |
63 | } | |
64 | } | |
65 | ||
66 | void | |
67 | zed_log_stderr_open(int level) | |
68 | { | |
69 | _ctx.do_stderr = 1; | |
70 | _ctx.level = level; | |
71 | } | |
72 | ||
73 | void | |
74 | zed_log_stderr_close(void) | |
75 | { | |
76 | _ctx.do_stderr = 0; | |
77 | } | |
78 | ||
79 | void | |
80 | zed_log_syslog_open(int facility) | |
81 | { | |
82 | const char *identity; | |
83 | ||
84 | _ctx.do_syslog = 1; | |
85 | identity = (_ctx.id[0] == '\0') ? NULL : _ctx.id; | |
86 | openlog(identity, LOG_NDELAY, facility); | |
87 | } | |
88 | ||
89 | void | |
90 | zed_log_syslog_close(void) | |
91 | { | |
92 | _ctx.do_syslog = 0; | |
93 | closelog(); | |
94 | } | |
95 | ||
96 | static void | |
97 | _zed_log_aux(int priority, const char *fmt, va_list vargs) | |
98 | { | |
99 | char buf[ZED_LOG_MAX_LOG_LEN]; | |
100 | char *syslogp; | |
101 | char *p; | |
102 | int len; | |
103 | int n; | |
104 | ||
105 | assert(fmt != NULL); | |
106 | ||
107 | syslogp = NULL; | |
108 | p = buf; | |
109 | len = sizeof (buf); | |
110 | ||
111 | if (_ctx.id[0] != '\0') { | |
112 | n = snprintf(p, len, "%s: ", _ctx.id); | |
113 | if ((n < 0) || (n >= len)) { | |
114 | p += len - 1; | |
115 | len = 0; | |
116 | } else { | |
117 | p += n; | |
118 | len -= n; | |
119 | } | |
120 | } | |
121 | if ((len > 0) && fmt) { | |
122 | syslogp = p; | |
123 | n = vsnprintf(p, len, fmt, vargs); | |
124 | if ((n < 0) || (n >= len)) { | |
125 | p += len - 1; | |
126 | len = 0; | |
127 | } else { | |
128 | p += n; | |
129 | len -= n; | |
130 | } | |
131 | } | |
132 | *p = '\0'; | |
133 | ||
134 | if (_ctx.do_syslog && syslogp) | |
135 | syslog(priority, "%s", syslogp); | |
136 | ||
137 | if (_ctx.do_stderr && priority <= _ctx.level) | |
138 | fprintf(stderr, "%s\n", buf); | |
139 | } | |
140 | ||
141 | /* | |
142 | * Log a message at the given [priority] level specified by the printf-style | |
143 | * format string [fmt]. | |
144 | */ | |
145 | void | |
146 | zed_log_msg(int priority, const char *fmt, ...) | |
147 | { | |
148 | va_list vargs; | |
149 | ||
150 | if (fmt) { | |
151 | va_start(vargs, fmt); | |
152 | _zed_log_aux(priority, fmt, vargs); | |
153 | va_end(vargs); | |
154 | } | |
155 | } | |
156 | ||
157 | /* | |
158 | * Log a fatal error message specified by the printf-style format string [fmt]. | |
159 | */ | |
160 | void | |
161 | zed_log_die(const char *fmt, ...) | |
162 | { | |
163 | va_list vargs; | |
164 | ||
165 | if (fmt) { | |
166 | va_start(vargs, fmt); | |
167 | _zed_log_aux(LOG_ERR, fmt, vargs); | |
168 | va_end(vargs); | |
169 | } | |
170 | exit(EXIT_FAILURE); | |
171 | } |