2
2
#include " ../../../StarlingMonkey/runtime/encode.h"
3
3
#include " ../host-api/host_api_fastly.h"
4
4
5
+ namespace builtins ::web::console {
6
+
7
+ class Console : public BuiltinNoConstructor <Console> {
8
+ private:
9
+ public:
10
+ static constexpr const char *class_name = " Console" ;
11
+ enum LogType {
12
+ Log,
13
+ Info,
14
+ Debug,
15
+ Warn,
16
+ Error,
17
+ };
18
+ enum Slots { Count };
19
+ static const JSFunctionSpec methods[];
20
+ static const JSPropertySpec properties[];
21
+ };
22
+
23
+ bool write_stderr = false ;
24
+ bool write_prefix = false ;
25
+
26
+ void builtin_impl_console_log (Console::LogType log_ty, const char *msg) {
27
+ FILE *output = stdout;
28
+ if (write_stderr) {
29
+ if (log_ty == Console::LogType::Warn || log_ty == Console::LogType::Error) {
30
+ output = stderr;
31
+ }
32
+ }
33
+ if (write_prefix) {
34
+ const char *prefix = " " ;
35
+ switch (log_ty) {
36
+ case Console::LogType::Log:
37
+ prefix = " Log" ;
38
+ break ;
39
+ case Console::LogType::Debug:
40
+ prefix = " Debug" ;
41
+ break ;
42
+ case Console::LogType::Info:
43
+ prefix = " Info" ;
44
+ break ;
45
+ case Console::LogType::Warn:
46
+ prefix = " Warn" ;
47
+ break ;
48
+ case Console::LogType::Error:
49
+ prefix = " Error" ;
50
+ break ;
51
+ }
52
+ fprintf (output, " %s: %s\n " , prefix, msg);
53
+ fflush (output);
54
+ } else {
55
+ fprintf (output, " %s\n " , msg);
56
+ fflush (output);
57
+ }
58
+ }
59
+
60
+ } // namespace builtins::web::console
61
+
5
62
namespace fastly ::logger {
6
63
7
64
bool Logger::log (JSContext *cx, unsigned argc, JS::Value *vp) {
@@ -77,6 +134,50 @@ bool Logger::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
77
134
return true ;
78
135
}
79
136
137
+ bool configure_console (JSContext *cx, unsigned argc, JS::Value *vp) {
138
+ JS::CallArgs args = JS::CallArgsFromVp (argc, vp);
139
+
140
+ // Check if we have at least one argument and it's an object
141
+ if (args.length () < 1 || !args[0 ].isObject ()) {
142
+ JS_ReportErrorUTF8 (cx, " configureConsole requires an options object as its argument" );
143
+ return false ;
144
+ }
145
+
146
+ // Get the options object
147
+ JS::RootedObject options (cx, &args[0 ].toObject ());
148
+ JS::RootedValue val (cx);
149
+
150
+ // Handle prefixing option
151
+ if (JS_GetProperty (cx, options, " prefixing" , &val)) {
152
+ if (!val.isUndefined ()) {
153
+ if (!val.isBoolean ()) {
154
+ JS_ReportErrorUTF8 (cx, " prefixing option must be a boolean" );
155
+ return false ;
156
+ }
157
+ builtins::web::console::write_prefix = val.toBoolean ();
158
+ }
159
+ } else {
160
+ return false ;
161
+ }
162
+
163
+ // Handle stderr option
164
+ if (JS_GetProperty (cx, options, " stderr" , &val)) {
165
+ if (!val.isUndefined ()) {
166
+ if (!val.isBoolean ()) {
167
+ JS_ReportErrorUTF8 (cx, " stderr option must be a boolean" );
168
+ return false ;
169
+ }
170
+ builtins::web::console::write_stderr = val.toBoolean ();
171
+ }
172
+ } else {
173
+ return false ;
174
+ }
175
+
176
+ // Set the return value to undefined
177
+ args.rval ().setUndefined ();
178
+ return true ;
179
+ }
180
+
80
181
bool install (api::Engine *engine) {
81
182
if (!Logger::init_class_impl (engine->cx (), engine->global ())) {
82
183
return false ;
@@ -89,6 +190,16 @@ bool install(api::Engine *engine) {
89
190
if (!JS_SetProperty (engine->cx (), logger_ns_obj, " Logger" , logger_val)) {
90
191
return false ;
91
192
}
193
+ auto configure_console_fn =
194
+ JS_NewFunction (engine->cx (), &configure_console, 1 , 0 , " configureConsole" );
195
+ RootedObject configure_console_obj (engine->cx (), JS_GetFunctionObject (configure_console_fn));
196
+ RootedValue configure_console_val (engine->cx (), ObjectValue (*configure_console_obj));
197
+ if (!JS_SetProperty (engine->cx (), logger_ns_obj, " configureConsole" , configure_console_val)) {
198
+ return false ;
199
+ }
200
+ if (!JS_SetProperty (engine->cx (), logger_obj, " configureConsole" , configure_console_val)) {
201
+ return false ;
202
+ }
92
203
if (!engine->define_builtin_module (" fastly:logger" , logger_ns_val)) {
93
204
return false ;
94
205
}
0 commit comments