ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
wasm_console.h File Reference

WASM browser console logging API. More...

Go to the source code of this file.

Functions

void wasm_log_to_console (int fd, const uint8_t *buf, size_t count)
 Route output to browser console based on log level.
 
int wasm_parse_log_level (const uint8_t *buf, size_t count)
 Parse log level from formatted message.
 

Detailed Description

WASM browser console logging API.

Definition in file wasm_console.h.

Function Documentation

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

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

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

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