diff --git a/jerry-core/jerry-api.h b/jerry-core/jerry-api.h index 82868d5b55..483f7c892b 100644 --- a/jerry-core/jerry-api.h +++ b/jerry-core/jerry-api.h @@ -162,7 +162,6 @@ typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_na */ #ifdef JERRY_ENABLE_LOG extern int jerry_debug_level; -extern FILE *jerry_log_file; #endif /* JERRY_ENABLE_LOG */ /** diff --git a/jerry-core/jerry-port.h b/jerry-core/jerry-port.h index 4660d2960e..d5d1faa4e3 100644 --- a/jerry-core/jerry-port.h +++ b/jerry-core/jerry-port.h @@ -29,12 +29,6 @@ extern "C" * @{ */ -/** - * Target port functions for console output - */ -int jerry_port_logmsg (FILE *stream, const char *format, ...); -int jerry_port_errormsg (const char *format, ...); - /* * Termination Port API * @@ -71,6 +65,54 @@ typedef enum */ void jerry_port_fatal (jerry_fatal_code_t code); +/* + * I/O Port API + */ + +/** + * Print a string to the console. The function should implement a printf-like + * interface, where the first argument specifies a format string on how to + * stringify the rest of the parameter list. + * + * This function is only called with strings coming from the executed ECMAScript + * wanting to print something as the result of its normal operation. + * + * It should be the port that decides what a "console" is. + * + * Example: a libc-based port may implement this with vprintf(). + */ +void jerry_port_console (const char *format, ...); + +/** + * Jerry log levels. The levels are in severity order + * where the most serious levels come first. + */ +typedef enum +{ + JERRY_LOG_LEVEL_ERROR, /**< the engine will terminate after the message is printed */ + JERRY_LOG_LEVEL_WARNING, /**< a request is aborted, but the engine continues its operation */ + JERRY_LOG_LEVEL_DEBUG, /**< debug messages from the engine, low volume */ + JERRY_LOG_LEVEL_TRACE /**< detailed info about engine internals, potentially high volume */ +} jerry_log_level_t; + +/** + * Display or log a debug/error message. The function should implement a printf-like + * interface, where the first argument specifies the log level + * and the second argument specifies a format string on how to stringify the rest + * of the parameter list. + * + * This function is only called with messages coming from the jerry engine as + * the result of some abnormal operation or describing its internal operations + * (e.g., data structure dumps or tracing info). + * + * It should be the port that decides whether error and debug messages are logged to + * the console, or saved to a database or to a file. + * + * Example: a libc-based port may implement this with vfprintf(stderr) or + * vfprintf(logfile), or both, depending on log level. + */ +void jerry_port_log (jerry_log_level_t level, const char *format, ...); + /* * Date Port API */ diff --git a/jerry-core/jerry.c b/jerry-core/jerry.c index 49479f5091..743d058dd2 100644 --- a/jerry-core/jerry.c +++ b/jerry-core/jerry.c @@ -86,11 +86,6 @@ static const char *wrong_args_msg_p = "wrong type of argument"; */ int jerry_debug_level = 0; -/** - * File, used for logging - */ -FILE *jerry_log_file = NULL; - #endif /* JERRY_ENABLE_LOG */ /** diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index 27000a9529..28ba4ccf29 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -86,9 +86,9 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch #define JERRY_LOG(lvl, ...) \ do \ { \ - if (lvl <= jerry_debug_level && jerry_log_file) \ + if (lvl <= jerry_debug_level) \ { \ - jerry_port_logmsg (jerry_log_file, __VA_ARGS__); \ + jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__); \ } \ } \ while (0) @@ -109,8 +109,8 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch #define JERRY_DDDLOG(...) JERRY_DLOG (__VA_ARGS__) #endif /* JERRY_ENABLE_LOG */ -#define JERRY_ERROR_MSG(...) jerry_port_errormsg (__VA_ARGS__) -#define JERRY_WARNING_MSG(...) JERRY_ERROR_MSG (__VA_ARGS__) +#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__) +#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__) /** * Mark for unreachable points and unimplemented cases diff --git a/main-unix.c b/main-unix.c index 66aa723af3..034c34f58e 100644 --- a/main-unix.c +++ b/main-unix.c @@ -48,14 +48,14 @@ read_file (const char *file_name, FILE *file = fopen (file_name, "r"); if (file == NULL) { - jerry_port_errormsg ("Error: failed to open file: %s\n", file_name); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name); return NULL; } size_t bytes_read = fread (buffer, 1u, sizeof (buffer), file); if (!bytes_read) { - jerry_port_errormsg ("Error: failed to read file: %s\n", file_name); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name); fclose (file); return NULL; } @@ -85,7 +85,7 @@ assert_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< f } else { - jerry_port_errormsg ("Script error: assertion failed\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script error: assertion failed\n"); exit (JERRY_STANDALONE_EXIT_CODE_FAIL); } } /* assert_handler */ @@ -114,7 +114,6 @@ print_help (char *name) " --save-snapshot-for-eval FILE\n" " --exec-snapshot FILE\n" " --log-level [0-3]\n" - " --log-file FILE\n" " --abort-on-fail\n" "\n", name); @@ -126,8 +125,10 @@ main (int argc, { if (argc > JERRY_MAX_COMMAND_LINE_ARGS) { - jerry_port_errormsg ("Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n", - argc, JERRY_MAX_COMMAND_LINE_ARGS); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, + "Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n", + argc, + JERRY_MAX_COMMAND_LINE_ARGS); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -154,9 +155,6 @@ main (int argc, bool is_repl_mode = false; -#ifdef JERRY_ENABLE_LOG - const char *log_file_name = NULL; -#endif /* JERRY_ENABLE_LOG */ for (i = 1; i < argc; i++) { if (!strcmp ("-h", argv[i]) || !strcmp ("--help", argv[i])) @@ -193,14 +191,14 @@ main (int argc, if (save_snapshot_file_name_p != NULL) { - jerry_port_errormsg ("Error: snapshot file name already specified\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: snapshot file name already specified\n"); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (++i >= argc) { - jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -211,7 +209,7 @@ main (int argc, { if (++i >= argc) { - jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -223,14 +221,14 @@ main (int argc, { if (++i >= argc) { - jerry_port_errormsg ("Error: no level specified for %s\n", argv[i - 1]); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no level specified for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (strlen (argv[i]) != 1 || argv[i][0] < '0' || argv[i][0] > '3') { - jerry_port_errormsg ("Error: wrong format for %s\n", argv[i - 1]); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -238,20 +236,6 @@ main (int argc, #ifdef JERRY_ENABLE_LOG flags |= JERRY_INIT_ENABLE_LOG; jerry_debug_level = argv[i][0] - '0'; -#endif /* JERRY_ENABLE_LOG */ - } - else if (!strcmp ("--log-file", argv[i])) - { - if (++i >= argc) - { - jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]); - print_usage (argv[0]); - return JERRY_STANDALONE_EXIT_CODE_FAIL; - } - -#ifdef JERRY_ENABLE_LOG - flags |= JERRY_INIT_ENABLE_LOG; - log_file_name = argv[i]; #endif /* JERRY_ENABLE_LOG */ } else if (!strcmp ("--abort-on-fail", argv[i])) @@ -260,7 +244,7 @@ main (int argc, } else if (!strncmp ("-", argv[i], 1)) { - jerry_port_errormsg ("Error: unrecognized option: %s\n", argv[i]); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: unrecognized option: %s\n", argv[i]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -274,13 +258,15 @@ main (int argc, { if (files_counter != 1) { - jerry_port_errormsg ("Error: --save-snapshot argument works with exactly one script\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, + "Error: --save-snapshot argument works with exactly one script\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (exec_snapshots_count != 0) { - jerry_port_errormsg ("Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, + "Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } } @@ -291,22 +277,6 @@ main (int argc, is_repl_mode = true; } -#ifdef JERRY_ENABLE_LOG - if (log_file_name) - { - jerry_log_file = fopen (log_file_name, "w"); - if (jerry_log_file == NULL) - { - jerry_port_errormsg ("Error: failed to open log file: %s\n", log_file_name); - return JERRY_STANDALONE_EXIT_CODE_FAIL; - } - } - else - { - jerry_log_file = stdout; - } -#endif /* JERRY_ENABLE_LOG */ - jerry_init (flags); jerry_value_t global_obj_val = jerry_get_global_object (); @@ -321,7 +291,7 @@ main (int argc, if (!is_assert_added) { - jerry_port_errormsg ("Warning: failed to register 'assert' method."); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Warning: failed to register 'assert' method."); } jerry_value_t ret_value = jerry_create_undefined (); @@ -466,14 +436,6 @@ main (int argc, jerry_release_value (print_function); } -#ifdef JERRY_ENABLE_LOG - if (jerry_log_file && jerry_log_file != stdout) - { - fclose (jerry_log_file); - jerry_log_file = NULL; - } -#endif /* JERRY_ENABLE_LOG */ - int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; if (jerry_value_has_error_flag (ret_value)) @@ -489,7 +451,7 @@ main (int argc, assert (sz == err_str_size); err_str_buf[err_str_size] = 0; - jerry_port_errormsg ("Script Error: unhandled exception: %s\n", err_str_buf); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: unhandled exception: %s\n", err_str_buf); jerry_release_value (err_str_val); diff --git a/targets/default/jerry-port-default-io.c b/targets/default/jerry-port-default-io.c index ec653da96c..8ad3f1453d 100644 --- a/targets/default/jerry-port-default-io.c +++ b/targets/default/jerry-port-default-io.c @@ -18,30 +18,30 @@ #include "jerry-port.h" /** - * Provide log message to filestream implementation for the engine. + * Provide console message implementation for the engine. */ -int jerry_port_logmsg (FILE *stream, /**< stream pointer */ - const char *format, /**< format string */ - ...) /**< parameters */ +void +jerry_port_console (const char *format, /**< format string */ + ...) /**< parameters */ { va_list args; - int count; va_start (args, format); - count = vfprintf (stream, format, args); + vfprintf (stdout, format, args); va_end (args); - return count; -} /* jerry_port_logmsg */ +} /* jerry_port_console */ /** - * Provide error message to console implementation for the engine. + * Provide log message implementation for the engine. */ -int jerry_port_errormsg (const char *format, /**< format string */ - ...) /**< parameters */ +void +jerry_port_log (jerry_log_level_t level, /**< log level */ + const char *format, /**< format string */ + ...) /**< parameters */ { + (void) level; /* default port implementation ignores the log level */ + va_list args; - int count; va_start (args, format); - count = vfprintf (stderr, format, args); + vfprintf (stderr, format, args); va_end (args); - return count; -} /* jerry_port_errormsg */ +} /* jerry_port_log */ diff --git a/targets/esp8266/user/jerry_port.c b/targets/esp8266/user/jerry_port.c index 2c3fffe33c..0b55f48a76 100644 --- a/targets/esp8266/user/jerry_port.c +++ b/targets/esp8266/user/jerry_port.c @@ -17,34 +17,36 @@ #include #include - /** - * Provide log message to filestream implementation for the engine. + * Provide console message implementation for the engine. */ -int jerry_port_logmsg (FILE* stream, const char* format, ...) +void +jerry_port_console (const char *format, /**< format string */ + ...) /**< parameters */ { va_list args; - int count = 0; va_start (args, format); - // TODO, uncomment when vfprint link is ok - //count = vfprintf (stream, format, args); + /* TODO, uncomment when vprint link is ok */ + /* vfprintf (stdout, format, args); */ va_end (args); - return count; -} +} /* jerry_port_console */ /** - * Provide error message to console implementation for the engine. + * Provide log message implementation for the engine. */ -int jerry_port_errormsg (const char* format, ...) +void +jerry_port_log (jerry_log_level_t level, /**< log level */ + const char *format, /**< format string */ + ...) /**< parameters */ { + (void) level; /* ignore log level */ + va_list args; - int count = 0; va_start (args, format); - // TODO, uncomment when vprint link is ok - //count = vprintf (format, args); + /* TODO, uncomment when vprint link is ok */ + /* vprintf (stderr, format, args); */ va_end (args); - return count; -} +} /* jerry_port_log */ /** exit - cause normal process termination */ diff --git a/targets/mbed/source/port/jerry_port.c b/targets/mbed/source/port/jerry_port.c index 8e4b5bedb6..2fce5d8c4c 100644 --- a/targets/mbed/source/port/jerry_port.c +++ b/targets/mbed/source/port/jerry_port.c @@ -23,35 +23,33 @@ #include "mbed-hal/us_ticker_api.h" /** - * Provide log message to filestream implementation for the engine. + * Provide console message implementation for the engine. */ -int -jerry_port_logmsg (FILE *stream, /**< stream pointer */ - const char *format, /**< format string */ - ...) /**< parameters */ +void +jerry_port_console (const char *format, /**< format string */ + ...) /**< parameters */ { va_list args; - int count; va_start (args, format); - count = vfprintf (stream, format, args); + vfprintf (stdout, format, args); va_end (args); - return count; -} /* jerry_port_logmsg */ +} /* jerry_port_console */ /** - * Provide error message to console implementation for the engine. + * Provide log message implementation for the engine. */ -int -jerry_port_errormsg (const char *format, /**< format string */ - ...) /**< parameters */ +void +jerry_port_log (jerry_log_level_t level, /**< log level */ + const char *format, /**< format string */ + ...) /**< parameters */ { + (void) level; /* ignore log level */ + va_list args; - int count; va_start (args, format); - count = vfprintf (stderr, format, args); + vfprintf (stderr, format, args); va_end (args); - return count; -} /* jerry_port_errormsg */ +} /* jerry_port_log */ /** * Implementation of jerry_port_fatal. diff --git a/targets/nuttx-stm32f4/main-nuttx.c b/targets/nuttx-stm32f4/main-nuttx.c index df3bc0111c..ae0f74c39d 100644 --- a/targets/nuttx-stm32f4/main-nuttx.c +++ b/targets/nuttx-stm32f4/main-nuttx.c @@ -1,4 +1,5 @@ /* Copyright 2016 Samsung Electronics Co., Ltd. + * Copyright 2016 University of Szeged. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,14 +55,14 @@ static char* read_sources (const char *script_file_names[], file = fopen (script_file_name, "r"); if (file == NULL) { - jerry_port_errormsg ("Failed to fopen [%s]\n", script_file_name); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fopen [%s]\n", script_file_name); return NULL; } int fseek_status = fseek (file, 0, SEEK_END); if (fseek_status != 0) { - jerry_port_errormsg ("Failed to fseek fseek_status(%d)\n", fseek_status); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fseek fseek_status(%d)\n", fseek_status); fclose (file); return NULL; } @@ -69,7 +70,7 @@ static char* read_sources (const char *script_file_names[], long script_len = ftell (file); if (script_len < 0) { - jerry_port_errormsg ("Failed to ftell script_len(%ld)\n", script_len); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to ftell script_len(%ld)\n", script_len); fclose (file); break; } @@ -82,14 +83,14 @@ static char* read_sources (const char *script_file_names[], if (total_length <= 0) { - jerry_port_errormsg ("Theres noting to read\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Theres noting to read\n"); return NULL; } source_buffer = (char*)malloc(total_length); if (source_buffer == NULL) { - jerry_port_errormsg ("Out of memory error\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Out of memory error\n"); return NULL; } memset(source_buffer, 0, sizeof(char)*total_length); @@ -102,21 +103,21 @@ static char* read_sources (const char *script_file_names[], if (file == NULL) { - jerry_port_errormsg ("Failed to fopen [%s]\n", script_file_name); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fopen [%s]\n", script_file_name); break; } int fseek_status = fseek (file, 0, SEEK_END); if (fseek_status != 0) { - jerry_port_errormsg ("Failed to fseek fseek_status(%d)\n", fseek_status); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fseek fseek_status(%d)\n", fseek_status); break; } long script_len = ftell (file); if (script_len < 0) { - jerry_port_errormsg ("Failed to ftell script_len(%ld)\n", script_len); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to ftell script_len(%ld)\n", script_len); break; } @@ -126,7 +127,7 @@ static char* read_sources (const char *script_file_names[], size_t bytes_read = fread (source_buffer_tail, 1, current_source_size, file); if (bytes_read < current_source_size) { - jerry_port_errormsg ("Failed to fread bytes_read(%d)\n", bytes_read); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fread bytes_read(%d)\n", bytes_read); break; } @@ -143,7 +144,7 @@ static char* read_sources (const char *script_file_names[], if (i < files_count) { - jerry_port_errormsg ("Failed to read script N%d\n", i + 1); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to read script N%d\n", i + 1); free(source_buffer); return NULL; } @@ -157,8 +158,9 @@ int jerryscript_entry (int argc, char *argv[]) { if (argc >= JERRY_MAX_COMMAND_LINE_ARGS) { - jerry_port_errormsg ("Too many command line arguments. Current maximum is %d\n", - JERRY_MAX_COMMAND_LINE_ARGS); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, + "Too many command line arguments. Current maximum is %d\n", + JERRY_MAX_COMMAND_LINE_ARGS); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -213,7 +215,7 @@ int jerryscript_entry (int argc, char *argv[]) } else { - jerry_port_errormsg ("Error: wrong format or invalid argument\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format or invalid argument\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } } @@ -234,7 +236,7 @@ int jerryscript_entry (int argc, char *argv[]) if (source_p == NULL) { - jerry_port_errormsg ("JERRY_STANDALONE_EXIT_CODE_FAIL\n"); + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "JERRY_STANDALONE_EXIT_CODE_FAIL\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -252,27 +254,30 @@ int jerryscript_entry (int argc, char *argv[]) } /** - * Provide log message to filestream implementation for the engine. + * Provide console message implementation for the engine. */ -int jerry_port_logmsg (FILE* stream, const char* format, ...) +void +jerry_port_console (const char *format, /**< format string */ + ...) /**< parameters */ { va_list args; - int count; va_start (args, format); - count = vfprintf (stream, format, args); + vfprintf (stdout, format, args); va_end (args); - return count; -} +} /* jerry_port_console */ /** - * Provide error message to console implementation for the engine. + * Provide log message implementation for the engine. */ -int jerry_port_errormsg (const char* format, ...) +void +jerry_port_log (jerry_log_level_t level, /**< log level */ + const char *format, /**< format string */ + ...) /**< parameters */ { + (void) level; /* ignore the log level */ + va_list args; - int count; va_start (args, format); - count = vfprintf (stderr, format, args); + vfprintf (stderr, format, args); va_end (args); - return count; -} +} /* jerry_port_log */ diff --git a/targets/zephyr/src/jerry-entry.c b/targets/zephyr/src/jerry-entry.c index c75868affb..42c8937b07 100755 --- a/targets/zephyr/src/jerry-entry.c +++ b/targets/zephyr/src/jerry-entry.c @@ -18,28 +18,31 @@ #include "jerry-port.h" /** - * Provide log message to filestream implementation for the engine. + * Provide console message implementation for the engine. */ -int jerry_port_logmsg (FILE *stream, const char *format, ...) +void +jerry_port_console (const char *format, /**< format string */ + ...) /**< parameters */ { va_list args; - int count; va_start (args, format); - count = vfprintf (stream, format, args); + vfprintf (stdout, format, args); va_end (args); - return count; -} /* jerry_port_logmsg */ +} /* jerry_port_console */ /** - * Provide error message to console implementation for the engine. + * Provide log message implementation for the engine. */ -int jerry_port_errormsg (const char *format, ...) +void +jerry_port_log (jerry_log_level_t level, /**< log level */ + const char *format, /**< format string */ + ...) /**< parameters */ { + (void) level; /* ignore log level */ + va_list args; - int count; va_start (args, format); - count = vfprintf (stderr, format, args); + vfprintf (stderr, format, args); va_end (args); - return count; -} /* jerry_port_errormsg */ +} /* jerry_port_log */