|
| 1 | +#include "log.h" |
| 2 | +#include "utils.h" |
| 3 | +#include <PmLogLib.h> |
| 4 | +#include <stdint.h> |
| 5 | +#include <time.h> |
| 6 | + |
| 7 | +uint64_t start = 0; |
| 8 | +PmLogContext context; |
| 9 | +LogLevel current_log_level = Info; |
| 10 | + |
| 11 | +void log_init() |
| 12 | +{ |
| 13 | + PmLogGetContext("hyperion-webos", &context); |
| 14 | +} |
| 15 | + |
| 16 | +void log_set_level(LogLevel level) |
| 17 | +{ |
| 18 | + current_log_level = level; |
| 19 | +} |
| 20 | + |
| 21 | +void log_printf(LogLevel level, const char* module, const char* fmt, ...) |
| 22 | +{ |
| 23 | + va_list args; |
| 24 | + va_start(args, fmt); |
| 25 | + const char* level_str; |
| 26 | + const char* color_str; |
| 27 | + |
| 28 | + if (start == 0) |
| 29 | + start = getticks_us(); |
| 30 | + |
| 31 | + if (level == Debug) { |
| 32 | + level_str = "DBG"; |
| 33 | + color_str = "\x1b[34m"; |
| 34 | + } else if (level == Info) { |
| 35 | + level_str = "INFO"; |
| 36 | + color_str = "\x1b[36m"; |
| 37 | + } else if (level == Warning) { |
| 38 | + level_str = "WARN"; |
| 39 | + color_str = "\x1b[33m"; |
| 40 | + } else { |
| 41 | + level_str = "ERR"; |
| 42 | + color_str = "\x1b[31m"; |
| 43 | + } |
| 44 | + |
| 45 | + char formatted[1024]; |
| 46 | + int prefix = snprintf(formatted, 1024, "%s: ", module); |
| 47 | + |
| 48 | + if (prefix < 0 || prefix > 1024) { |
| 49 | + // This shall only happen if module is 1020 characters long. |
| 50 | + fprintf(stderr, "Log message truncated...\n"); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + vsnprintf(formatted + prefix, 1024 - prefix, fmt, args); |
| 55 | + |
| 56 | + _PmLogMsgKV(context, (PmLogLevel)level, 0, level == Debug ? NULL : level_str, 0, NULL, NULL, formatted); |
| 57 | + |
| 58 | + if (level <= current_log_level) { |
| 59 | + fprintf(stderr, "%10.3fs %s[%4s %-20s]\x1b[0m %s\n", (getticks_us() - start) / 1000000.0, color_str, level_str, module, formatted + prefix); |
| 60 | + } |
| 61 | + |
| 62 | + va_end(args); |
| 63 | +} |
0 commit comments