| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "nd_log-internals.h" |
| 4 | |
| 5 | const char *timestamp_usec_annotator(struct log_field *lf) { |
| 6 | usec_t ut = log_field_to_uint64(lf); |
| 7 | |
| 8 | if(!ut) |
| 9 | return NULL; |
| 10 | |
| 11 | static __thread char datetime[RFC3339_MAX_LENGTH]; |
| 12 | rfc3339_datetime_ut(datetime, sizeof(datetime), ut, 3, false); |
| 13 | return datetime; |
| 14 | } |
| 15 | |
| 16 | const char *errno_annotator(struct log_field *lf) { |
| 17 | int64_t errnum = log_field_to_int64(lf); |
| 18 | |
| 19 | if(errnum == 0) |
| 20 | return NULL; |
| 21 | |
| 22 | static __thread char buf[256]; |
| 23 | size_t len = print_uint64(buf, errnum); |
| 24 | buf[len++] = ','; |
| 25 | buf[len++] = ' '; |
| 26 | |
| 27 | char *msg_to = &buf[len]; |
| 28 | size_t msg_size = sizeof(buf) - len; |
| 29 | |
| 30 | const char *s = errno2str((int)errnum, msg_to, msg_size); |
| 31 | if(s != msg_to) |
| 32 | strncpyz(msg_to, s, msg_size - 1); |
| 33 | |
| 34 | return buf; |
| 35 | } |
| 36 | |
| 37 | #if defined(OS_WINDOWS) |
| 38 | const char *winerror_annotator(struct log_field *lf) { |
| 39 | DWORD errnum = log_field_to_uint64(lf); |
| 40 | |
| 41 | if (errnum == 0) |
| 42 | return NULL; |
| 43 | |
| 44 | static __thread char buf[256]; |
| 45 | size_t len = print_uint64(buf, errnum); |
| 46 | buf[len++] = ','; |
| 47 | buf[len++] = ' '; |
| 48 | |
| 49 | char *msg_to = &buf[len]; |
| 50 | size_t msg_size = sizeof(buf) - len; |
| 51 | |
| 52 | wchar_t wbuf[1024]; |
| 53 | DWORD size = FormatMessageW( |
| 54 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 55 | NULL, |
| 56 | errnum, |
| 57 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 58 | wbuf, |
| 59 | (DWORD)(sizeof(wbuf) / sizeof(wchar_t) - 1), |
| 60 | NULL |
| 61 | ); |
| 62 | |
| 63 | if (size > 0) { |
| 64 | // Remove \r\n at the end |
| 65 | while (size > 0 && (wbuf[size - 1] == L'\r' || wbuf[size - 1] == L'\n')) |
| 66 | wbuf[--size] = L'\0'; |
| 67 | |
| 68 | // Convert wide string to UTF-8 |
| 69 | int utf8_size = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, msg_to, (int)msg_size, NULL, NULL); |
| 70 | if (utf8_size == 0) |
| 71 | snprintf(msg_to, msg_size - 1, "unknown error code"); |
| 72 | msg_to[msg_size - 1] = '\0'; |
| 73 | } |
| 74 | else |
| 75 | snprintf(msg_to, msg_size - 1, "unknown error code"); |
| 76 | |
| 77 | return buf; |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | const char *priority_annotator(struct log_field *lf) { |
| 82 | uint64_t pri = log_field_to_uint64(lf); |
| 83 | return nd_log_id2priority(pri); |
| 84 | } |