From 29f6ffc35beba4d5465f4c990d1088648c5f7f72 Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Wed, 8 Aug 2018 16:35:03 +0200 Subject: [PATCH] Make logging an optional feature and disable it by default (#2449) JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu --- docs/02.API-REFERENCE.md | 1 + docs/05.PORT-API.md | 5 +++++ jerry-core/CMakeLists.txt | 13 +++++++++++++ jerry-core/api/jerry.c | 3 +++ jerry-core/include/jerryscript-core.h | 1 + jerry-core/include/jerryscript-port.h | 5 +++++ jerry-core/jrt/jrt.h | 20 ++++++++++++++++++++ jerry-core/parser/js/js-parser.c | 12 ++++++------ tools/build.py | 3 +++ tools/run-tests.py | 2 ++ 10 files changed, 59 insertions(+), 6 deletions(-) diff --git a/docs/02.API-REFERENCE.md b/docs/02.API-REFERENCE.md index c23062168..8a215af60 100644 --- a/docs/02.API-REFERENCE.md +++ b/docs/02.API-REFERENCE.md @@ -60,6 +60,7 @@ Possible compile time enabled feature types: - JERRY_FEATURE_DATE - Date support - JERRY_FEATURE_REGEXP - RegExp support - JERRY_FEATURE_LINE_INFO - line info available + - JERRY_FEATURE_LOGGING - logging ## jerry_parse_opts_t diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index ce07cd1d6..50ab930e4 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -63,6 +63,11 @@ typedef enum * * Example: a libc-based port may implement this with vfprintf(stderr) or * vfprintf(logfile), or both, depending on log level. + * + * Note: + * This port function is called by jerry-core when JERRY_ENABLE_LOGGING is + * defined. It is also common practice though to use this function in + * application code. */ void jerry_port_log (jerry_log_level_t level, const char *fmt, ...); ``` diff --git a/jerry-core/CMakeLists.txt b/jerry-core/CMakeLists.txt index 3f914e8ae..76f4bcea3 100644 --- a/jerry-core/CMakeLists.txt +++ b/jerry-core/CMakeLists.txt @@ -26,6 +26,7 @@ set(FEATURE_ERROR_MESSAGES OFF CACHE BOOL "Enable error messages?") set(FEATURE_EXTERNAL_CONTEXT OFF CACHE BOOL "Enable external context?") set(FEATURE_JS_PARSER ON CACHE BOOL "Enable js-parser?") set(FEATURE_LINE_INFO OFF CACHE BOOL "Enable line info?") +set(FEATURE_LOGGING OFF CACHE BOOL "Enable logging?") set(FEATURE_MEM_STATS OFF CACHE BOOL "Enable memory statistics?") set(FEATURE_MEM_STRESS_TEST OFF CACHE BOOL "Enable mem-stress test?") set(FEATURE_PARSER_DUMP OFF CACHE BOOL "Enable parser byte-code dumps?") @@ -66,6 +67,12 @@ if(JERRY_CMDLINE_SNAPSHOT) set(FEATURE_SNAPSHOT_SAVE_MESSAGE " (FORCED BY SNAPSHOT TOOL)") endif() +if(FEATURE_MEM_STATS OR FEATURE_PARSER_DUMP OR FEATURE_REGEXP_DUMP) + set(FEATURE_LOGGING ON) + + set(FEATURE_LOGGING_MESSAGE " (FORCED BY STATS OR DUMP)") +endif() + # Status messages message(STATUS "ENABLE_ALL_IN_ONE " ${ENABLE_ALL_IN_ONE} ${ENABLE_ALL_IN_ONE_MESSAGE}) message(STATUS "FEATURE_CPOINTER_32_BIT " ${FEATURE_CPOINTER_32_BIT} ${FEATURE_CPOINTER_32_BIT_MESSAGE}) @@ -74,6 +81,7 @@ message(STATUS "FEATURE_ERROR_MESSAGES " ${FEATURE_ERROR_MESSAGES}) message(STATUS "FEATURE_EXTERNAL_CONTEXT " ${FEATURE_EXTERNAL_CONTEXT}) message(STATUS "FEATURE_JS_PARSER " ${FEATURE_JS_PARSER}) message(STATUS "FEATURE_LINE_INFO " ${FEATURE_LINE_INFO}) +message(STATUS "FEATURE_LOGGING " ${FEATURE_LOGGING} ${FEATURE_LOGGING_MESSAGE}) message(STATUS "FEATURE_MEM_STATS " ${FEATURE_MEM_STATS}) message(STATUS "FEATURE_MEM_STRESS_TEST " ${FEATURE_MEM_STRESS_TEST}) message(STATUS "FEATURE_PARSER_DUMP " ${FEATURE_PARSER_DUMP} ${FEATURE_PARSER_DUMP_MESSAGE}) @@ -187,6 +195,11 @@ if(FEATURE_LINE_INFO) set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LINE_INFO) endif() +# Logging +if(FEATURE_LOGGING) + set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOGGING) +endif() + # Memory statistics if(FEATURE_MEM_STATS) set(DEFINES_JERRY ${DEFINES_JERRY} JMEM_STATS) diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 91a6ce529..6188fb3ac 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -883,6 +883,9 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check * #ifdef JERRY_ENABLE_LINE_INFO || feature == JERRY_FEATURE_LINE_INFO #endif /* JERRY_ENABLE_LINE_INFO */ +#ifdef JERRY_ENABLE_LOGGING + || feature == JERRY_FEATURE_LOGGING +#endif /* JERRY_ENABLE_LOGGING */ ); } /* jerry_is_feature_enabled */ diff --git a/jerry-core/include/jerryscript-core.h b/jerry-core/include/jerryscript-core.h index cf844ed3c..6e455ac6b 100644 --- a/jerry-core/include/jerryscript-core.h +++ b/jerry-core/include/jerryscript-core.h @@ -91,6 +91,7 @@ typedef enum JERRY_FEATURE_DATE, /**< Date support */ JERRY_FEATURE_REGEXP, /**< Regexp support */ JERRY_FEATURE_LINE_INFO, /**< line info available */ + JERRY_FEATURE_LOGGING, /**< logging */ JERRY_FEATURE__COUNT /**< number of features. NOTE: must be at the end of the list */ } jerry_feature_t; diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index e72ee425d..777b5e747 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -97,6 +97,11 @@ typedef enum * * Example: a libc-based port may implement this with vfprintf(stderr) or * vfprintf(logfile), or both, depending on log level. + * + * Note: + * This port function is called by jerry-core when JERRY_ENABLE_LOGGING is + * defined. It is also common practice though to use this function in + * application code. */ void JERRY_ATTR_FORMAT (printf, 2, 3) jerry_port_log (jerry_log_level_t level, const char *format, ...); diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index cc65aac22..9e554549d 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -49,6 +49,19 @@ */ #define JERRY_UNUSED(x) ((void) (x)) +#define JERRY_UNUSED_1(_1) JERRY_UNUSED (_1) +#define JERRY_UNUSED_2(_1, _2) JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2) +#define JERRY_UNUSED_3(_1, _2, _3) JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3) +#define JERRY_UNUSED_4(_1, _2, _3, _4) JERRY_UNUSED (_1), JERRY_UNUSED_3 (_2, _3, _4) +#define JERRY_UNUSED_5(_1, _2, _3, _4, _5) JERRY_UNUSED (_1), JERRY_UNUSED_4 (_2, _3, _4, _5) + +#define JERRY_VA_ARGS_NUM_IMPL(_1, _2, _3, _4, _5, N, ...) N +#define JERRY_VA_ARGS_NUM(...) JERRY_VA_ARGS_NUM_IMPL (__VA_ARGS__, 5, 4, 3, 2, 1, 0) + +#define JERRY_UNUSED_ALL_IMPL_(nargs) JERRY_UNUSED_ ## nargs +#define JERRY_UNUSED_ALL_IMPL(nargs) JERRY_UNUSED_ALL_IMPL_ (nargs) +#define JERRY_UNUSED_ALL(...) JERRY_UNUSED_ALL_IMPL (JERRY_VA_ARGS_NUM (__VA_ARGS__)) (__VA_ARGS__) + /* * Asserts * @@ -114,10 +127,17 @@ void JERRY_ATTR_NORETURN jerry_fatal (jerry_fatal_code_t code); /* * Logging */ +#ifdef JERRY_ENABLE_LOGGING #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__) #define JERRY_DEBUG_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__) #define JERRY_TRACE_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_TRACE, __VA_ARGS__) +#else /* !JERRY_ENABLE_LOGGING */ +#define JERRY_ERROR_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0) +#define JERRY_WARNING_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0) +#define JERRY_DEBUG_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0) +#define JERRY_TRACE_MSG(...) do { if (false) { JERRY_UNUSED_ALL (__VA_ARGS__); } } while (0) +#endif /* JERRY_ENABLE_LOGGING */ /** * Size of struct member diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index f5d572bc6..e25ccf709 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -2575,9 +2575,9 @@ parser_parse_function (parser_context_t *context_p, /**< context */ if (context_p->is_show_opcodes) { #ifndef CONFIG_DISABLE_ES2015_CLASS - bool is_constructor = context_p->status_flags & PARSER_CLASS_CONSTRUCTOR; - JERRY_DEBUG_MSG (is_constructor ? "\n--- Class constructor parsing start ---\n\n" - : "\n--- Function parsing start ---\n\n"); + JERRY_DEBUG_MSG ("\n--- %s parsing start ---\n\n", + (context_p->status_flags & PARSER_CLASS_CONSTRUCTOR) ? "Class constructor" + : "Function"); #else /* CONFIG_DISABLE_ES2015_CLASS */ JERRY_DEBUG_MSG ("\n--- Function parsing start ---\n\n"); #endif /* !CONFIG_DISABLE_ES2015_CLASS */ @@ -2679,9 +2679,9 @@ parser_parse_function (parser_context_t *context_p, /**< context */ if (context_p->is_show_opcodes) { #ifndef CONFIG_DISABLE_ES2015_CLASS - bool is_constructor = context_p->status_flags & PARSER_CLASS_CONSTRUCTOR; - JERRY_DEBUG_MSG (is_constructor ? "\n--- Class constructor parsing end ---\n\n" - : "\n--- Function parsing end ---\n\n"); + JERRY_DEBUG_MSG ("\n--- %s parsing end ---\n\n", + (context_p->status_flags & PARSER_CLASS_CONSTRUCTOR) ? "Class constructor" + : "Function"); #else /* CONFIG_DISABLE_ES2015_CLASS */ JERRY_DEBUG_MSG ("\n--- Function parsing end ---\n\n"); #endif /* !CONFIG_DISABLE_ES2015_CLASS */ diff --git a/tools/build.py b/tools/build.py index dc31eea72..666ff84a6 100755 --- a/tools/build.py +++ b/tools/build.py @@ -116,6 +116,8 @@ def get_arguments(): help='enable js-parser (%(choices)s)') coregrp.add_argument('--line-info', metavar='X', choices=['ON', 'OFF'], type=str.upper, help='provide line info (%(choices)s)') + coregrp.add_argument('--logging', metavar='X', choices=['ON', 'OFF'], type=str.upper, + help='enable logging (%(choices)s)') coregrp.add_argument('--mem-heap', metavar='SIZE', type=int, help='size of memory heap (in kilobytes)') coregrp.add_argument('--mem-stats', metavar='X', choices=['ON', 'OFF'], type=str.upper, @@ -189,6 +191,7 @@ def generate_build_options(arguments): build_options_append('FEATURE_DEBUGGER', arguments.jerry_debugger) build_options_append('FEATURE_JS_PARSER', arguments.js_parser) build_options_append('FEATURE_LINE_INFO', arguments.line_info) + build_options_append('FEATURE_LOGGING', arguments.logging) build_options_append('MEM_HEAP_SIZE_KB', arguments.mem_heap) build_options_append('FEATURE_MEM_STATS', arguments.mem_stats) build_options_append('FEATURE_MEM_STRESS_TEST', arguments.mem_stress_test) diff --git a/tools/run-tests.py b/tools/run-tests.py index c9feb7709..5f937a5c8 100755 --- a/tools/run-tests.py +++ b/tools/run-tests.py @@ -120,6 +120,8 @@ JERRY_BUILDOPTIONS = [ ['--lto=on']), Options('buildoption_test-error_messages', ['--error-messages=on']), + Options('buildoption_test-logging', + ['--logging=on']), Options('buildoption_test-all_in_one', ['--all-in-one=on']), Options('buildoption_test-valgrind',