Skip to content

Commit

Permalink
Overrides default Lua print with tracing alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
cipharius committed Sep 1, 2023
1 parent b3364d0 commit e8b805b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/engine/arcan_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ extern bool arcan_trace_enabled;
*/
void arcan_trace_setbuffer(uint8_t* buf, size_t buf_sz, bool* finish_flag);

/*
* appends a plain log message to the trace buffer
*/
void arcan_trace_log(const char* message, size_t len);

/* add a trace entry-point (though call through the TRACE_MARK macros),
* sys returns to the main system group (graphics, video, 3d, ...) and
* subsys for a group specific subsystem (where useful distinctions exist).
Expand Down
38 changes: 38 additions & 0 deletions src/engine/arcan_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,40 @@ static bool validblendmode(int m)
m == BLEND_PREMUL;
}

#ifndef MIN
#define MIN(x,y) (((x)<(y))?(x):(y))
#endif
#define CUSTOM_PRINT_BUFFER_LEN 4096
static char custom_print_buffer[CUSTOM_PRINT_BUFFER_LEN] = "LUA_PRINT: ";
static int custom_print(lua_State* ctx)
{
int n = lua_gettop(ctx);
int total_len = 11;

for (int i = 1; i <= n; i++) {
size_t str_len = 0;
const char* str = lua_tolstring(ctx, i, &str_len);
size_t write_len = MIN(str_len, CUSTOM_PRINT_BUFFER_LEN - 2 - total_len);
memcpy(&custom_print_buffer[total_len], str, write_len);
total_len += write_len + 1;

if (total_len == CUSTOM_PRINT_BUFFER_LEN - 1) {
break;
} else {
custom_print_buffer[total_len - 1] = '\t';
}
}

if (n > 0)
custom_print_buffer[total_len - 1] = '\n';
custom_print_buffer[total_len] = '\0';

fprintf(stdout, "%s", custom_print_buffer);
arcan_trace_log(custom_print_buffer, total_len);

return 0;
}

static int zapresource(lua_State* ctx)
{
LUA_TRACE("zap_resource");
Expand Down Expand Up @@ -12173,6 +12207,10 @@ static const luaL_Reg netfuns[] = {
#undef EXT_MAPTBL_NETWORK
register_tbl(ctx, netfuns);

/* Define a custom print function for integration with tracer */
lua_pushcfunction(ctx, custom_print);
lua_setglobal(ctx, "print");

/*
* METATABLE definitions
* [calcImage] => used for calctargets
Expand Down
16 changes: 16 additions & 0 deletions src/engine/arcan_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ static bool append_string(const char* str)
return true;
}

void arcan_trace_log(const char* message, size_t len)
{
if (!arcan_trace_enabled)
return;

#ifdef WITH_TRACY
TracyCMessage(message, len);
#else
for (int i=0; i < len; i++) {
if (buffer_pos == buffer_sz)
return;
buffer[buffer_pos + i] = message[i];
}
#endif
}

#ifdef WITH_TRACY
const uint32_t color_lut[] = {
0x000000, // DEFAULT
Expand Down

0 comments on commit e8b805b

Please sign in to comment.