Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/libc/emscripten_console.c
6162 views
1
/*
2
* Copyright 2021 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
#include <alloca.h>
8
#include <stdarg.h>
9
#include <stdio.h>
10
11
#include <emscripten.h>
12
#include <emscripten/console.h>
13
#include "emscripten_internal.h"
14
15
static void vlogf(const char* fmt, va_list ap, void (*callback)(const char*)) {
16
va_list ap2;
17
va_copy(ap2, ap);
18
size_t len = vsnprintf(0, 0, fmt, ap2);
19
va_end(ap2);
20
char* buf = alloca(len + 1);
21
vsnprintf(buf, len + 1, fmt, ap);
22
callback(buf);
23
}
24
25
void emscripten_console_logf(const char* fmt, ...) {
26
va_list ap;
27
va_start(ap, fmt);
28
vlogf(fmt, ap, &emscripten_console_log);
29
va_end(ap);
30
}
31
32
void emscripten_console_errorf(const char* fmt, ...) {
33
va_list ap;
34
va_start(ap, fmt);
35
vlogf(fmt, ap, &emscripten_console_error);
36
va_end(ap);
37
}
38
39
void emscripten_console_warnf(const char* fmt, ...) {
40
va_list ap;
41
va_start(ap, fmt);
42
vlogf(fmt, ap, &emscripten_console_warn);
43
va_end(ap);
44
}
45
46
void emscripten_console_tracef(const char* fmt, ...) {
47
va_list ap;
48
va_start(ap, fmt);
49
vlogf(fmt, ap, &emscripten_console_trace);
50
va_end(ap);
51
}
52
53
void emscripten_outf(const char* fmt, ...) {
54
va_list ap;
55
va_start(ap, fmt);
56
vlogf(fmt, ap, &emscripten_out);
57
va_end(ap);
58
}
59
60
void emscripten_errf(const char* fmt, ...) {
61
va_list ap;
62
va_start(ap, fmt);
63
vlogf(fmt, ap, &emscripten_err);
64
va_end(ap);
65
}
66
67
#ifndef NDEBUG
68
void emscripten_dbgf(const char* fmt, ...) {
69
va_list ap;
70
va_start(ap, fmt);
71
vlogf(fmt, ap, &emscripten_dbg);
72
va_end(ap);
73
}
74
75
void emscripten_dbg_backtracef(const char* fmt, ...) {
76
va_list ap;
77
va_start(ap, fmt);
78
vlogf(fmt, ap, &emscripten_dbg_backtrace);
79
va_end(ap);
80
}
81
#endif
82
83
void emscripten_log(int flags, const char* fmt, ...) {
84
va_list ap;
85
va_start(ap, fmt);
86
// Note: we have to inline `vlogf` here instead of using it, because
87
// `_emscripten_log_formatted` has a different signature than the
88
// `callback` parameter of `vlogf`, and we can't use it without a callback
89
// as we need `alloca` to remain on stack.
90
va_list ap2;
91
va_copy(ap2, ap);
92
size_t len = vsnprintf(0, 0, fmt, ap2);
93
va_end(ap2);
94
char* buf = alloca(len + 1);
95
vsnprintf(buf, len + 1, fmt, ap);
96
va_end(ap);
97
_emscripten_log_formatted(flags, buf);
98
}
99
100