aboutsummaryrefslogtreecommitdiff
path: root/src/lua/log.c
blob: ee853691eafdd6a64f7e6db8c10d1e334feaa4f4 (plain)
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
#include <assert.h>
#include <string.h>

#include "log.h"

static lua_CFunction lua_log_getinfo = NULL;

static lua_CFunction lua_log_stringformat = NULL;

static lua_CFunction lua_log_tostring = NULL;

static void lua_log_debuginfo(lua_State *state, const char **module, const char **func,
                              char buffer[], size_t size)
{
    lua_pushcfunction(state, lua_log_getinfo);

    // Ignore the first frame (the c function itself)
    lua_pushnumber(state, 2);

    // S: source, l: currentline, n: name
    const char *query = "Sln";
    lua_pushstring(state, query);

    if (lua_pcall(state, 2, 1, 0) == LUA_OK) {
        lua_getfield(state, -1, "short_src");
        const char *shortsrc = lua_tostring(state, -1);

        lua_getfield(state, -2, "currentline");
        int line = lua_tointeger(state, -1);

        lua_getfield(state, -3, "name");
        const char *name = lua_tostring(state, -1);

        if (!strncmp(shortsrc, "[string ", 8)) {
            int length = strlen(shortsrc);
            snprintf(buffer, size, "%.*s", length - 9, shortsrc + 8);

            *func = buffer;
            *module = "lua";
        } else {
            snprintf(buffer, size, "%s:%d", shortsrc, line);

            *func = name ? name : "?";
            *module = buffer;
        }
    } else {
        log_trace("Failed to retrieve Lua debug information");
    }

    lua_pop(state, 3);
}

// Normal logging with any_log_format
//
static int lua_log_wrapper(lua_State *state, int skip_args, any_log_level_t level)
{
    // Use string.format to print safely
    //
    int n_args = lua_gettop(state);
    lua_pushcfunction(state, lua_log_stringformat);

    for (int i = 1; i <= n_args; ++i)
        lua_pushvalue(state, skip_args + i);

    lua_call(state, n_args, 1);
    const char *message = lua_tostring(state, -1);

    const char *module, *func;
    char buffer[4096];
    lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer));

    any_log_format(level, module, func, "%s", message);
    return 0;
}

static int lua_log_format(lua_State *state)
{
    const char *param = luaL_checkstring(state, 1);
    any_log_level_t level = any_log_level_from_string(param);

    if (level == ANY_LOG_ALL)
        luaL_error(state, "Invalid log level, got \"%s\"", param);

    return lua_log_wrapper(state, 1, level);
}

static int lua_log_error(lua_State *state)
{
    return lua_log_wrapper(state, 0, ANY_LOG_ERROR);
}

static int lua_log_warn(lua_State *state)
{
    return lua_log_wrapper(state, 0, ANY_LOG_WARN);
}

static int lua_log_info(lua_State *state)
{
    return lua_log_wrapper(state, 0, ANY_LOG_INFO);
}

static int lua_log_debug(lua_State *state)
{
    return lua_log_wrapper(state, 0, ANY_LOG_DEBUG);
}

static int lua_log_trace(lua_State *state)
{
    return lua_log_wrapper(state, 0, ANY_LOG_TRACE);
}

// Structured logging with any_log_value
//
static int lua_log_wrapperv(lua_State *state, any_log_level_t level)
{
    const char *message = lua_tostring(state, 1);
    lua_remove(state, 1);
    luaL_checktype(state, 1, LUA_TTABLE);

    const char *module, *func;
    char buffer[4096];
    lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer));

    any_log_value(level, module, func, message,
                  "g:value", ANY_LOG_FORMATTER(lua_print_value), state,
                  NULL);
    return 0;
}

static int lua_log_formatv(lua_State *state)
{
    const char *param = luaL_checkstring(state, 1);
    any_log_level_t level = any_log_level_from_string(param);

    if (level == ANY_LOG_ALL)
        luaL_error(state, "Invalid log level, got \"%s\"", param);

    lua_remove(state, 1);
    return lua_log_wrapperv(state, level);
}

static int lua_log_errorv(lua_State *state)
{
    return lua_log_wrapperv(state, ANY_LOG_ERROR);
}

static int lua_log_warnv(lua_State *state)
{
    return lua_log_wrapperv(state, ANY_LOG_WARN);
}

static int lua_log_infov(lua_State *state)
{
    return lua_log_wrapperv(state, ANY_LOG_INFO);
}

static int lua_log_debugv(lua_State *state)
{
    return lua_log_wrapperv(state, ANY_LOG_DEBUG);
}

static int lua_log_tracev(lua_State *state)
{
    return lua_log_wrapperv(state, ANY_LOG_TRACE);
}

void lua_print_value(FILE *stream, lua_State *state)
{
    lua_print_value_at(stream, state, 1);
}

void lua_print_value_at(FILE *stream, lua_State *state, int index)
{
    switch (lua_type(state, index)) {
        case LUA_TNIL:
            fprintf(stream, "nil");
            break;

        case LUA_TNUMBER:
            fprintf(stream, "%lf", lua_tonumber(state, index));
            break;

        case LUA_TBOOLEAN:
            fprintf(stream, lua_toboolean(state, index) ? "true" : "false");
            break;

        case LUA_TSTRING:
            fprintf(stream, "\"%s\"", lua_tostring(state, index));
            break;

        case LUA_TTABLE:
            fputc('{', stream);

            bool first = true;
            lua_pushnil(state);
            while (lua_next(state, index) != 0) {
                if (!first) {
                    fputc(',', stream);
                    fputc(' ', stream);
                }
                first = false;

                if (lua_isnumber(state, -2)) {
                    fprintf(stream, "[%ld] = ", lua_tointeger(state, -2));
                } else {
                    fprintf(stream, "%s = ", lua_tostring(state, -2));
                }

                lua_print_value_at(stream, state, lua_gettop(state));
                lua_pop(state, 1);
            }

            fputc('}', stream);
            break;


        case LUA_TFUNCTION:
            fprintf(stream, "function (%p)", lua_topointer(state, index));
            break;

        case LUA_TTHREAD:
            fprintf(stream, "thread (%p)", lua_tothread(state, index));
            break;

        default:
            lua_pushcfunction(state, lua_log_tostring);
            lua_pushvalue(state, index);
            lua_call(state, 1, 1);
            fprintf(stream, "%s", lua_tostring(state, -1));
            lua_pop(state, 1);
            break;
    }
}

int lua_log_library(lua_State *state)
{
    static const luaL_Reg library[] = {
        // TODO: Is it wise to allow panic from Lua?
        //{ "panic",  lua_log_panic },

        { "format", lua_log_format },
        { "error",  lua_log_error },
        { "warn",   lua_log_warn },
        { "info",   lua_log_info },
        { "debug",  lua_log_debug },
        { "trace",  lua_log_trace },

        { "value_format", lua_log_formatv },
        { "value_error",  lua_log_errorv },
        { "value_warn",   lua_log_warnv },
        { "value_info",   lua_log_infov },
        { "value_debug",  lua_log_debugv },
        { "value_trace",  lua_log_tracev },
        { NULL, NULL },
    };

    lua_getglobal(state, "debug");
    lua_getfield(state, -1, "getinfo");
    lua_log_getinfo = lua_tocfunction(state, -1);

    lua_getglobal(state, "string");
    lua_getfield(state, -1, "format");
    lua_log_stringformat = lua_tocfunction(state, -1);

    lua_getglobal(state, "tostring");
    lua_log_tostring = lua_tocfunction(state, -1);

    lua_pop(state, 5);
    luaL_newlib(state, library);
    return 1;
}