Update Red Hat Copyright Notices
[nbdkit.git] / filters / log / output.c
blob4a1160466c42ef24144f6df4d8746cbc814fb0d0
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <pthread.h>
43 #include <sys/time.h>
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <unistd.h>
48 #include <nbdkit-filter.h>
50 #include "cleanup.h"
51 #include "utils.h"
52 #include "windows-compat.h"
54 #include "log.h"
56 enum type { ENTER, LEAVE, PRINT };
58 /* Adds an entry to the logfile. */
59 static void
60 to_file (struct handle *h, log_id_t id, const char *act, enum type type,
61 const char *fmt, va_list args)
63 struct timeval tv;
64 struct tm tm;
65 char timestamp[27] = "Time unknown";
67 /* Logging is best effort, so ignore failure to get timestamp */
68 if (!gettimeofday (&tv, NULL)) {
69 size_t s;
71 gmtime_r (&tv.tv_sec, &tm);
72 s = strftime (timestamp, sizeof timestamp - sizeof ".000000" + 1,
73 "%F %T", &tm);
74 assert (s);
75 snprintf (timestamp + s, sizeof timestamp - s, ".%06ld",
76 0L + tv.tv_usec);
79 #ifdef HAVE_FLOCKFILE
80 flockfile (logfile);
81 #endif
83 if (h)
84 fprintf (logfile, "%s connection=%" PRIu64 " %s%s",
85 timestamp, h->connection, type == LEAVE ? "..." : "", act);
86 else
87 fprintf (logfile, "%s %s%s",
88 timestamp, type == LEAVE ? "..." : "",act);
90 if (id)
91 fprintf (logfile, " id=%" PRIu64, id);
93 if (fmt[0] != 0)
94 fprintf (logfile, " ");
95 vfprintf (logfile, fmt, args);
97 if (type == ENTER)
98 fprintf (logfile, " ...");
100 fputc ('\n', logfile);
101 fflush (logfile);
102 #ifdef HAVE_FUNLOCKFILE
103 funlockfile (logfile);
104 #endif
107 /* Runs the script. */
108 static void
109 to_script (struct handle *h, log_id_t id, const char *act, enum type type,
110 const char *fmt, va_list args)
112 FILE *fp;
113 CLEANUP_FREE char *str = NULL;
114 size_t len = 0;
115 int r;
117 /* Create the shell variables + script. */
118 fp = open_memstream (&str, &len);
119 if (!fp) {
120 /* Not much we can do, but at least record the error. */
121 nbdkit_error ("logscript: open_memstream: %m");
122 return;
125 fprintf (fp, "act=%s\n", act);
126 if (h)
127 fprintf (fp, "connection=%" PRIu64 "\n", h->connection);
128 switch (type) {
129 case ENTER: fprintf (fp, "type=ENTER\n"); break;
130 case LEAVE: fprintf (fp, "type=LEAVE\n"); break;
131 case PRINT: fprintf (fp, "type=PRINT\n"); break;
133 if (id)
134 fprintf (fp, "id=%" PRIu64 "\n", id);
136 vfprintf (fp, fmt, args);
137 fprintf (fp, "\n");
139 fprintf (fp, "%s", logscript);
140 fclose (fp);
142 /* Run the script. Log the status, but ignore it. */
143 r = system (str);
144 exit_status_to_nbd_error (r, "logscript");
147 void
148 enter (struct handle *h, log_id_t id, const char *act,
149 const char *fmt, ...)
151 va_list args;
153 if (logfile) {
154 va_start (args, fmt);
155 to_file (h, id, act, ENTER, fmt, args);
156 va_end (args);
158 if (logscript) {
159 va_start (args, fmt);
160 to_script (h, id, act, ENTER, fmt, args);
161 va_end (args);
165 void
166 leave (struct handle *h, log_id_t id, const char *act,
167 const char *fmt, ...)
169 va_list args;
171 if (logfile) {
172 va_start (args, fmt);
173 to_file (h, id, act, LEAVE, fmt, args);
174 va_end (args);
176 if (logscript) {
177 va_start (args, fmt);
178 to_script (h, id, act, LEAVE, fmt, args);
179 va_end (args);
183 void
184 print (struct handle *h, const char *act, const char *fmt, ...)
186 va_list args;
188 if (logfile) {
189 va_start (args, fmt);
190 to_file (h, 0, act, PRINT, fmt, args);
191 va_end (args);
193 if (logscript) {
194 va_start (args, fmt);
195 to_script (h, 0, act, PRINT, fmt, args);
196 va_end (args);
200 void
201 leave_simple (struct handle *h, log_id_t id, const char *act, int r, int *err)
203 const char *s;
205 /* Only decode what server/protocol.c:nbd_errno() recognizes */
206 if (r == -1) {
207 switch (*err) {
208 case EROFS:
209 case EPERM:
210 s = " error=EPERM";
211 break;
212 case EIO:
213 s = " error=EIO";
214 break;
215 case ENOMEM:
216 s = " error=ENOMEM";
217 break;
218 #ifdef EDQUOT
219 case EDQUOT:
220 #endif
221 case EFBIG:
222 case ENOSPC:
223 s = " error=ENOSPC";
224 break;
225 #ifdef ESHUTDOWN
226 case ESHUTDOWN:
227 s = " error=ESHUTDOWN";
228 break;
229 #endif
230 case ENOTSUP:
231 #if ENOTSUP != EOPNOTSUPP
232 case EOPNOTSUPP:
233 #endif
234 s = " error=ENOTSUP";
235 break;
236 case EOVERFLOW:
237 s = " error=EOVERFLOW";
238 break;
239 case EINVAL:
240 default:
241 s = " error=EINVAL";
244 else
245 s = "";
247 leave (h, id, act, "return=%d%s", r, s);
250 void
251 leave_simple2 (struct leave_simple_params *params)
253 leave_simple (params->h, params->id, params->act, *params->r, params->err);