diff options
author | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
---|---|---|
committer | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
commit | cc3f33db7a8d3c4ad373e646b199808e01bc5d9b (patch) | |
tree | ec09d690c7656ab5f2cc72607e05fb359c24d8b2 /src/core/log.c |
added webrdp public code
Diffstat (limited to 'src/core/log.c')
-rw-r--r-- | src/core/log.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/core/log.c b/src/core/log.c new file mode 100644 index 0000000..5671d90 --- /dev/null +++ b/src/core/log.c @@ -0,0 +1,164 @@ +/* BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST <www.newinfosec.ru>, sss <sss at dark-alexandr dot net>. + * + */ + +#include <stdio.h> +#include <stdint.h> +#include <stdbool.h> +#include <time.h> + +#include "webrdp_core_api.h" + +#include "globals.h" + +#include "log.h" +#include "utilities.h" + +#include "ws_session.h" +#include "wrdp_thpool.h" + +/* + * sample: + * [D 190318 20:25:24 main.c:666] + */ + +static void +print_time() +{ + time_t t = time(0); + struct tm *ptm = localtime(&t); + + if (!ptm) + printf("%ld", t); + else + printf("%02d-%02d %02d:%02d:%02d", ptm->tm_mon, ptm->tm_mday, + ptm->tm_hour, ptm->tm_min, ptm->tm_sec); +} + +static void +binary_print(const uint8_t *buf, size_t buf_len) +{ + hex_print(buf, buf_len); +} + +static void +text_print(const uint8_t *buf, size_t buf_len) +{ + printf("%.*s", (int)buf_len, buf); +} + +static void +print_msg_level(wrdp_log_level_e *level) +{ + char l; + switch (*level) + { + case wrdp_log_level_debug: + { + l = 'D'; + } + break; + case wrdp_log_level_warning: + { + l = 'W'; + } + break; + case wrdp_log_level_error: + { + l = 'E'; + } + break; + case wrdp_log_level_trace: + { + l = 'T'; + } + break; + case wrdp_log_level_info: + default: + { + l = 'I'; + } + break; + } + printf("%c", l); +} + +static void +print_sender_info(log_msg_info *msg_info) +{ + task_info *info = msg_info->task_info; + wrdp_thpool_task *t = msg_info->wrdp_thpool_task; + ws_session *s = msg_info->ws_session; + if (!t) + { + if (info && info->wrdp_thpool_task) + t = info->wrdp_thpool_task; + else if (s && s->wrdp_thpool_task) + t = s->wrdp_thpool_task; + else if (s && s->task_info) + { + task_info *i = s->task_info; + if (i->wrdp_thpool_task) + t = i->wrdp_thpool_task; + } + } + if (!info) + { + if (s && s->task_info) + info = s->task_info; + } + if (t) + printf(" task: %p", t); + if (info) + printf(" task_info: %p", info); + if (s) + printf(" ws_session: %p", s); +} + +static void +format_prefix(log_msg_info *msg_info) +{ + printf("["); + print_msg_level(&(msg_info->level)); + printf(" "); + print_time(); + print_sender_info(msg_info); + printf("] "); +} + +void +log_msg( + const uint8_t *buf, size_t buf_len, wrdp_log_level_e level, uint16_t flags) +{ + log_msg_info i = {0}; + i.buf = buf; + i.buf_size = buf_len; + i.flags = flags; + i.level = level; + log_msg_ex(&i); +} + +void +log_msg_ex(log_msg_info *msg_info) +{ + size_t buf_size; + if (msg_info->level > g_globals.settings.log_level) + { + return; + } + buf_size = msg_info->buf_size; + if (!buf_size) + buf_size = strlen((const char *)msg_info->buf); + format_prefix(msg_info); + if (msg_info->flags & wrdp_log_flag_binary) + { + binary_print(msg_info->buf, buf_size); + } + else + { + text_print(msg_info->buf, buf_size); + } + printf("\n"); +} |