ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
wasm.c File Reference

WASM logging support for routing to browser console. More...

Go to the source code of this file.

Macros

#define LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE   4096
#define LOG_WASM_MSG_BUFFER_SIZE   4096

Functions

 EM_JS (void, js_console_log,(int level, const char *message), { const msg=UTF8ToString(message);switch(level) { case 0:case 1:case 2:console.log(msg);break;case 3:console.warn(msg);break;case 4:case 5:console.error(msg);break;default:console.log(msg);} })
void platform_log_hook (log_level_t level, const char *message)
int wasm_parse_log_level (const uint8_t *buf, size_t count)
 Parse log level from formatted message.
void wasm_log_to_console (int fd, const uint8_t *buf, size_t count)
 Route output to browser console based on log level.

Detailed Description

WASM logging support for routing to browser console.

Definition in file wasm.c.

Macro Definition Documentation

◆ LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE

#define LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE   4096

Definition at line 8 of file wasm.c.

Referenced by platform_log_hook().

◆ LOG_WASM_MSG_BUFFER_SIZE

#define LOG_WASM_MSG_BUFFER_SIZE   4096

Definition at line 9 of file wasm.c.

Referenced by wasm_log_to_console().

Function Documentation

◆ EM_JS()

EM_JS ( void ,
js_console_log ,
(int level, const char *message) ,
{ const msg=UTF8ToString(message);switch(level) { case 0:case 1:case 2:console.log(msg);break;case 3:console.warn(msg);break;case 4:case 5:console.error(msg);break;default:console.log(msg);} }  )

◆ platform_log_hook()

void platform_log_hook ( log_level_t level,
const char * message )

Definition at line 49 of file wasm.c.

49 {
50 if (message) {
51 // Strip ANSI codes for clean browser console output
52 char clean_message[LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE];
53 strip_ansi_codes(message, clean_message, sizeof(clean_message));
54 js_console_log((int)level, clean_message);
55 }
56}
void strip_ansi_codes(const char *input, char *output, size_t output_size)
Strip ANSI escape codes from a string.
#define LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE
Definition wasm.c:8

References LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE, and strip_ansi_codes().

◆ wasm_log_to_console()

void wasm_log_to_console ( int fd,
const uint8_t * buf,
size_t count )

Route output to browser console based on log level.

Parses the log level from the message format [LEVEL] and routes to the appropriate console method (console.debug, console.log, etc).

Parameters
fdFile descriptor (typically 1 for stdout, 2 for stderr)
bufMessage buffer
countMessage length

Definition at line 111 of file wasm.c.

111 {
112 // For WASM, route stdout/stderr to browser console
113 if ((fd == 1 || fd == 2) && buf && count > 0) {
114 // Parse log level from formatted message
115 int level = wasm_parse_log_level(buf, count);
116 if (level >= 0) {
117 // Create null-terminated string for console output
118 // Copy buffer to local array and null-terminate
119 static char msg_buffer[LOG_WASM_MSG_BUFFER_SIZE];
120 size_t copy_len =
121 (count < sizeof(msg_buffer) - 1) ? count : (sizeof(msg_buffer) - 1);
122 memcpy(msg_buffer, buf, copy_len);
123 msg_buffer[copy_len] = '\0';
124
125 // Remove trailing newline if present (console adds it automatically)
126 if (copy_len > 0 && msg_buffer[copy_len - 1] == '\n') {
127 msg_buffer[copy_len - 1] = '\0';
128 }
129
130 // Route to console
131 js_console_log(level, msg_buffer);
132 }
133 }
134}
#define LOG_WASM_MSG_BUFFER_SIZE
Definition wasm.c:9
int wasm_parse_log_level(const uint8_t *buf, size_t count)
Parse log level from formatted message.
Definition wasm.c:60

References LOG_WASM_MSG_BUFFER_SIZE, and wasm_parse_log_level().

◆ wasm_parse_log_level()

int wasm_parse_log_level ( const uint8_t * buf,
size_t count )

Parse log level from formatted message.

Expects format: "[LEVEL] message..."

Parameters
bufMessage buffer
countMessage length
Returns
log_level_t enum value (0-5), or -1 if not found

Definition at line 60 of file wasm.c.

60 {
61 if (!buf || count < 5) {
62 return -1; // Too short to contain [LEVEL]
63 }
64
65 // Check if message starts with [
66 if (buf[0] != '[') {
67 return -1;
68 }
69
70 // Look for closing ]
71 int bracket_end = -1;
72 for (size_t i = 1; i < count && i < 10; i++) {
73 if (buf[i] == ']') {
74 bracket_end = (int)i;
75 break;
76 }
77 }
78
79 if (bracket_end < 3) {
80 return -1; // Too short, minimum is [XX]
81 }
82
83 // Extract level string
84 int level_len = bracket_end - 1;
85 const char *level_start = (const char *)&buf[1];
86
87 // Match against known level strings (from logging.c)
88 if (level_len == 3 && strncmp(level_start, "DEV", 3) == 0) {
89 return LOG_DEV;
90 }
91 if (level_len == 5 && strncmp(level_start, "DEBUG", 5) == 0) {
92 return LOG_DEBUG;
93 }
94 if (level_len == 4 && strncmp(level_start, "INFO", 4) == 0) {
95 return LOG_INFO;
96 }
97 if (level_len == 4 && strncmp(level_start, "WARN", 4) == 0) {
98 return LOG_WARN;
99 }
100 if (level_len == 5 && strncmp(level_start, "ERROR", 5) == 0) {
101 return LOG_ERROR;
102 }
103 if (level_len == 5 && strncmp(level_start, "FATAL", 5) == 0) {
104 return LOG_FATAL;
105 }
106
107 return -1; // Level not recognized
108}
#define LOG_DEV
Definition log/types.h:42
#define LOG_DEBUG
Definition log/types.h:43
#define LOG_FATAL
Definition log/types.h:47
#define LOG_ERROR
Definition log/types.h:46
#define LOG_WARN
Definition log/types.h:45
#define LOG_INFO
Definition log/types.h:44

References LOG_DEBUG, LOG_DEV, LOG_ERROR, LOG_FATAL, LOG_INFO, and LOG_WARN.

Referenced by wasm_log_to_console().