| 1 | /* |
| 2 | * Copyright (c) 2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/types.h> |
| 18 | |
| 19 | #include <time.h> |
| 20 | #include <syslog.h> |
| 21 | |
| 22 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 23 | |
| 24 | struct kore_wlogcxx . . $ kore_wlog# { |
| 25 | int priocxx . . $ kore_wlog#prio.; |
| 26 | u_int16_tcxx . . $ u_int16_t# widcxx . . $ kore_wlog#wid.; |
| 27 | size_t loglencxx . . $ kore_wlog#loglen.; |
| 28 | char logmsgcxx . . $ kore_wlog#logmsg.[]; |
| 29 | }; |
| 30 | |
| 31 | static void log_printcxx . . $ log_print(afcfcee7634adf7d).(int, const char *, ...) |
| 32 | __attribute__((format (printf, 2, 3))); |
| 33 | static void log_from_workercxx . . $ log_from_worker(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *); |
| 34 | |
| 35 | static FILEcxx . . $ FILE# *fpcxx . . $ fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 36 | |
| 37 | void |
| 38 | kore_log_initcxx . . $ kore_log_init(49f6e7a06ebc5aa8).(void) |
| 39 | { |
| 40 | #if defined(KORE_SINGLE_BINARY) |
| 41 | extern const char *__progname; |
| 42 | const char *name = kore_strdup(__progname); |
| 43 | #else |
| 44 | const char *name = "kore"; |
| 45 | #endif |
| 46 | |
| 47 | fpcxx . . $ fp. = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 48 | |
| 49 | if (!kore_foregroundcxx . . $ kore_foreground.) |
| 50 | openlogcxx . . $ openlog(7b85637613228108).(name, LOG_NDELAYcxx . . $ `<external>/6168640806541382331/syslog.h:165:9`! | LOG_PIDcxx . . $ `<external>/6168640806541382331/syslog.h:162:9`!, LOG_DAEMONcxx . . $ `<external>/6168640806541382331/syslog.h:96:9`!); |
| 51 | |
| 52 | kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_WORKER_LOGcxx . . $ `include/kore/kore.h:671:9`!, log_from_workercxx . . $ log_from_worker(72fdcea79115518d).); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | kore_log_filecxx . . $ kore_log_file(85c52e162fed56f9).(const char *path) |
| 57 | { |
| 58 | if ((fpcxx . . $ fp. = fopencxx . . $ fopen(82ec0787cd37e0a3).(path, "a")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 59 | fpcxx . . $ fp. = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 60 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("fopen(%s): %s", path, errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(int prio, const char *fmt, ...) |
| 66 | { |
| 67 | va_list args; |
| 68 | const char *str; |
| 69 | struct kore_wlogcxx . . $ kore_wlog# wlog; |
| 70 | struct kore_bufcxx . . $ kore_buf# buf, pkt; |
| 71 | |
| 72 | kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&buf, 128); |
| 73 | |
| 74 | va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(args, fmt); |
| 75 | kore_buf_appendvcxx . . $ kore_buf_appendv(c929ca63aaa59654).(&buf, fmt, args); |
| 76 | va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(args); |
| 77 | |
| 78 | if (workercxx . . $ worker. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 79 | kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(&pkt, sizeof(wlog) + buf.offsetcxx . . $ kore_buf#offset.); |
| 80 | |
| 81 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&wlog, 0, sizeof(wlog)); |
| 82 | |
| 83 | wlog.priocxx . . $ kore_wlog#prio. = prio; |
| 84 | wlog.widcxx . . $ kore_wlog#wid. = workercxx . . $ worker.->idcxx . . $ kore_worker#id.; |
| 85 | wlog.loglencxx . . $ kore_wlog#loglen. = buf.offsetcxx . . $ kore_buf#offset.; |
| 86 | |
| 87 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&pkt, &wlog, sizeof(wlog)); |
| 88 | kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(&pkt, buf.datacxx . . $ kore_buf#data., buf.offsetcxx . . $ kore_buf#offset.); |
| 89 | |
| 90 | kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_MSG_PARENTcxx . . $ `include/kore/kore.h:679:9`!, KORE_MSG_WORKER_LOGcxx . . $ `include/kore/kore.h:671:9`!, |
| 91 | pkt.datacxx . . $ kore_buf#data., pkt.offsetcxx . . $ kore_buf#offset.); |
| 92 | |
| 93 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&pkt); |
| 94 | } else { |
| 95 | str = kore_buf_stringifycxx . . $ kore_buf_stringify(4bc5b263e10e2f7b).(&buf, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 96 | |
| 97 | if (kore_foregroundcxx . . $ kore_foreground. || fpcxx . . $ fp. != stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!) |
| 98 | log_printcxx . . $ log_print(afcfcee7634adf7d).(prio, "[parent]: %s\n", str); |
| 99 | else |
| 100 | syslogcxx . . $ syslog(afcfcee7634adf7d).(prio, "[parent]: %s", str); |
| 101 | } |
| 102 | |
| 103 | kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(&buf); |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | log_from_workercxx . . $ log_from_worker(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data) |
| 108 | { |
| 109 | const char *name; |
| 110 | const struct kore_wlogcxx . . $ kore_wlog# *wlog; |
| 111 | |
| 112 | if (msg->lengthcxx . . $ kore_msg#length. < sizeof(*wlog)) { |
| 113 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, |
| 114 | "too short worker log received (%zu < %zu)", |
| 115 | msg->lengthcxx . . $ kore_msg#length., sizeof(*wlog)); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | wlog = data; |
| 120 | name = kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(wlog->widcxx . . $ kore_wlog#wid.); |
| 121 | |
| 122 | if (kore_foregroundcxx . . $ kore_foreground. || fpcxx . . $ fp. != stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!) { |
| 123 | log_printcxx . . $ log_print(afcfcee7634adf7d).(wlog->priocxx . . $ kore_wlog#prio., "%s: %.*s\n", |
| 124 | name, (int)wlog->loglencxx . . $ kore_wlog#loglen., wlog->logmsgcxx . . $ kore_wlog#logmsg.); |
| 125 | } else { |
| 126 | syslogcxx . . $ syslog(afcfcee7634adf7d).(wlog->priocxx . . $ kore_wlog#prio., "%s: %.*s", |
| 127 | name, (int)wlog->loglencxx . . $ kore_wlog#loglen., wlog->logmsgcxx . . $ kore_wlog#logmsg.); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | log_printcxx . . $ log_print(afcfcee7634adf7d).(int prio, const char *fmt, ...) |
| 133 | { |
| 134 | struct tmcxx . . $ tm# *t; |
| 135 | struct timespeccxx . . $ timespec# ts; |
| 136 | va_list args; |
| 137 | char tbuf[32]; |
| 138 | |
| 139 | va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(args, fmt); |
| 140 | |
| 141 | switch (prio) { |
| 142 | case LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!: |
| 143 | case LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!: |
| 144 | case LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!: |
| 145 | case LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!: |
| 146 | case LOG_DEBUGcxx . . $ `<external>/6168640806541382331/syslog.h:58:9`!: |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | (void)clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_REALTIMEcxx . . $ `<external>/89459239771508533/time.h:46:10`!, &ts); |
| 151 | t = gmtimecxx . . $ gmtime(9477a1348abbbc5).(&ts.tv_seccxx . . $ timespec#tv_sec.); |
| 152 | |
| 153 | if (strftimecxx . . $ strftime(98e75925167c4f1b).(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", t) > 0) |
| 154 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fpcxx . . $ fp., "%s.%03ld UTC ", tbuf, ts.tv_nseccxx . . $ timespec#tv_nsec. / 1000000); |
| 155 | |
| 156 | vfprintfcxx . . $ vfprintf(bded311e54d67f9c).(fpcxx . . $ fp., fmt, args); |
| 157 | fflushcxx . . $ fflush(89bc1efcdac0ca22).(fpcxx . . $ fp.); |
| 158 | |
| 159 | va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(args); |
| 160 | } |