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_console.h File Reference

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.

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 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().