Annotation of src/usr.bin/cvs/log.c, Revision 1.31
1.31 ! niallo 1: /* $OpenBSD: log.c,v 1.30 2006/01/02 17:06:10 xsa Exp $ */
1.1 jfb 2: /*
1.2 jfb 3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.1 jfb 4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.29 xsa 27: #include "includes.h"
1.1 jfb 28:
1.16 xsa 29: #include "cvs.h"
1.1 jfb 30: #include "log.h"
31:
32: extern char *__progname;
33:
1.3 jfb 34:
1.2 jfb 35: #ifdef unused
1.13 jfb 36: static char *cvs_log_levels[LP_MAX + 1] = {
1.1 jfb 37: "debug",
38: "info",
39: "notice",
40: "warning",
41: "error",
42: "alert",
1.4 jfb 43: "error",
44: "abort",
1.13 jfb 45: "trace",
1.1 jfb 46: };
1.2 jfb 47: #endif
1.1 jfb 48:
1.13 jfb 49: static int cvs_slpriomap[LP_MAX + 1] = {
1.1 jfb 50: LOG_DEBUG,
1.24 xsa 51: LOG_INFO,
1.23 xsa 52: LOG_NOTICE,
1.1 jfb 53: LOG_WARNING,
54: LOG_ERR,
55: LOG_ALERT,
56: LOG_ERR,
1.4 jfb 57: LOG_ERR,
1.13 jfb 58: LOG_DEBUG,
1.1 jfb 59: };
60:
1.26 niallo 61: #if !defined(RCSPROG)
1.19 joris 62: static int send_m = 1;
1.26 niallo 63: #endif
1.1 jfb 64: static u_int cvs_log_dest = LD_STD;
65: static u_int cvs_log_flags = 0;
66:
1.10 jfb 67: static struct syslog_data cvs_sl = SYSLOG_DATA_INIT;
1.1 jfb 68:
1.9 jfb 69: /* filter manipulation macros */
1.22 xsa 70: #define CVS_LOG_FLTRRST() (cvs_log_filters = 0)
71: #define CVS_LOG_FLTRSET(l) (cvs_log_filters |= (1 << l))
72: #define CVS_LOG_FLTRGET(l) (cvs_log_filters & (1 << l))
73: #define CVS_LOG_FLTRCLR(l) (cvs_log_filters &= ~(1 << l))
1.1 jfb 74:
1.9 jfb 75: static u_int cvs_log_filters;
1.1 jfb 76:
77:
78: /*
79: * cvs_log_init()
80: *
81: * Initialize the logging facility of the server.
82: */
1.30 xsa 83: void
1.1 jfb 84: cvs_log_init(u_int dest, u_int flags)
85: {
86: int slopt;
87:
88: cvs_log_dest = dest;
89: cvs_log_flags = flags;
90:
91: /* by default, filter only LP_DEBUG and LP_INFO levels */
1.9 jfb 92: CVS_LOG_FLTRRST();
93: CVS_LOG_FLTRSET(LP_DEBUG);
94: CVS_LOG_FLTRSET(LP_INFO);
1.1 jfb 95:
1.8 jfb 96: /* traces are enabled with the -t command-line option */
1.9 jfb 97: CVS_LOG_FLTRSET(LP_TRACE);
1.8 jfb 98:
1.1 jfb 99: if (dest & LD_SYSLOG) {
100: slopt = 0;
101:
102: if (dest & LD_CONS)
103: slopt |= LOG_CONS;
104: if (flags & LF_PID)
105: slopt |= LOG_PID;
106:
107: openlog_r(__progname, slopt, LOG_DAEMON, &cvs_sl);
108: }
109: }
110:
111:
112: /*
113: * cvs_log_cleanup()
114: *
115: * Cleanup the logging facility.
116: */
117: void
118: cvs_log_cleanup(void)
119: {
1.7 tedu 120:
1.1 jfb 121: closelog_r(&cvs_sl);
122: }
123:
124:
125: /*
126: * cvs_log_filter()
127: *
128: * Apply or remove filters on the logging facility. The exact operation is
129: * specified by the <how> and <level> arguments. The <how> arguments tells
130: * how the filters will be affected, and <level> gives the log levels that
131: * will be affected by the change.
132: * Returns 0 on success, or -1 on failure.
133: */
134:
135: int
136: cvs_log_filter(u_int how, u_int level)
137: {
138: u_int i;
139:
140: if ((level > LP_MAX) && (level != LP_ALL)) {
141: cvs_log(LP_ERR, "invalid log level for filter");
142: return (-1);
143: }
144:
145: switch (how) {
146: case LP_FILTER_SET:
147: if (level == LP_ALL)
1.9 jfb 148: for (i = 0; i <= LP_MAX; i++)
149: CVS_LOG_FLTRSET(i);
1.1 jfb 150: else
1.9 jfb 151: CVS_LOG_FLTRSET(level);
1.1 jfb 152: break;
153: case LP_FILTER_UNSET:
154: if (level == LP_ALL)
1.9 jfb 155: CVS_LOG_FLTRRST();
1.1 jfb 156: else
1.9 jfb 157: CVS_LOG_FLTRCLR(level);
1.1 jfb 158: break;
159: default:
160: return (-1);
161: }
162:
163: return (0);
164: }
165:
166:
167: /*
168: * cvs_log()
169: *
170: * Log the format-string message
171: * The <fmt> argument should not have a terminating newline, as this is taken
172: * care of by the logging facility.
173: */
174: int
175: cvs_log(u_int level, const char *fmt, ...)
176: {
177: int ret;
178: va_list vap;
179:
180: va_start(vap, fmt);
181: ret = cvs_vlog(level, fmt, vap);
182: va_end(vap);
183:
184: return (ret);
185: }
186:
187:
188: /*
189: * cvs_vlog()
190: *
191: * The <fmt> argument should not have a terminating newline, as this is taken
192: * care of by the logging facility.
193: */
194: int
195: cvs_vlog(u_int level, const char *fmt, va_list vap)
196: {
197: int ecp;
1.25 xsa 198: char prefix[64], buf[1024], ebuf[255];
1.1 jfb 199: FILE *out;
1.26 niallo 200: #if !defined(RCSPROG)
1.27 joris 201: char *cmdname;
1.20 joris 202: struct cvs_cmd *cmdp;
1.26 niallo 203: #endif
1.1 jfb 204:
1.13 jfb 205: if (level > LP_MAX)
1.4 jfb 206: return (-1);
1.1 jfb 207:
208: /* apply any filters */
1.9 jfb 209: if (CVS_LOG_FLTRGET(level))
1.1 jfb 210: return (0);
211:
212: if (level == LP_ERRNO)
213: ecp = errno;
1.4 jfb 214: else
215: ecp = 0;
1.1 jfb 216:
1.17 xsa 217: /* always use the command name in error messages, not aliases */
1.26 niallo 218: #if !defined(RCSPROG)
1.27 joris 219: if (cvs_command == NULL)
220: cmdname = " ";
221: else {
222: cmdp = cvs_findcmd(cvs_command);
223: cmdname = cmdp->cmd_name;
224: }
1.17 xsa 225:
1.3 jfb 226: /* The cvs program appends the command name to the program name */
1.8 jfb 227: if (level == LP_TRACE) {
228: strlcpy(prefix, " -> ", sizeof(prefix));
229: if (cvs_cmdop == CVS_OP_SERVER)
230: prefix[0] = 'S';
231: } else if (cvs_command != NULL) {
1.4 jfb 232: if (level == LP_ABORT)
233: snprintf(prefix, sizeof(prefix), "%s [%s aborted]",
1.27 joris 234: __progname, cmdname);
1.4 jfb 235: else
236: snprintf(prefix, sizeof(prefix), "%s %s", __progname,
1.27 joris 237: cmdname);
1.6 deraadt 238: } else /* just use the standard strlcpy */
1.26 niallo 239: #endif
1.3 jfb 240: strlcpy(prefix, __progname, sizeof(prefix));
241:
1.8 jfb 242: if ((cvs_log_flags & LF_PID) && (level != LP_TRACE)) {
1.1 jfb 243: snprintf(buf, sizeof(buf), "[%d]", (int)getpid());
244: strlcat(prefix, buf, sizeof(prefix));
245: }
246:
247: vsnprintf(buf, sizeof(buf), fmt, vap);
248: if (level == LP_ERRNO) {
249: snprintf(ebuf, sizeof(ebuf), ": %s", strerror(errno));
250: strlcat(buf, ebuf, sizeof(buf));
251: }
252:
253: if (cvs_log_dest & LD_STD) {
1.24 xsa 254: if (level < LP_NOTICE)
1.1 jfb 255: out = stdout;
256: else
257: out = stderr;
1.3 jfb 258:
1.26 niallo 259: #if !defined(RCSPROG)
1.3 jfb 260: if (cvs_cmdop == CVS_OP_SERVER) {
261: if (out == stdout)
262: putc('M', out);
1.11 jfb 263: else {
264: out = stdout;
1.3 jfb 265: putc('E', out);
1.11 jfb 266: }
1.3 jfb 267: putc(' ', out);
268: }
1.26 niallo 269: #endif
1.1 jfb 270:
1.8 jfb 271: fputs(prefix, out);
272: if (level != LP_TRACE)
273: fputs(": ", out);
274: fputs(buf, out);
275: fputc('\n', out);
1.1 jfb 276: }
277:
278: if (cvs_log_dest & LD_SYSLOG)
279: syslog_r(cvs_slpriomap[level], &cvs_sl, "%s", buf);
280:
281: /* preserve it just in case we changed it? */
282: if (level == LP_ERRNO)
283: errno = ecp;
284:
285: return (0);
1.2 jfb 286: }
287:
288:
289: /*
290: * cvs_printf()
291: *
1.14 jfb 292: * Wrapper function around printf() that prepends a 'M' command when
1.2 jfb 293: * the program is acting as server.
294: */
295: int
296: cvs_printf(const char *fmt, ...)
297: {
298: int ret;
1.26 niallo 299: #if !defined(RCSPROG)
1.14 jfb 300: char *nstr, *dp, *sp;
1.26 niallo 301: #endif
1.2 jfb 302: va_list vap;
303:
304: va_start(vap, fmt);
1.14 jfb 305:
1.26 niallo 306: #if !defined(RCSPROG)
1.14 jfb 307: if (cvs_cmdop == CVS_OP_SERVER) {
308: ret = vasprintf(&nstr, fmt, vap);
309: if (ret != -1) {
310: for (dp = nstr; *dp != '\0';) {
311: sp = strchr(dp, '\n');
312: if (sp == NULL)
313: for (sp = dp; *sp != '\0'; sp++)
314: ;
315:
1.21 joris 316: if (send_m) {
1.18 joris 317: send_m = 0;
318: putc('M', stdout);
319: putc(' ', stdout);
320: }
321:
1.14 jfb 322: fwrite(dp, sizeof(char), (size_t)(sp - dp),
323: stdout);
324:
325: if (*sp != '\n')
326: break;
327:
328: putc('\n', stdout);
1.18 joris 329: send_m = 1;
1.14 jfb 330: dp = sp + 1;
331: }
1.28 joris 332: xfree(nstr);
1.14 jfb 333: }
334: } else
1.26 niallo 335: #endif
1.14 jfb 336: ret = vprintf(fmt, vap);
337:
1.2 jfb 338: va_end(vap);
339: return (ret);
1.19 joris 340: }