From d6c9c5911ea998762a90c8245584e699cd06f6e2 Mon Sep 17 00:00:00 2001 From: Andrey Shitov Date: Mon, 18 May 2015 22:00:30 +0300 Subject: [PATCH] Add logging support for linux. JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov a.shitov@samsung.com --- CMakeLists.txt | 5 ++++ Makefile | 8 ++++- jerry-core/CMakeLists.txt | 5 ++++ jerry-core/jerry.cpp | 17 +++++++++-- jerry-core/jerry.h | 6 ++++ jerry-core/jrt/jrt.h | 31 ++++++++++++++++++++ main-linux.cpp | 62 +++++++++++++++++++++++++++++++++++++-- 7 files changed, 129 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 675b154281..e426e51454 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ project (Jerry CXX C ASM) # Determining platform and defining options option(ENABLE_VALGRIND "Enable valgrind helpers in memory allocators" OFF) option(ENABLE_LTO "Enable LTO build" ON) + option(ENABLE_LOG "Enable LOG build" OFF) set(PLATFORM "${CMAKE_SYSTEM_NAME}") string(TOUPPER "${PLATFORM}" PLATFORM) @@ -337,6 +338,10 @@ project (Jerry CXX C ASM) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) add_custom_target(mcu_header_with_script_to_run.${TARGET_NAME} DEPENDS ${MCU_SCRIPT_GENERATED_HEADER}) set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_MCU_SCRIPT_HEADER="${MCU_SCRIPT_GENERATED_HEADER}") + elseif("${PLATFORM}" STREQUAL "LINUX") + if("${ENABLE_LOG}" STREQUAL "ON") + set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG) + endif() endif() set_property(TARGET ${TARGET_NAME} diff --git a/Makefile b/Makefile index c5824c9fba..e303e0c224 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,12 @@ LTO := OFF endif + # LOG + LOG ?= OFF + ifneq ($(LOG),ON) + LOG := OFF + endif + # External build configuration # List of include paths for external libraries (semicolon-separated) EXTERNAL_LIBS_INTERFACE ?= @@ -151,7 +157,7 @@ $(BUILD_DIRS_NATIVE): prerequisites fi; \ mkdir -p $@ && \ cd $@ && \ - cmake -DENABLE_VALGRIND=$(VALGRIND) -DENABLE_LTO=$(LTO) -DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_linux_$$arch.cmake ../../.. &>cmake.log || \ + cmake -DENABLE_VALGRIND=$(VALGRIND) -DENABLE_LOG=$(LOG) -DENABLE_LTO=$(LTO) -DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_linux_$$arch.cmake ../../.. &>cmake.log || \ (echo "CMake run failed. See "`pwd`"/cmake.log for details."; exit 1;) $(BUILD_DIRS_NUTTX): prerequisites diff --git a/jerry-core/CMakeLists.txt b/jerry-core/CMakeLists.txt index 902bdf8dce..6828d9ebf7 100644 --- a/jerry-core/CMakeLists.txt +++ b/jerry-core/CMakeLists.txt @@ -140,6 +140,11 @@ project (JerryCore CXX C ASM) set(INCLUDE_CORE ${INCLUDE_CORE} ${INCLUDE_THIRD_PARTY_VALGRIND}) endif() + # Log + if("${ENABLE_LOG}" STREQUAL "ON") + set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG) + endif() + # Platform-specific configuration set(DEFINES_JERRY ${DEFINES_JERRY} ${DEFINES_JERRY_${PLATFORM_EXT}}) diff --git a/jerry-core/jerry.cpp b/jerry-core/jerry.cpp index c8af1c283e..a7e679736d 100644 --- a/jerry-core/jerry.cpp +++ b/jerry-core/jerry.cpp @@ -71,6 +71,11 @@ static bool jerry_api_available; */ char jerry_extension_characters_buffer[CONFIG_EXTENSION_CHAR_BUFFER_SIZE]; +#ifdef JERRY_ENABLE_LOG +int jerry_debug_level = 0; +FILE *jerry_log_file = nullptr; +#endif + /** * Assert that it is correct to call API in current state. * @@ -1119,6 +1124,13 @@ jerry_api_eval (const char *source_p, /**< source code */ void jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */ { + if (flags & (JERRY_FLAG_ENABLE_LOG)) + { +#ifndef JERRY_ENABLE_LOG + JERRY_WARNING_MSG ("Ignoring log options because of '!JERRY_ENABLE_LOG' build configuration.\n"); +#endif + } + if (flags & (JERRY_FLAG_MEM_STATS)) { #ifndef MEM_STATS @@ -1126,14 +1138,15 @@ jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */ | JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE); - printf ("Ignoring memory statistics option because of '!MEM_STATS' build configuration.\n"); + JERRY_WARNING_MSG ("Ignoring memory statistics option because of '!MEM_STATS' build configuration.\n"); #endif /* !MEM_STATS */ } else if (flags & (JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE)) { flags &= ~(JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE); - printf ("Ignoring detailed memory statistics options because memory statistics dump mode is not enabled.\n"); + JERRY_WARNING_MSG ( + "Ignoring detailed memory statistics options because memory statistics dump mode is not enabled.\n"); } jerry_flags = flags; diff --git a/jerry-core/jerry.h b/jerry-core/jerry.h index 45c60e5e52..dbc9815aec 100644 --- a/jerry-core/jerry.h +++ b/jerry-core/jerry.h @@ -39,6 +39,7 @@ typedef uint32_t jerry_flag_t; #define JERRY_FLAG_MEM_STATS_SEPARATE (1u << 3) /**< dump memory statistics and reset peak values after parse */ #define JERRY_FLAG_PARSE_ONLY (1u << 4) /**< parse only, prevents script execution (only for testing) * FIXME: Remove. */ +#define JERRY_FLAG_ENABLE_LOG (1u << 5) /**< enable logging */ /** * Error codes @@ -67,6 +68,11 @@ extern const char *jerry_commit_hash; */ extern const char *jerry_branch_name; +#ifdef JERRY_ENABLE_LOG +extern int jerry_debug_level; +extern FILE *jerry_log_file; +#endif /* JERRY_ENABLE_LOG */ + /** * Jerry error callback type */ diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index bfd6fad8f4..170b5ba186 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -16,6 +16,7 @@ #ifndef JERRY_GLOBALS_H #define JERRY_GLOBALS_H +#include #include "jerry.h" #include "jrt-types.h" @@ -92,6 +93,36 @@ extern void __noreturn jerry_unimplemented (const char *comment, const char *fil #define JERRY_ASSERT(x) do { if (false) { (void)(x); } } while (0) #endif /* !JERRY_NDEBUG */ +#ifdef JERRY_ENABLE_LOG +#define JERRY_LOG(lvl, ...) \ + do \ + { \ + if (lvl <= jerry_debug_level && jerry_log_file) \ + { \ + fprintf (jerry_log_file, __VA_ARGS__); \ + } \ + } \ + while (0) + +#define JERRY_DLOG(...) JERRY_LOG (1, __VA_ARGS__) +#define JERRY_DDLOG(...) JERRY_LOG (2, __VA_ARGS__) +#define JERRY_DDDLOG(...) JERRY_LOG (3, __VA_ARGS__) +#else /* !JERRY_ENABLE_LOG */ +#define JERRY_DLOG(...) \ + do \ + { \ + if (false) \ + { \ + jerry_ref_unused_variables (0, __VA_ARGS__); \ + } \ + } while (0) +#define JERRY_DDLOG(...) JERRY_DLOG (__VA_ARGS__) +#define JERRY_DDDLOG(...) JERRY_DLOG (__VA_ARGS__) +#endif /* !JERRY_ENABLE_LOG */ + +#define JERRY_ERROR_MSG(...) fprintf (stderr, __VA_ARGS__) +#define JERRY_WARNING_MSG(...) JERRY_ERROR_MSG (__VA_ARGS__) + /** * Mark for unreachable points and unimplemented cases */ diff --git a/main-linux.cpp b/main-linux.cpp index 26430470f5..56648ad8b7 100644 --- a/main-linux.cpp +++ b/main-linux.cpp @@ -17,6 +17,7 @@ #include #include "jerry.h" +#include "jrt/jrt.h" #include "plugins/io/init.h" @@ -93,7 +94,7 @@ read_sources (const char *script_file_names[], if (i < files_count) { - printf ("Failed to read script N%d\n", i + 1); + JERRY_ERROR_MSG ("Failed to read script N%d\n", i + 1); return NULL; } @@ -113,7 +114,7 @@ main (int argc, { if (argc >= JERRY_MAX_COMMAND_LINE_ARGS) { - printf ("Too many command line arguments. Current maximum is %d (JERRY_MAX_COMMAND_LINE_ARGS)\n", argc); + JERRY_ERROR_MSG ("Too many command line arguments. Current maximum is %d (JERRY_MAX_COMMAND_LINE_ARGS)\n", argc); return JERRY_STANDALONE_EXIT_CODE_FAIL; } @@ -130,6 +131,9 @@ main (int argc, jerry_flag_t flags = JERRY_FLAG_EMPTY; +#ifdef JERRY_ENABLE_LOG + const char *log_file_name = nullptr; +#endif /* JERRY_ENABLE_LOG */ for (i = 1; i < argc; i++) { if (!strcmp ("-v", argv[i])) @@ -159,6 +163,36 @@ main (int argc, { flags |= JERRY_FLAG_SHOW_OPCODES; } + else if (!strcmp ("--log-level", argv[i])) + { + flags |= JERRY_FLAG_ENABLE_LOG; + if (++i < argc && strlen (argv[i]) == 1 && argv[i][0] >='0' && argv[i][0] <= '3') + { +#ifdef JERRY_ENABLE_LOG + jerry_debug_level = argv[i][0] - '0'; +#endif /* JERRY_ENABLE_LOG */ + } + else + { + JERRY_ERROR_MSG ("Error: wrong format or invalid argument\n"); + return JERRY_STANDALONE_EXIT_CODE_FAIL; + } + } + else if (!strcmp ("--log-file", argv[i])) + { + flags |= JERRY_FLAG_ENABLE_LOG; + if (++i < argc) + { +#ifdef JERRY_ENABLE_LOG + log_file_name = argv[i]; +#endif /* JERRY_ENABLE_LOG */ + } + else + { + JERRY_ERROR_MSG ("Error: wrong format of the arguments\n"); + return JERRY_STANDALONE_EXIT_CODE_FAIL; + } + } else { file_names[files_counter++] = argv[i]; @@ -180,6 +214,22 @@ main (int argc, } else { +#ifdef JERRY_ENABLE_LOG + if (log_file_name) + { + jerry_log_file = fopen (log_file_name, "w"); + if (jerry_log_file == nullptr) + { + JERRY_ERROR_MSG ("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); plugin_io_init (); @@ -201,6 +251,14 @@ main (int argc, jerry_cleanup (); +#ifdef JERRY_ENABLE_LOG + if (jerry_log_file && jerry_log_file != stdout) + { + fclose (jerry_log_file); + jerry_log_file = nullptr; + } +#endif /* JERRY_ENABLE_LOG */ + if (ret_code == JERRY_COMPLETION_CODE_OK) { return JERRY_STANDALONE_EXIT_CODE_OK;