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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
* Logging infrastructure that aims to support multi-threading, indentation
* and ansi colors.
*/
#include "rust_internal.h"
#include "sync/spin_lock.h"
#include "util/array_list.h"
#include <stdarg.h>
static uint32_t
read_type_bit_mask() {
uint32_t bits = rust_log::ULOG | rust_log::ERR;
char *env_str = getenv("RUST_LOG");
if (env_str) {
bits = 0;
bits |= strstr(env_str, "err") ? rust_log::ERR : 0;
bits |= strstr(env_str, "mem") ? rust_log::MEM : 0;
bits |= strstr(env_str, "comm") ? rust_log::COMM : 0;
bits |= strstr(env_str, "task") ? rust_log::TASK : 0;
bits |= strstr(env_str, "up") ? rust_log::UPCALL : 0;
bits |= strstr(env_str, "dom") ? rust_log::DOM : 0;
bits |= strstr(env_str, "ulog") ? rust_log::ULOG : 0;
bits |= strstr(env_str, "trace") ? rust_log::TRACE : 0;
bits |= strstr(env_str, "dwarf") ? rust_log::DWARF : 0;
bits |= strstr(env_str, "cache") ? rust_log::CACHE : 0;
bits |= strstr(env_str, "timer") ? rust_log::TIMER : 0;
bits |= strstr(env_str, "gc") ? rust_log::GC : 0;
bits |= strstr(env_str, "stdlib") ? rust_log::STDLIB : 0;
bits |= strstr(env_str, "special") ? rust_log::SPECIAL : 0;
bits |= strstr(env_str, "kern") ? rust_log::KERN : 0;
bits |= strstr(env_str, "all") ? rust_log::ALL : 0;
bits = strstr(env_str, "none") ? 0 : bits;
}
return bits;
}
rust_log::ansi_color
get_type_color(rust_log::log_type type) {
rust_log::ansi_color color = rust_log::WHITE;
if (type & rust_log::ERR)
color = rust_log::RED;
if (type & rust_log::MEM)
color = rust_log::YELLOW;
if (type & rust_log::UPCALL)
color = rust_log::GREEN;
if (type & rust_log::COMM)
color = rust_log::MAGENTA;
if (type & rust_log::DOM)
color = rust_log::LIGHTTEAL;
if (type & rust_log::TASK)
color = rust_log::LIGHTTEAL;
return color;
}
static const char * _foreground_colors[] = { "[37m",
"[31m", "[1;31m",
"[32m", "[1;32m",
"[33m", "[1;33m",
"[31m", "[1;31m",
"[35m", "[1;35m",
"[36m", "[1;36m" };
/**
* Synchronizes access to the underlying logging mechanism.
*/
static spin_lock _log_lock;
static uint32_t _last_thread_id;
rust_log::rust_log(rust_srv *srv, rust_dom *dom) :
_srv(srv),
_dom(dom),
_type_bit_mask(read_type_bit_mask()),
_use_colors(getenv("RUST_COLOR_LOG")),
_indent(0) {
}
rust_log::~rust_log() {
}
const uint16_t
hash(uintptr_t ptr) {
// Robert Jenkins' 32 bit integer hash function
ptr = (ptr + 0x7ed55d16) + (ptr << 12);
ptr = (ptr ^ 0xc761c23c) ^ (ptr >> 19);
ptr = (ptr + 0x165667b1) + (ptr << 5);
ptr = (ptr + 0xd3a2646c) ^ (ptr << 9);
ptr = (ptr + 0xfd7046c5) + (ptr << 3);
ptr = (ptr ^ 0xb55a4f09) ^ (ptr >> 16);
return (uint16_t) ptr;
}
const char *
get_color(uintptr_t ptr) {
return _foreground_colors[hash(ptr) % rust_log::LIGHTTEAL];
}
char *
copy_string(char *dst, const char *src, size_t length) {
return strncpy(dst, src, length) + length;
}
char *
append_string(char *buffer, const char *format, ...) {
if (buffer != NULL && format) {
va_list args;
va_start(args, format);
vsprintf(buffer + strlen(buffer), format, args);
va_end(args);
}
return buffer;
}
char *
append_string(char *buffer, rust_log::ansi_color color,
const char *format, ...) {
if (buffer != NULL && format) {
append_string(buffer, "\x1b%s", _foreground_colors[color]);
va_list args;
va_start(args, format);
vsprintf(buffer + strlen(buffer), format, args);
va_end(args);
append_string(buffer, "\x1b[0m");
}
return buffer;
}
void
rust_log::trace_ln(uint32_t thread_id, char *prefix, char *message) {
char buffer[1024] = "";
_log_lock.lock();
append_string(buffer, "%-34s", prefix);
for (uint32_t i = 0; i < _indent; i++) {
append_string(buffer, " ");
}
append_string(buffer, "%s", message);
if (_last_thread_id != thread_id) {
_last_thread_id = thread_id;
_srv->log("---");
}
_srv->log(buffer);
_log_lock.unlock();
}
void
rust_log::trace_ln(rust_task *task, char *message) {
#if defined(__WIN32__)
uint32_t thread_id = 0;
#else
uint32_t thread_id = hash((uint32_t) pthread_self());
#endif
char prefix[1024] = "";
if (_dom && _dom->name) {
append_string(prefix, "%04" PRIxPTR ":%.10s:",
thread_id, _dom->name);
} else {
append_string(prefix, "%04" PRIxPTR ":0x%08" PRIxPTR ":",
thread_id, (uintptr_t) _dom);
}
if (task) {
if (task->name) {
append_string(prefix, "%.10s:", task->name);
} else {
append_string(prefix, "0x%08" PRIxPTR ":", (uintptr_t) task);
}
}
trace_ln(thread_id, prefix, message);
}
/**
* Traces a log message if the specified logging type is not filtered.
*/
void
rust_log::trace_ln(rust_task *task, uint32_t type_bits, char *message) {
trace_ln(task, get_type_color((rust_log::log_type) type_bits),
type_bits, message);
}
/**
* Traces a log message using the specified ANSI color code.
*/
void
rust_log::trace_ln(rust_task *task, ansi_color color,
uint32_t type_bits, char *message) {
if (is_tracing(type_bits)) {
if (_use_colors) {
char buffer[512] = "";
append_string(buffer, color, "%s", message);
trace_ln(task, buffer);
} else {
trace_ln(task, message);
}
}
}
bool
rust_log::is_tracing(uint32_t type_bits) {
return type_bits & _type_bit_mask;
}
void
rust_log::indent() {
_indent++;
}
void
rust_log::outdent() {
_indent--;
}
void
rust_log::reset_indent(uint32_t indent) {
_indent = indent;
}
|