Skip to content

Add printing out debug logs using external output #1439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/iotjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@

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

IOTJS_EXTERN_C void iotjs_conf_console_out(int (*fp)(int level, const char* fmt,
...));

#endif /* IOTJS_IOTJS_H */
4 changes: 4 additions & 0 deletions src/iotjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ static void iotjs_uv_walk_to_close_callback(uv_handle_t* handle, void* arg) {
iotjs_handlewrap_close(handle_wrap, NULL);
}

void iotjs_conf_console_out(int (*out)(int lv, const char* fmt, ...)) {
iotjs_set_console_out(out);
}

int iotjs_entry(int argc, char** argv) {
int ret_code = 0;

Expand Down
7 changes: 6 additions & 1 deletion src/iotjs_debuglog.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

#include "iotjs_debuglog.h"

iotjs_console_out_t iotjs_console_out = NULL;

void iotjs_set_console_out(iotjs_console_out_t output) {
iotjs_console_out = output;
}

#ifdef ENABLE_DEBUG_LOG
int iotjs_debug_level = DBGLEV_ERR;
FILE* iotjs_log_stream;
Expand Down Expand Up @@ -53,7 +59,6 @@ void iotjs_debuglog_init() {
#endif // ENABLE_DEBUG_LOG
}


void iotjs_debuglog_release() {
#ifdef ENABLE_DEBUG_LOG
if (iotjs_log_stream != stderr && iotjs_log_stream != stdout) {
Expand Down
31 changes: 19 additions & 12 deletions src/iotjs_debuglog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
#ifndef IOTJS_DEBUGLOG_H
#define IOTJS_DEBUGLOG_H

#define DBGLEV_ERR 1
#define DBGLEV_WARN 2
#define DBGLEV_INFO 3

typedef int (*iotjs_console_out_t)(int level, const char* format, ...);
extern iotjs_console_out_t iotjs_console_out;
extern void iotjs_set_console_out(iotjs_console_out_t output);

#ifdef ENABLE_DEBUG_LOG

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

#define DBGLEV_ERR 1
#define DBGLEV_WARN 2
#define DBGLEV_INFO 3

#define IOTJS_DLOG(lvl, ...) \
do { \
if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \
fprintf(iotjs_log_stream, "[%s] ", iotjs_debug_prefix[lvl]); \
fprintf(iotjs_log_stream, __VA_ARGS__); \
fprintf(iotjs_log_stream, "\n"); \
fflush(iotjs_log_stream); \
} \
#define IOTJS_DLOG(lvl, ...) \
do { \
if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \
if (iotjs_console_out) { \
iotjs_console_out(lvl, __VA_ARGS__); \
} else { \
fprintf(iotjs_log_stream, "[%s] ", iotjs_debug_prefix[lvl]); \
fprintf(iotjs_log_stream, __VA_ARGS__); \
fprintf(iotjs_log_stream, "\n"); \
fflush(iotjs_log_stream); \
} \
} \
} while (0)
#define DLOG(...) IOTJS_DLOG(DBGLEV_ERR, __VA_ARGS__)
#define DDLOG(...) IOTJS_DLOG(DBGLEV_WARN, __VA_ARGS__)
Expand Down
17 changes: 12 additions & 5 deletions src/modules/iotjs_module_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include "iotjs_def.h"
#include "iotjs_debuglog.h"

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

for (idx = 0; idx < str_len; idx++) {
if (str[idx] != 0) {
fprintf(out_fd, "%c", str[idx]);
} else {
fprintf(out_fd, "\\u0000");
if (iotjs_console_out) {
int level = (out_fd == stdout) ? DBGLEV_INFO : DBGLEV_ERR;
iotjs_console_out(level, "%s", str);
} else {
for (idx = 0; idx < str_len; idx++) {
if (str[idx] != 0) {
fprintf(out_fd, "%c", str[idx]);
} else {
fprintf(out_fd, "\\u0000");
}
}
}

iotjs_string_destroy(&msg);
return jerry_create_undefined();
}
Expand Down