ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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);const levelNames=[ 'DEV', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'];const levelName=(level >=0 &&level<=5) ? levelNames[level] :'???^;const msgLen=msg.length;const hasTimestamp=msg.startsWith('[') &&msg.includes(':') &&msg.includes('.') &&msg[msg.indexOf(']')+1]===' ';const debugInfo=`[WASM_MSG_LEN=${msgLen}, HAS_TS=${hasTimestamp}]`;switch(level) { case 0:case 1:case 2:console.log(debugInfo, msg);break;case 3:console.warn(debugInfo, msg);break;case 4:case 5:console.error(debugInfo, msg);break;default:console.log(debugInfo, 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);const levelNames=[ 'DEV', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'];const levelName=(level >=0 &&level<=5) ? levelNames[level] :'???^;const msgLen=msg.length;const hasTimestamp=msg.startsWith('[') &&msg.includes(':') &&msg.includes('.') &&msg[msg.indexOf(']')+1]===' ';const debugInfo=`[WASM_MSG_LEN=${msgLen}, HAS_TS=${hasTimestamp}]`;switch(level) { case 0:case 1:case 2:console.log(debugInfo, msg);break;case 3:console.warn(debugInfo, msg);break;case 4:case 5:console.error(debugInfo, msg);break;default:console.log(debugInfo, msg);} }  )

◆ platform_log_hook()

void platform_log_hook ( log_level_t level,
const char * message )

Definition at line 58 of file wasm.c.

58 {
59 if (message) {
60 // Strip ANSI codes for clean browser console output
61 static char clean_message[LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE];
62 strip_ansi_codes(message, clean_message, sizeof(clean_message));
63 js_console_log((int)level, clean_message);
64 }
65}
void strip_ansi_codes(const char *input, char *output, size_t output_size)
Strip ANSI escape codes from a string.
Definition string.c:374
#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 120 of file wasm.c.

120 {
121 // For WASM, route stdout/stderr to browser console
122 if ((fd == 1 || fd == 2) && buf && count > 0) {
123 // Parse log level from formatted message
124 int level = wasm_parse_log_level(buf, count);
125 if (level >= 0) {
126 // Create null-terminated string for console output
127 // Copy buffer to local array and null-terminate
128 static char msg_buffer[LOG_WASM_MSG_BUFFER_SIZE];
129 size_t copy_len = (count < sizeof(msg_buffer) - 1) ? count : (sizeof(msg_buffer) - 1);
130 memcpy(msg_buffer, buf, copy_len);
131 msg_buffer[copy_len] = '\0';
132
133 // Remove trailing newline if present (console adds it automatically)
134 if (copy_len > 0 && msg_buffer[copy_len - 1] == '\n') {
135 msg_buffer[copy_len - 1] = '\0';
136 }
137
138 // Route to console
139 js_console_log(level, msg_buffer);
140 }
141 }
142}
#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:69

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 69 of file wasm.c.

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

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

Referenced by wasm_log_to_console().