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
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE 4096
9#define LOG_WASM_MSG_BUFFER_SIZE 4096
10
11#include <ascii-chat/log/log.h>
13#include <emscripten.h>
14#include <stddef.h>
15#include <stdint.h>
16#include <string.h>
17
18// EM_JS bridge: route to appropriate console method based on log level
19// level: 0=DEV, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=FATAL
20// Message contains the COMPLETE formatted log line with timestamp, level,
21// thread, file:line, etc.
22// clang-format off
23EM_JS(void, js_console_log, (int level, const char *message), {
24 const msg = UTF8ToString(message);
25 // Route to appropriate console method
26 switch (level) {
27 case 0: // LOG_DEV
28 case 1: // LOG_DEBUG
29 case 2: // LOG_INFO
30 console.log(msg);
31 break;
32 case 3: // LOG_WARN
33 console.warn(msg);
34 break;
35 case 4: // LOG_ERROR
36 case 5: // LOG_FATAL
37 console.error(msg);
38 break;
39 default:
40 console.log(msg);
41 }
42});
43// clang-format on
44
45// Platform hook called by logging system
46// This is called after each log message is formatted, before printing to
47// stderr/stdout Strip ANSI codes from the formatted message before sending it
48// to the console.
49void platform_log_hook(log_level_t level, const char *message) {
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}
57
58// Parse log level from formatted message: "[LEVEL] message..."
59// Returns the log_level_t enum value, or -1 if not found
60int wasm_parse_log_level(const uint8_t *buf, size_t count) {
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}
109
110// Public API for logging to console
111void wasm_log_to_console(int fd, const uint8_t *buf, size_t count) {
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}
unsigned char uint8_t
log_level_t
Logging levels enumeration.
Definition log/types.h:33
void strip_ansi_codes(const char *input, char *output, size_t output_size)
Strip ANSI escape codes from a string.
📝 Logging API with multiple log levels and terminal output control
#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
🔤 String Manipulation and Shell Escaping Utilities
#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
void platform_log_hook(log_level_t level, const char *message)
Definition wasm.c:49
#define LOG_WASM_CLEAN_MESSAGE_BUFFER_SIZE
Definition wasm.c:8
void wasm_log_to_console(int fd, const uint8_t *buf, size_t count)
Route output to browser console based on log level.
Definition wasm.c:111
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);} })