Codebergdnkl
foot
master
1#include "log.h"
3#include <errno.h>
4#include <stdarg.h>
5#include <stdbool.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <syslog.h>
11#include <unistd.h>
13#include "debug.h"
14#include "util.h"
15#include "xsnprintf.h"
17static bool colorize = false;
18static bool do_syslog = false;
19static enum log_class log_level = LOG_CLASS_NONE;
21static const struct {
22 const char name[8];
23 const char log_prefix[7];
24 uint8_t color;
25 int syslog_equivalent;
26} log_level_map[] = {
27 [LOG_CLASS_NONE] = {"none", "none", 5, -1},
28 [LOG_CLASS_ERROR] = {"error", " err", 31, LOG_ERR},
29 [LOG_CLASS_WARNING] = {"warning", "warn", 33, LOG_WARNING},
30 [LOG_CLASS_INFO] = {"info", "info", 97, LOG_INFO},
31 [LOG_CLASS_DEBUG] = {"debug", " dbg", 36, LOG_DEBUG},
32};
34void
35log_init(enum log_colorize _colorize, bool _do_syslog,
36 enum log_facility syslog_facility, enum log_class _log_level)
37{
38 static const int facility_map[] = {
39 [LOG_FACILITY_USER] = LOG_USER,
40 [LOG_FACILITY_DAEMON] = LOG_DAEMON,
41 };
43 /* Don't use colors if NO_COLOR is defined and not empty */
44 const char *no_color_str = getenv("NO_COLOR");
45 const bool no_color = no_color_str != NULL && no_color_str[0] != '\0';
47 colorize = _colorize == LOG_COLORIZE_ALWAYS
48 || (_colorize == LOG_COLORIZE_AUTO
49 && !no_color && isatty(STDERR_FILENO));
50 do_syslog = _do_syslog;
51 log_level = _log_level;
53 int slvl = log_level_map[_log_level].syslog_equivalent;
54 if (slvl < 0)
55 do_syslog = false;
57 if (do_syslog) {
58 openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]);
60 xassert(slvl >= 0);
61 setlogmask(LOG_UPTO(slvl));
62 }
63}
65void
66log_deinit(void)
67{
68 if (do_syslog)
69 closelog();
70}
72static void
73_log(enum log_class log_class, const char *module, const char *file, int lineno,
74 const char *fmt, int sys_errno, va_list va)
75{
76 xassert(log_class > LOG_CLASS_NONE);
77 xassert(log_class < ALEN(log_level_map));
79 if (log_class > log_level)
80 return;
82 const char *prefix = log_level_map[log_class].log_prefix;
83 unsigned int class_clr = log_level_map[log_class].color;
85 char clr[16];
86 xsnprintf(clr, sizeof(clr), "\033[%um", class_clr);
87 fprintf(stderr, "%s%s%s: ", colorize ? clr : "", prefix, colorize ? "\033[0m" : "");
89 if (colorize)
90 fputs("\033[2m", stderr);
91 fprintf(stderr, "%s:%d: ", file, lineno);
92 if (colorize)
93 fputs("\033[0m", stderr);
95 vfprintf(stderr, fmt, va);
97 if (sys_errno != 0)
98 fprintf(stderr, ": %s", strerror(sys_errno));
100 fputc('\n', stderr);
101}
103static void
104_sys_log(enum log_class log_class, const char *module,
105 const char UNUSED *file, int UNUSED lineno,
106 const char *fmt, int sys_errno, va_list va)
107{
108 xassert(log_class > LOG_CLASS_NONE);
109 xassert(log_class < ALEN(log_level_map));
111 if (!do_syslog)
112 return;
114 if (log_class > log_level)
115 return;
117 /* Map our log level to syslog's level */
118 int level = log_level_map[log_class].syslog_equivalent;
120 char msg[4096];
121 int n = vsnprintf(msg, sizeof(msg), fmt, va);
122 xassert(n >= 0);
124 if (sys_errno != 0 && (size_t)n < sizeof(msg))
125 snprintf(msg + n, sizeof(msg) - n, ": %s", strerror(sys_errno));
127 syslog(level, "%s: %s", module, msg);
128}
130void
131log_msg_va(enum log_class log_class, const char *module,
132 const char *file, int lineno, const char *fmt, va_list va)
133{
134 va_list va2;
135 va_copy(va2, va);
136 _log(log_class, module, file, lineno, fmt, 0, va);
137 _sys_log(log_class, module, file, lineno, fmt, 0, va2);
138 va_end(va2);
139}
141void
142log_msg(enum log_class log_class, const char *module,
143 const char *file, int lineno, const char *fmt, ...)
144{
145 va_list va;
146 va_start(va, fmt);
147 log_msg_va(log_class, module, file, lineno, fmt, va);
148 va_end(va);
149}
151void
152log_errno_va(enum log_class log_class, const char *module,
153 const char *file, int lineno,
154 const char *fmt, va_list va)
155{
156 log_errno_provided_va(log_class, module, file, lineno, errno, fmt, va);
157}
159void
160log_errno(enum log_class log_class, const char *module,
161 const char *file, int lineno,
162 const char *fmt, ...)
163{
164 va_list va;
165 va_start(va, fmt);
166 log_errno_va(log_class, module, file, lineno, fmt, va);
167 va_end(va);
168}
170void
171log_errno_provided_va(enum log_class log_class, const char *module,
172 const char *file, int lineno, int errno_copy,
173 const char *fmt, va_list va)
174{
175 va_list va2;
176 va_copy(va2, va);
177 _log(log_class, module, file, lineno, fmt, errno_copy, va);
178 _sys_log(log_class, module, file, lineno, fmt, errno_copy, va2);
179 va_end(va2);
180}
182void
183log_errno_provided(enum log_class log_class, const char *module,
184 const char *file, int lineno, int errno_copy,
185 const char *fmt, ...)
186{
187 va_list va;
188 va_start(va, fmt);
189 log_errno_provided_va(log_class, module, file, lineno, errno_copy, fmt, va);
190 va_end(va);
191}
193static size_t
194map_len(void)
195{
196 size_t len = ALEN(log_level_map);
197#ifndef _DEBUG
198 /* Exclude "debug" entry for non-debug builds */
199 len--;
200#endif
201 return len;
202}
204int
205log_level_from_string(const char *str)
206{
207 if (unlikely(str[0] == '\0'))
208 return -1;
210 for (int i = 0, n = map_len(); i < n; i++)
211 if (streq(str, log_level_map[i].name))
212 return i;
214 return -1;
215}
217const char *
218log_level_string_hint(void)
219{
220 static char buf[64];
221 if (buf[0] != '\0')
222 return buf;
224 for (size_t i = 0, pos = 0, n = map_len(); i < n; i++) {
225 const char *entry = log_level_map[i].name;
226 const char *delim = (i + 1 < n) ? ", " : "";
227 pos += xsnprintf(buf + pos, sizeof(buf) - pos, "'%s'%s", entry, delim);
228 }
230 return buf;
231}
Diagnostics5 git calls · 25 ms
CallStatusSizeTime025 ms
discover200178 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=10322 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetch2001.7 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=1692 git.response.cache=HIT git.response.duration_ms=20 git.fetch.wants=ebf411ecaaece44fbddf49f0ac74eedff3cb8e47 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1692 git.pkt.payload.max=1665 git.sideband.band1.bytes=1665 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e22c54dc632a83
- Request
- 6967c5d3…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format