Annotation of src/usr.bin/cvs/log.c, Revision 1.41
1.41 ! tobias 1: /* $OpenBSD: log.c,v 1.40 2007/06/26 02:24:10 niallo Exp $ */
1.1 jfb 2: /*
1.35 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.2 jfb 4: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.1 jfb 5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. The name of the author may not be used to endorse or promote products
14: * derived from this software without specific prior written permission.
15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
1.38 otto 28: #include <errno.h>
29: #include <string.h>
1.1 jfb 30:
1.16 xsa 31: #include "cvs.h"
1.1 jfb 32:
33: extern char *__progname;
1.19 joris 34: static int send_m = 1;
1.1 jfb 35:
36: /*
37: * cvs_log()
38: *
39: * Log the format-string message
40: * The <fmt> argument should not have a terminating newline, as this is taken
41: * care of by the logging facility.
42: */
1.34 xsa 43: void
1.1 jfb 44: cvs_log(u_int level, const char *fmt, ...)
45: {
46: va_list vap;
47:
48: va_start(vap, fmt);
1.34 xsa 49: cvs_vlog(level, fmt, vap);
1.1 jfb 50: va_end(vap);
51: }
52:
53: /*
54: * cvs_vlog()
55: *
56: * The <fmt> argument should not have a terminating newline, as this is taken
57: * care of by the logging facility.
58: */
1.34 xsa 59: void
1.1 jfb 60: cvs_vlog(u_int level, const char *fmt, va_list vap)
61: {
62: int ecp;
63: FILE *out;
1.20 joris 64: struct cvs_cmd *cmdp;
1.1 jfb 65:
1.35 joris 66: if (cvs_trace != 1 && level == LP_TRACE)
1.34 xsa 67: return;
1.1 jfb 68:
69: if (level == LP_ERRNO)
70: ecp = errno;
1.4 jfb 71: else
72: ecp = 0;
1.1 jfb 73:
1.35 joris 74: if (level == LP_NOTICE)
75: out = stdout;
76: else
77: out = stderr;
78:
1.36 joris 79: if (cvs_server_active) {
1.35 joris 80: if (out == stdout)
81: putc('M', out);
82: else {
1.1 jfb 83: out = stdout;
1.35 joris 84: putc('E', out);
1.3 jfb 85: }
1.1 jfb 86:
1.35 joris 87: putc(' ', out);
1.1 jfb 88: }
89:
1.39 xsa 90: /* The cvs program appends the command name to the program name */
91: if (level == LP_TRACE) {
92: if (cvs_server_active)
93: putc('S', out);
94: else
95: putc('C', out);
96: (void)fputs("-> ", out);
1.41 ! tobias 97: } else if (level != LP_RCS) {
1.39 xsa 98: (void)fputs(__progname, out);
99: if (cvs_command != NULL) {
100: /*
101: * always use the command name in error messages,
102: * not aliases
103: */
104: cmdp = cvs_findcmd(cvs_command);
105: putc(' ', out);
106: if (level == LP_ABORT)
107: (void)fprintf(out,
108: "[%s aborted]", cmdp->cmd_name);
109: else
110: (void)fputs(cmdp->cmd_name, out);
111: }
112: (void)fputs(": ", out);
113: }
114:
115: (void)vfprintf(out, fmt, vap);
116: if (level == LP_ERRNO) {
117: (void)fprintf(out, ": %s\n", strerror(ecp));
1.1 jfb 118:
1.39 xsa 119: /* preserve it just in case we changed it? */
1.1 jfb 120: errno = ecp;
1.39 xsa 121: } else
122: fputc('\n', out);
1.2 jfb 123: }
124:
125: /*
126: * cvs_printf()
127: *
1.14 jfb 128: * Wrapper function around printf() that prepends a 'M' command when
1.2 jfb 129: * the program is acting as server.
130: */
131: int
132: cvs_printf(const char *fmt, ...)
133: {
134: int ret;
1.14 jfb 135: char *nstr, *dp, *sp;
1.2 jfb 136: va_list vap;
137:
138: va_start(vap, fmt);
1.14 jfb 139:
140: ret = vasprintf(&nstr, fmt, vap);
1.32 ray 141: if (ret == -1)
142: fatal("cvs_printf: %s", strerror(errno));
143: for (dp = nstr; *dp != '\0';) {
144: sp = strchr(dp, '\n');
145: if (sp == NULL)
146: for (sp = dp; *sp != '\0'; sp++)
147: ;
148:
1.40 niallo 149: if (cvs_server_active && send_m) {
1.32 ray 150: send_m = 0;
151: putc('M', stdout);
152: putc(' ', stdout);
1.14 jfb 153: }
1.32 ray 154:
1.40 niallo 155: if (dp != nstr && dp != sp &&
156: !strncmp(dp, LOG_REVSEP, sp - dp))
157: putc('>', stdout);
158:
1.32 ray 159: fwrite(dp, sizeof(char), (size_t)(sp - dp), stdout);
160:
161: if (*sp != '\n')
162: break;
163:
164: putc('\n', stdout);
165: send_m = 1;
166: dp = sp + 1;
1.14 jfb 167: }
1.32 ray 168: xfree(nstr);
1.14 jfb 169:
1.2 jfb 170: va_end(vap);
171: return (ret);
1.19 joris 172: }