Skip to content

Commit c6e391e

Browse files
daeyeonLaszloLango
authored andcommitted
Add printing out debug logs using external output (#1439)
IoT.js-DCO-1.0-Signed-off-by: Daeyeon Jeong [email protected]
1 parent 45139e7 commit c6e391e

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

include/iotjs.h

+2
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626

2727
IOTJS_EXTERN_C int iotjs_entry(int argc, char** argv);
2828

29+
IOTJS_EXTERN_C void iotjs_conf_console_out(int (*fp)(int level, const char* fmt,
30+
...));
2931

3032
#endif /* IOTJS_IOTJS_H */

src/iotjs.c

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ static void iotjs_uv_walk_to_close_callback(uv_handle_t* handle, void* arg) {
172172
iotjs_handlewrap_close(handle_wrap, NULL);
173173
}
174174

175+
void iotjs_conf_console_out(int (*out)(int lv, const char* fmt, ...)) {
176+
iotjs_set_console_out(out);
177+
}
178+
175179
int iotjs_entry(int argc, char** argv) {
176180
int ret_code = 0;
177181

src/iotjs_debuglog.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
#include "iotjs_debuglog.h"
2020

21+
iotjs_console_out_t iotjs_console_out = NULL;
22+
23+
void iotjs_set_console_out(iotjs_console_out_t output) {
24+
iotjs_console_out = output;
25+
}
26+
2127
#ifdef ENABLE_DEBUG_LOG
2228
int iotjs_debug_level = DBGLEV_ERR;
2329
FILE* iotjs_log_stream;
@@ -53,7 +59,6 @@ void iotjs_debuglog_init() {
5359
#endif // ENABLE_DEBUG_LOG
5460
}
5561

56-
5762
void iotjs_debuglog_release() {
5863
#ifdef ENABLE_DEBUG_LOG
5964
if (iotjs_log_stream != stderr && iotjs_log_stream != stdout) {

src/iotjs_debuglog.h

+19-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
#ifndef IOTJS_DEBUGLOG_H
1717
#define IOTJS_DEBUGLOG_H
1818

19+
#define DBGLEV_ERR 1
20+
#define DBGLEV_WARN 2
21+
#define DBGLEV_INFO 3
22+
23+
typedef int (*iotjs_console_out_t)(int level, const char* format, ...);
24+
extern iotjs_console_out_t iotjs_console_out;
25+
extern void iotjs_set_console_out(iotjs_console_out_t output);
1926

2027
#ifdef ENABLE_DEBUG_LOG
2128

@@ -25,18 +32,18 @@ extern int iotjs_debug_level;
2532
extern FILE* iotjs_log_stream;
2633
extern const char* iotjs_debug_prefix[4];
2734

28-
#define DBGLEV_ERR 1
29-
#define DBGLEV_WARN 2
30-
#define DBGLEV_INFO 3
31-
32-
#define IOTJS_DLOG(lvl, ...) \
33-
do { \
34-
if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \
35-
fprintf(iotjs_log_stream, "[%s] ", iotjs_debug_prefix[lvl]); \
36-
fprintf(iotjs_log_stream, __VA_ARGS__); \
37-
fprintf(iotjs_log_stream, "\n"); \
38-
fflush(iotjs_log_stream); \
39-
} \
35+
#define IOTJS_DLOG(lvl, ...) \
36+
do { \
37+
if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \
38+
if (iotjs_console_out) { \
39+
iotjs_console_out(lvl, __VA_ARGS__); \
40+
} else { \
41+
fprintf(iotjs_log_stream, "[%s] ", iotjs_debug_prefix[lvl]); \
42+
fprintf(iotjs_log_stream, __VA_ARGS__); \
43+
fprintf(iotjs_log_stream, "\n"); \
44+
fflush(iotjs_log_stream); \
45+
} \
46+
} \
4047
} while (0)
4148
#define DLOG(...) IOTJS_DLOG(DBGLEV_ERR, __VA_ARGS__)
4249
#define DDLOG(...) IOTJS_DLOG(DBGLEV_WARN, __VA_ARGS__)

src/modules/iotjs_module_console.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "iotjs_def.h"
17+
#include "iotjs_debuglog.h"
1718

1819
// This function should be able to print utf8 encoded string
1920
// as utf8 is internal string representation in Jerryscript
@@ -25,13 +26,19 @@ static jerry_value_t Print(const jerry_value_t* jargv,
2526
unsigned str_len = iotjs_string_size(&msg);
2627
unsigned idx = 0;
2728

28-
for (idx = 0; idx < str_len; idx++) {
29-
if (str[idx] != 0) {
30-
fprintf(out_fd, "%c", str[idx]);
31-
} else {
32-
fprintf(out_fd, "\\u0000");
29+
if (iotjs_console_out) {
30+
int level = (out_fd == stdout) ? DBGLEV_INFO : DBGLEV_ERR;
31+
iotjs_console_out(level, "%s", str);
32+
} else {
33+
for (idx = 0; idx < str_len; idx++) {
34+
if (str[idx] != 0) {
35+
fprintf(out_fd, "%c", str[idx]);
36+
} else {
37+
fprintf(out_fd, "\\u0000");
38+
}
3339
}
3440
}
41+
3542
iotjs_string_destroy(&msg);
3643
return jerry_create_undefined();
3744
}

0 commit comments

Comments
 (0)