1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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");
}
|