Skip to content

Add logging support for linux. #79

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
May 20, 2015
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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?=
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions jerry-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}})

Expand Down
17 changes: 15 additions & 2 deletions jerry-core/jerry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -1119,21 +1124,29 @@ 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
flags &= ~(JERRY_FLAG_MEM_STATS
| 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;
Expand Down
6 changes: 6 additions & 0 deletions jerry-core/jerry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
31 changes: 31 additions & 0 deletions jerry-core/jrt/jrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef JERRY_GLOBALS_H
#define JERRY_GLOBALS_H

#include <stdio.h>
#include "jerry.h"
#include "jrt-types.h"

Expand Down Expand Up @@ -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
*/
Expand Down
62 changes: 60 additions & 2 deletions main-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string.h>

#include "jerry.h"
#include "jrt/jrt.h"

#include "plugins/io/init.h"

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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]))
Expand Down Expand Up @@ -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];
Expand All @@ -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 ();
Expand All @@ -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;
Expand Down