Skip to content

Commit

Permalink
(lua) overrides default Lua print with tracing alt.
Browse files Browse the repository at this point in the history
  • Loading branch information
cipharius committed Oct 6, 2023
1 parent 123b17a commit 07baa1f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/engine/alt/trace.c
Expand Up @@ -198,3 +198,50 @@ void alt_trace_finish(lua_State* L)
arcan_conductor_toggle_watchdog();
luaL_unref(L, LUA_REGISTRYINDEX, trace_cb);
}

void alt_trace_log(lua_State* ctx)
{
const char str_prefix[] = "LUA_PRINT: ";

int n_args = lua_gettop(ctx);

int total_len = sizeof(str_prefix) - 1;
for (int i = 1; i <= n_args; ++i) {
size_t str_len = 0;
const char* str = lua_tolstring(ctx, i, &str_len);
total_len += str_len + 1;
}
total_len += 1;

char* log_buffer = arcan_alloc_mem(
total_len,
ARCAN_MEM_STRINGBUF,
ARCAN_MEM_TEMPORARY | ARCAN_MEM_NONFATAL,
ARCAN_MEMALIGN_NATURAL
);
if (log_buffer == 0) {
const char oom_msg[] = "Couldn't log trace message: Out of memory\n";
arcan_trace_log(oom_msg, sizeof(oom_msg));
return;
}

int running_len = sizeof(str_prefix) - 1;
memcpy(&log_buffer[0], str_prefix, running_len);

for (int i = 1; i <= n_args; ++i) {
size_t str_len = 0;
const char* str = lua_tolstring(ctx, i, &str_len);

memcpy(&log_buffer[running_len], str, str_len);
running_len += str_len + 1;
log_buffer[running_len - 1] = '\t';
}

log_buffer[running_len - 1] = '\n';
log_buffer[running_len] = '\0';

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

arcan_mem_free(log_buffer);
}
5 changes: 5 additions & 0 deletions src/engine/alt/trace.h
Expand Up @@ -9,6 +9,11 @@ bool alt_trace_start(lua_State* ctx, intptr_t cb, size_t sz);
*/
void alt_trace_finish(lua_State* ctx);

/*
* logs text string in stdout and in active trace buffer
*/
void alt_trace_log(lua_State* ctx);

/*
* copy [msg] and keep as the last known crash source in the
* accumulated trash buffer.
Expand Down
5 changes: 5 additions & 0 deletions src/engine/arcan_general.h
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
4 changes: 4 additions & 0 deletions src/engine/arcan_lua.c
Expand Up @@ -12231,6 +12231,10 @@ static const luaL_Reg netfuns[] = {
#undef EXT_MAPTBL_NETWORK
register_tbl(ctx, netfuns);

/* Override the default print for integration with tracer */
lua_pushcfunction(ctx, alt_trace_log);
lua_setglobal(ctx, "print");

/*
* METATABLE definitions
* [calcImage] => used for calctargets
Expand Down
7 changes: 7 additions & 0 deletions src/engine/arcan_math.h
Expand Up @@ -8,6 +8,13 @@
#define EPSILON 0.000001f
#define DEG2RAD(X) (X * M_PI / 180)

#ifndef MIN
#define MIN(x,y) (((x)<(y))?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) (((x)>(y))?(x):(y))
#endif

typedef struct {
union {
struct {
Expand Down
16 changes: 16 additions & 0 deletions src/engine/arcan_trace.c
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 07baa1f

Please sign in to comment.