Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_log.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <math.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include "vtc.h"
39
#include "vtc_log.h"
40
41
#include "vtim.h"
42
#include "vbt.h"
43
44
static pthread_mutex_t  vtclog_mtx;
45
static char             *vtclog_buf;
46
static unsigned         vtclog_left;
47
48
static pthread_key_t log_key;
49
static double t0;
50
51
void
52
vtc_log_set_cmd(struct vtclog *vl, const struct cmds *cmds)
53 5290
{
54 5290
        AN(cmds);
55 5290
        vl->cmds = cmds;
56 5290
}
57
58
/**********************************************************************/
59
60
#define GET_VL(vl)                                      \
61 693723
        do {                                            \
62 693723
                CHECK_OBJ_NOTNULL(vl, VTCLOG_MAGIC);    \
63 693723
                PTOK(pthread_mutex_lock(&vl->mtx));     \
64 693723
                vl->act = 1;                            \
65 693694
                VSB_clear(vl->vsb);                     \
66 693694
        } while(0)
67
68
#define REL_VL(vl)                                      \
69 693694
        do {                                            \
70 693694
                AZ(VSB_finish(vl->vsb));                \
71 693694
                vtc_log_emit(vl);                       \
72 693694
                VSB_clear(vl->vsb);                     \
73 693694
                vl->act = 0;                            \
74 693694
                PTOK(pthread_mutex_unlock(&vl->mtx));   \
75 693751
        } while(0)
76
77
78
struct vtclog *
79
vtc_logopen(const char *fmt, ...)
80 8944
{
81 8944
        struct vtclog *vl;
82 8944
        va_list ap;
83 8944
        char buf[BUFSIZ];
84
85 8944
        va_start(ap, fmt);
86 8944
        vbprintf(buf, fmt, ap);
87 8944
        va_end(ap);
88
89 8944
        ALLOC_OBJ(vl, VTCLOG_MAGIC);
90 8944
        AN(vl);
91 8944
        REPLACE(vl->id, buf);
92 8944
        vl->vsb = VSB_new_auto();
93 8944
        PTOK(pthread_mutex_init(&vl->mtx, NULL));
94 8945
        PTOK(pthread_setspecific(log_key, vl));
95 8945
        return (vl);
96 8945
}
97
98
void
99
vtc_logclose(void *arg)
100 7822
{
101 7822
        struct vtclog *vl;
102
103 7822
        CAST_OBJ_NOTNULL(vl, arg, VTCLOG_MAGIC);
104 7822
        if (pthread_getspecific(log_key) == vl)
105 7822
                PTOK(pthread_setspecific(log_key, NULL));
106 7822
        VSB_destroy(&vl->vsb);
107 7822
        PTOK(pthread_mutex_destroy(&vl->mtx));
108 7822
        REPLACE(vl->id, NULL);
109 7822
        FREE_OBJ(vl);
110 7822
}
111
112
static void v_noreturn_
113
vtc_logfail(void)
114 3
{
115
116 3
        vtc_error = 2;
117 3
        if (!pthread_equal(pthread_self(), vtc_thread))
118 0
                pthread_exit(NULL);
119 3
        else
120 3
                exit(fail_out());
121
122 0
        WRONG("unreachable"); /*lint !e827 Help Coverity Scan see noreturn. */
123 0
}
124
125
static const char * const lead[] = {
126
        "----",
127
        "*   ",
128
        "**  ",
129
        "*** ",
130
        "****"
131
};
132
133
#define NLEAD (sizeof(lead)/sizeof(lead[0]))
134
135
static void
136
vtc_leadinv(const struct vtclog *vl, int lvl, const char *fmt, va_list ap)
137 545633
{
138
139 545633
        assert(lvl < (int)NLEAD);
140 545633
        assert(lvl >= 0);
141 545633
        VSB_printf(vl->vsb, "%s %-5s ",
142 545633
            lead[lvl < 0 ? 1: lvl], vl->id);
143 545633
        if (fmt != NULL)
144 545660
                (void)VSB_vprintf(vl->vsb, fmt, ap);
145 545633
}
146
147
static void
148
vtc_leadin(const struct vtclog *vl, int lvl, const char *fmt, ...)
149 700
{
150 700
        va_list ap;
151
152 700
        va_start(ap, fmt);
153 700
        vtc_leadinv(vl, lvl, fmt, ap);
154 700
        va_end(ap);
155 700
}
156
157
static void
158
vtc_log_emit(const struct vtclog *vl)
159 693551
{
160 693551
        unsigned l;
161 693551
        int i;
162 693551
        int t_this;
163 693551
        static int t_last = -1;
164
165 693551
        l = VSB_len(vl->vsb);
166 693551
        if (l == 0)
167 9428
                return;
168 684123
        t_this = (int)round((VTIM_mono() - t0) * 1000);
169 684123
        PTOK(pthread_mutex_lock(&vtclog_mtx));
170 684327
        if (t_last != t_this) {
171 45972
                assert(vtclog_left > 25);
172 45972
                i = snprintf(vtclog_buf, vtclog_left,
173 45972
                    "**** dT    %d.%03d\n", t_this / 1000, t_this % 1000);
174 45972
                t_last = t_this;
175 45972
                vtclog_buf += i;
176 45972
                vtclog_left -= i;
177 45972
        }
178 684327
        assert(vtclog_left > l);
179 684327
        vmemcpy(vtclog_buf, VSB_data(vl->vsb), l);
180 684327
        vtclog_buf += l;
181 684327
        *vtclog_buf = '\0';
182 684327
        vtclog_left -= l;
183 684327
        PTOK(pthread_mutex_unlock(&vtclog_mtx));
184 684327
}
185
186
void
187
vtc_fatal(struct vtclog *vl, const char *fmt, ...)
188 3
{
189
190 3
        GET_VL(vl);
191 3
        va_list ap;
192 3
        va_start(ap, fmt);
193 3
        vtc_leadinv(vl, 0, fmt, ap);
194 3
        VSB_putc(vl->vsb, '\n');
195 3
        va_end(ap);
196 3
        REL_VL(vl);
197
198 3
        vtc_logfail();
199 3
}
200
201
void
202
vtc_log(struct vtclog *vl, int lvl, const char *fmt, ...)
203 547090
{
204
205 547090
        GET_VL(vl);
206 547071
        va_list ap;
207 547071
        va_start(ap, fmt);
208 547071
        if (lvl >= 0) {
209 544931
                vtc_leadinv(vl, lvl, fmt, ap);
210 544931
                VSB_putc(vl->vsb, '\n');
211 544931
        }
212 547071
        va_end(ap);
213 547071
        REL_VL(vl);
214
215 547106
        if (lvl == 0)
216 0
                vtc_logfail();
217 547106
}
218
219
/**********************************************************************
220
 * Dump a string
221
 */
222
223 293152
#define MAX_DUMP 8192
224
225
void
226
vtc_dump(struct vtclog *vl, int lvl, const char *pfx, const char *str, int len)
227 146210
{
228 146210
        char buf[64];
229 146210
        int quote = VSB_QUOTE_UNSAFE | VSB_QUOTE_ESCHEX;
230
231 146210
        AN(pfx);
232 146210
        GET_VL(vl);
233 146200
        if (str == NULL)
234 1
                vtc_leadin(vl, lvl, "%s(null)\n", pfx);
235 146199
        else {
236 146199
                bprintf(buf, "%s %-5s %s|",
237 146199
                    lead[lvl < 0 ? 1: lvl], vl->id, pfx);
238 146199
                if (len < 0)
239 112574
                        len = vstrlen(str);
240 33625
                else if (str[0] == 0x1f && (uint8_t)str[1] == 0x8b)
241 283
                        quote = VSB_QUOTE_HEX; // Dump gzip data in HEX
242 146199
                VSB_quote_pfx(vl->vsb, buf, str,
243 146199
                    len > MAX_DUMP ? MAX_DUMP : len, quote);
244 146199
                if (quote == VSB_QUOTE_HEX)
245 283
                        VSB_putc(vl->vsb, '\n');
246 146199
                if (len > MAX_DUMP)
247 377
                        VSB_printf(vl->vsb, "%s [...] (%d)\n",
248 377
                            buf, len - MAX_DUMP);
249 146199
        }
250 146200
        REL_VL(vl);
251 146222
        if (lvl == 0)
252 0
                vtc_logfail();
253 146222
}
254
255
/**********************************************************************
256
 * Hexdump
257
 */
258
259
void
260
vtc_hexdump(struct vtclog *vl, int lvl, const char *pfx,
261
    const void *ptr, unsigned len)
262 420
{
263 420
        int nl = 1;
264 420
        unsigned l;
265 420
        const uint8_t *ss = ptr;
266
267 420
        AN(pfx);
268 420
        GET_VL(vl);
269 420
        if (ss == NULL)
270 0
                vtc_leadin(vl, lvl, "%s(null)\n", pfx);
271 420
        else {
272 7241
                for (l = 0; l < len; l++, ss++) {
273 6823
                        if (l > 512) {
274 2
                                VSB_cat(vl->vsb, "...");
275 2
                                break;
276 2
                        }
277 6821
                        if (nl) {
278 699
                                vtc_leadin(vl, lvl, "%s| ", pfx);
279 699
                                nl = 0;
280 699
                        }
281 6821
                        VSB_printf(vl->vsb, " %02x", *ss);
282 6821
                        if ((l & 0xf) == 0xf) {
283 336
                                VSB_cat(vl->vsb, "\n");
284 336
                                nl = 1;
285 336
                        }
286 6821
                }
287 420
        }
288 420
        if (!nl)
289 363
                VSB_cat(vl->vsb, "\n");
290 420
        REL_VL(vl);
291 420
        if (lvl == 0)
292 0
                vtc_logfail();
293 420
}
294
295
/**********************************************************************/
296
297
static void v_noreturn_
298
vtc_log_VAS_Fail(const char *func, const char *file, int line,
299
    const char *cond, enum vas_e why)
300 0
{
301 0
        char buf[4096] = "";
302 0
        struct vtclog *vl;
303 0
        int e = errno;
304
305 0
        (void)why;
306
307 0
        if (VBT_dump(sizeof buf, buf) < 0) {
308 0
                bprintf(buf, "Failed to print backtrace: %d (%s)\n",
309 0
                    errno, strerror(errno));
310 0
        }
311
312 0
        vl = pthread_getspecific(log_key);
313 0
        if (vl == NULL || vl->act) {
314 0
                fprintf(stderr,
315 0
                    "Assert error in %s(), %s line %d:\n"
316 0
                    "  Condition(%s) not true. (errno=%d %s)\n"
317 0
                    "%s\n",
318 0
                    func, file, line, cond, e, strerror(e), buf);
319 0
        } else {
320 0
                vtc_fatal(vl, "Assert error in %s(), %s line %d:"
321 0
                    "  Condition(%s) not true."
322 0
                    "  Errno=%d %s\n"
323 0
                    "%s\n",
324 0
                    func, file, line, cond, e, strerror(e), buf);
325 0
        }
326 0
        abort();
327 0
}
328
329
/**********************************************************************/
330
331
void
332
vtc_loginit(char *buf, unsigned buflen)
333 1122
{
334
335 1122
        VAS_Fail_Func = vtc_log_VAS_Fail;
336 1122
        t0 = VTIM_mono();
337 1122
        vtclog_buf = buf;
338 1122
        vtclog_left = buflen;
339 1122
        PTOK(pthread_mutex_init(&vtclog_mtx, NULL));
340 1122
        PTOK(pthread_key_create(&log_key, NULL));
341 1122
}