forked from ernestrc/logd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogd_module.c
365 lines (313 loc) · 8.86 KB
/
logd_module.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdbool.h>
#include <string.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include "config.h"
#include "log.h"
#include "logd_module.h"
#include "util.h"
#define LUA_NAME_PRINT "print"
#define LUA_LEGACY_NAME_DEBUG "debug"
#define LUA_NAME_LOG_GET "log_get"
#define LUA_NAME_LOG_SET "log_set"
#define LUA_NAME_LOG_REMOVE "log_remove"
#define LUA_NAME_LOG_RESET "log_reset"
#define LUA_NAME_TABLE_TO_LOGPTR "to_logptr"
#define LUA_NAME_LOG_TO_TABLE "to_table"
#define LUA_NAME_LOG_TO_STR "to_str"
#define LUA_LEGACY_NAME_LOG_STRING "log_string"
#define LUA_NAME_LOG_CLONE "log_clone"
#define TO_LOG_PTR(L, var, idx, fn_name) \
switch (lua_type(L, idx)) { \
case LUA_TLIGHTUSERDATA: \
case LUA_TUSERDATA: \
var = (log_t*)lua_touserdata(L, idx); \
if (!var->is_safe) { \
luaL_error(L, \
"it is not safe to use a logptr outside of logd.on_log's " \
"calling thread's context. Clone first with `logd.clone`'"); \
} \
break; \
default: \
luaL_error(L, \
"1st argument must be a logptr in call to '" fn_name "': found %s", \
lua_typename(L, lua_type(L, idx))); \
return 0; \
}
#define GET_USERDATA_PROPS(log) ((prop_t*)((log) + 1))
#define NEW_USERDATA_LOG(L, size) \
((log_t*)lua_newuserdata((L), sizeof(log_t) + (size) * sizeof(prop_t)))
static void table_to_log(
lua_State* L, int idx, prop_t* props, int props_len, log_t* log)
{
const char* key;
const char* value;
int added_props = 0;
bool level_added = false;
bool time_added = false;
bool date_added = false;
DEBUG_ASSERT(props != NULL);
DEBUG_ASSERT(log != NULL);
lua_pushnil(L);
while (lua_next(L, idx)) {
if (added_props >= props_len) {
luaL_error(L,
"table exceeds max table len of %d in call to table_to_log",
props_len);
return;
}
key = lua_tostring(L, -2);
if (key == NULL) {
luaL_error(L, "table key must be a string in call to table_to_log");
return;
}
if (!level_added && strcmp(key, KEY_LEVEL) == 0)
level_added = true;
else if (!time_added && strcmp(key, KEY_TIME) == 0)
time_added = true;
else if (!date_added && strcmp(key, KEY_DATE) == 0)
date_added = true;
switch (lua_type(L, -1)) {
case LUA_TNONE:
case LUA_TNIL:
value = "nil";
break;
case LUA_TBOOLEAN:
value = lua_toboolean(L, -1) ? "true" : "false";
break;
case LUA_TNUMBER:
case LUA_TSTRING:
value = lua_tostring(L, -1);
break;
case LUA_TTABLE:
value = "<table>";
break;
case LUA_TFUNCTION:
value = "<func>";
break;
case LUA_TLIGHTUSERDATA:
case LUA_TUSERDATA:
value = "<ptr>";
break;
case LUA_TTHREAD:
value = "<thread>";
break;
default:
abort(); /* not possible */
}
DEBUG_ASSERT(added_props < props_len);
log_set(log, &props[added_props++], key, value);
lua_pop(L, 1);
}
if (!level_added) {
DEBUG_ASSERT(added_props < props_len);
log_set(log, &props[added_props++], KEY_LEVEL, "DEBUG");
}
if (!time_added) {
DEBUG_ASSERT(added_props < props_len);
log_set(log, &props[added_props++], KEY_TIME, util_get_time());
}
if (!date_added) {
DEBUG_ASSERT(added_props < props_len);
log_set(log, &props[added_props++], KEY_DATE, util_get_date());
}
}
/*
* logd_table_to_logptr will convert a table into a logptr userdata.
* The returned pointer will be valid until the original table is GC'd.
*/
static int table_to_logptr(lua_State* L, int idx)
{
log_t* log;
prop_t* props;
switch (lua_type(L, idx)) {
case LUA_TTABLE:
log = NEW_USERDATA_LOG(L, LOGD_PRINT_MAX_KEYS);
props = GET_USERDATA_PROPS(log);
log_init(log);
log->is_safe = true;
table_to_log(L, idx, props, LOGD_PRINT_MAX_KEYS, log);
break;
default:
luaL_error(L,
"1st argument must be a table in call to '" LUA_NAME_TABLE_TO_LOGPTR
"': found %s",
lua_typename(L, lua_type(L, idx)));
break;
}
return 1;
}
static int logd_table_to_logptr(lua_State* L) { return table_to_logptr(L, 1); }
static char* force_snprintl(lua_State* L, log_t* log)
{
int size = snprintl(NULL, 0, log);
int strlen = size + 1;
char* str = malloc(strlen);
if (str == NULL) {
luaL_error(L, "force_snprintl: ENOMEM");
return NULL; /* unreachable */
}
snprintl(str, strlen, log);
return str;
}
static int logd_log_to_str(lua_State* L)
{
log_t* log;
TO_LOG_PTR(L, log, 1, LUA_NAME_LOG_TO_STR);
char* str = force_snprintl(L, log);
lua_pushstring(L, str);
free(str);
return 1;
}
static int logd_log_to_table(lua_State* L)
{
log_t* log;
prop_t* prop;
TO_LOG_PTR(L, log, 1, LUA_NAME_LOG_TO_TABLE);
lua_newtable(L);
for (prop = log->props; prop != NULL; prop = prop->next) {
lua_pushstring(L, prop->key);
lua_pushstring(L, prop->value);
lua_settable(L, 2);
}
return 1;
}
int lua_print_log(lua_State* L, int idx)
{
log_t* log;
TO_LOG_PTR(L, log, idx, LUA_NAME_PRINT);
char* str = force_snprintl(L, log);
lua_getglobal(L, "io");
lua_getfield(L, -1, "write");
lua_pushstring(L, str);
lua_call(L, 1, 0);
lua_getfield(L, -1, "write");
lua_pushliteral(L, "\n");
lua_call(L, 1, 0);
lua_pop(L, 1);
free(str);
return 0;
}
static int logd_print(lua_State* L)
{
switch (lua_type(L, 1)) {
case LUA_TUSERDATA:
case LUA_TLIGHTUSERDATA:
lua_print_log(L, 1);
break;
case LUA_TTABLE:
logd_table_to_logptr(L);
lua_print_log(L, 2);
lua_pop(L, 2);
break;
case LUA_TSTRING:
lua_newtable(L);
lua_pushliteral(L, "msg");
lua_pushvalue(L, 1);
lua_settable(L, 2);
table_to_logptr(L, 2);
lua_print_log(L, 3);
lua_pop(L, 3);
break;
default:
luaL_error(L,
"1st argument must be a string, a table or a logptr in call to "
"'" LUA_NAME_PRINT "': found %s",
lua_typename(L, lua_type(L, 1)));
break;
}
return 0;
}
static int logd_log_clone(lua_State* L)
{
log_t *orig, *clone;
prop_t* clone_props;
TO_LOG_PTR(L, orig, 1, LUA_NAME_LOG_RESET);
int size = log_size(orig);
clone = NEW_USERDATA_LOG(L, size);
clone_props = GET_USERDATA_PROPS(clone);
log_init(clone);
clone->is_safe = true;
for (prop_t* next = orig->props; next != NULL; next = next->next)
log_set(clone, clone_props++, next->key, next->value);
return 1;
}
static int logd_log_set(lua_State* L)
{
log_t* log;
TO_LOG_PTR(L, log, 1, LUA_NAME_LOG_SET);
const char* key = lua_tostring(L, 2);
if (key == NULL) {
luaL_error(L,
"2nd argument must be a string in call to '" LUA_NAME_LOG_SET
"': found %s",
lua_typename(L, lua_type(L, 2)));
}
const char* value = lua_tostring(L, 3);
if (value == NULL) {
luaL_error(L,
"3rd argument must be a string in call to '" LUA_NAME_LOG_SET
"': found %s",
lua_typename(L, lua_type(L, 3)));
}
// usage of this after being GC'd is protected by
// the log->is_safe check.
prop_t* prop = lua_newuserdata(L, sizeof(prop_t));
log_set(log, prop, key, value);
return 1;
}
static int logd_log_remove(lua_State* L)
{
log_t* log;
TO_LOG_PTR(L, log, 1, LUA_NAME_LOG_REMOVE);
const char* key = lua_tostring(L, 2);
if (key == NULL) {
luaL_error(L,
"2nd argument must be a string in call to '" LUA_NAME_LOG_REMOVE
"': found %s",
lua_typename(L, lua_type(L, 2)));
}
log_remove(log, key);
return 0;
}
static int logd_log_get(lua_State* L)
{
log_t* log;
TO_LOG_PTR(L, log, 1, LUA_NAME_LOG_GET);
const char* key = lua_tostring(L, 2);
if (key == NULL) {
luaL_error(L,
"2nd argument must be a string in call to '" LUA_NAME_LOG_GET
"': found %s",
lua_typename(L, lua_type(L, 2)));
}
const char* value = log_get(log, key);
if (value != NULL)
lua_pushstring(L, value);
else
lua_pushnil(L);
return 1;
}
static int logd_log_reset(lua_State* L)
{
log_t* log;
TO_LOG_PTR(L, log, 1, LUA_NAME_LOG_RESET);
log_init(log);
return 0;
}
static const struct luaL_Reg logd_functions[] = {{LUA_NAME_PRINT, &logd_print},
{LUA_LEGACY_NAME_DEBUG, &logd_print},
{LUA_NAME_TABLE_TO_LOGPTR, &logd_table_to_logptr},
{LUA_NAME_LOG_TO_STR, &logd_log_to_str},
{LUA_NAME_LOG_TO_TABLE, &logd_log_to_table},
{LUA_NAME_LOG_CLONE, &logd_log_clone},
{LUA_LEGACY_NAME_LOG_STRING, &logd_log_to_str},
{LUA_NAME_LOG_GET, &logd_log_get}, {LUA_NAME_LOG_SET, &logd_log_set},
{LUA_NAME_LOG_REMOVE, &logd_log_remove},
{LUA_NAME_LOG_RESET, &logd_log_reset}, {NULL, NULL}};
LUALIB_API int luaopen_logd(lua_State* L)
{
luaL_register(L, LUA_NAME_LOGD_MODULE, logd_functions);
return 1;
}