Skip to content

Commit d6c9c59

Browse files
committed
Add logging support for linux.
JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov [email protected]
1 parent 0c05874 commit d6c9c59

File tree

7 files changed

+129
-5
lines changed

7 files changed

+129
-5
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ project (Jerry CXX C ASM)
6161
# Determining platform and defining options
6262
option(ENABLE_VALGRIND "Enable valgrind helpers in memory allocators" OFF)
6363
option(ENABLE_LTO "Enable LTO build" ON)
64+
option(ENABLE_LOG "Enable LOG build" OFF)
6465

6566
set(PLATFORM "${CMAKE_SYSTEM_NAME}")
6667
string(TOUPPER "${PLATFORM}" PLATFORM)
@@ -337,6 +338,10 @@ project (Jerry CXX C ASM)
337338
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
338339
add_custom_target(mcu_header_with_script_to_run.${TARGET_NAME} DEPENDS ${MCU_SCRIPT_GENERATED_HEADER})
339340
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_MCU_SCRIPT_HEADER="${MCU_SCRIPT_GENERATED_HEADER}")
341+
elseif("${PLATFORM}" STREQUAL "LINUX")
342+
if("${ENABLE_LOG}" STREQUAL "ON")
343+
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG)
344+
endif()
340345
endif()
341346

342347
set_property(TARGET ${TARGET_NAME}

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
LTO := OFF
6363
endif
6464

65+
# LOG
66+
LOG ?= OFF
67+
ifneq ($(LOG),ON)
68+
LOG := OFF
69+
endif
70+
6571
# External build configuration
6672
# List of include paths for external libraries (semicolon-separated)
6773
EXTERNAL_LIBS_INTERFACE ?=
@@ -151,7 +157,7 @@ $(BUILD_DIRS_NATIVE): prerequisites
151157
fi; \
152158
mkdir -p $@ && \
153159
cd $@ && \
154-
cmake -DENABLE_VALGRIND=$(VALGRIND) -DENABLE_LTO=$(LTO) -DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_linux_$$arch.cmake ../../.. &>cmake.log || \
160+
cmake -DENABLE_VALGRIND=$(VALGRIND) -DENABLE_LOG=$(LOG) -DENABLE_LTO=$(LTO) -DCMAKE_TOOLCHAIN_FILE=build/configs/toolchain_linux_$$arch.cmake ../../.. &>cmake.log || \
155161
(echo "CMake run failed. See "`pwd`"/cmake.log for details."; exit 1;)
156162

157163
$(BUILD_DIRS_NUTTX): prerequisites

jerry-core/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ project (JerryCore CXX C ASM)
140140
set(INCLUDE_CORE ${INCLUDE_CORE} ${INCLUDE_THIRD_PARTY_VALGRIND})
141141
endif()
142142

143+
# Log
144+
if("${ENABLE_LOG}" STREQUAL "ON")
145+
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG)
146+
endif()
147+
143148
# Platform-specific configuration
144149
set(DEFINES_JERRY ${DEFINES_JERRY} ${DEFINES_JERRY_${PLATFORM_EXT}})
145150

jerry-core/jerry.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ static bool jerry_api_available;
7171
*/
7272
char jerry_extension_characters_buffer[CONFIG_EXTENSION_CHAR_BUFFER_SIZE];
7373

74+
#ifdef JERRY_ENABLE_LOG
75+
int jerry_debug_level = 0;
76+
FILE *jerry_log_file = nullptr;
77+
#endif
78+
7479
/**
7580
* Assert that it is correct to call API in current state.
7681
*
@@ -1119,21 +1124,29 @@ jerry_api_eval (const char *source_p, /**< source code */
11191124
void
11201125
jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */
11211126
{
1127+
if (flags & (JERRY_FLAG_ENABLE_LOG))
1128+
{
1129+
#ifndef JERRY_ENABLE_LOG
1130+
JERRY_WARNING_MSG ("Ignoring log options because of '!JERRY_ENABLE_LOG' build configuration.\n");
1131+
#endif
1132+
}
1133+
11221134
if (flags & (JERRY_FLAG_MEM_STATS))
11231135
{
11241136
#ifndef MEM_STATS
11251137
flags &= ~(JERRY_FLAG_MEM_STATS
11261138
| JERRY_FLAG_MEM_STATS_PER_OPCODE
11271139
| JERRY_FLAG_MEM_STATS_SEPARATE);
11281140

1129-
printf ("Ignoring memory statistics option because of '!MEM_STATS' build configuration.\n");
1141+
JERRY_WARNING_MSG ("Ignoring memory statistics option because of '!MEM_STATS' build configuration.\n");
11301142
#endif /* !MEM_STATS */
11311143
}
11321144
else if (flags & (JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE))
11331145
{
11341146
flags &= ~(JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE);
11351147

1136-
printf ("Ignoring detailed memory statistics options because memory statistics dump mode is not enabled.\n");
1148+
JERRY_WARNING_MSG (
1149+
"Ignoring detailed memory statistics options because memory statistics dump mode is not enabled.\n");
11371150
}
11381151

11391152
jerry_flags = flags;

jerry-core/jerry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef uint32_t jerry_flag_t;
3939
#define JERRY_FLAG_MEM_STATS_SEPARATE (1u << 3) /**< dump memory statistics and reset peak values after parse */
4040
#define JERRY_FLAG_PARSE_ONLY (1u << 4) /**< parse only, prevents script execution (only for testing)
4141
* FIXME: Remove. */
42+
#define JERRY_FLAG_ENABLE_LOG (1u << 5) /**< enable logging */
4243

4344
/**
4445
* Error codes
@@ -67,6 +68,11 @@ extern const char *jerry_commit_hash;
6768
*/
6869
extern const char *jerry_branch_name;
6970

71+
#ifdef JERRY_ENABLE_LOG
72+
extern int jerry_debug_level;
73+
extern FILE *jerry_log_file;
74+
#endif /* JERRY_ENABLE_LOG */
75+
7076
/**
7177
* Jerry error callback type
7278
*/

jerry-core/jrt/jrt.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef JERRY_GLOBALS_H
1717
#define JERRY_GLOBALS_H
1818

19+
#include <stdio.h>
1920
#include "jerry.h"
2021
#include "jrt-types.h"
2122

@@ -92,6 +93,36 @@ extern void __noreturn jerry_unimplemented (const char *comment, const char *fil
9293
#define JERRY_ASSERT(x) do { if (false) { (void)(x); } } while (0)
9394
#endif /* !JERRY_NDEBUG */
9495

96+
#ifdef JERRY_ENABLE_LOG
97+
#define JERRY_LOG(lvl, ...) \
98+
do \
99+
{ \
100+
if (lvl <= jerry_debug_level && jerry_log_file) \
101+
{ \
102+
fprintf (jerry_log_file, __VA_ARGS__); \
103+
} \
104+
} \
105+
while (0)
106+
107+
#define JERRY_DLOG(...) JERRY_LOG (1, __VA_ARGS__)
108+
#define JERRY_DDLOG(...) JERRY_LOG (2, __VA_ARGS__)
109+
#define JERRY_DDDLOG(...) JERRY_LOG (3, __VA_ARGS__)
110+
#else /* !JERRY_ENABLE_LOG */
111+
#define JERRY_DLOG(...) \
112+
do \
113+
{ \
114+
if (false) \
115+
{ \
116+
jerry_ref_unused_variables (0, __VA_ARGS__); \
117+
} \
118+
} while (0)
119+
#define JERRY_DDLOG(...) JERRY_DLOG (__VA_ARGS__)
120+
#define JERRY_DDDLOG(...) JERRY_DLOG (__VA_ARGS__)
121+
#endif /* !JERRY_ENABLE_LOG */
122+
123+
#define JERRY_ERROR_MSG(...) fprintf (stderr, __VA_ARGS__)
124+
#define JERRY_WARNING_MSG(...) JERRY_ERROR_MSG (__VA_ARGS__)
125+
95126
/**
96127
* Mark for unreachable points and unimplemented cases
97128
*/

main-linux.cpp

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <string.h>
1818

1919
#include "jerry.h"
20+
#include "jrt/jrt.h"
2021

2122
#include "plugins/io/init.h"
2223

@@ -93,7 +94,7 @@ read_sources (const char *script_file_names[],
9394

9495
if (i < files_count)
9596
{
96-
printf ("Failed to read script N%d\n", i + 1);
97+
JERRY_ERROR_MSG ("Failed to read script N%d\n", i + 1);
9798

9899
return NULL;
99100
}
@@ -113,7 +114,7 @@ main (int argc,
113114
{
114115
if (argc >= JERRY_MAX_COMMAND_LINE_ARGS)
115116
{
116-
printf ("Too many command line arguments. Current maximum is %d (JERRY_MAX_COMMAND_LINE_ARGS)\n", argc);
117+
JERRY_ERROR_MSG ("Too many command line arguments. Current maximum is %d (JERRY_MAX_COMMAND_LINE_ARGS)\n", argc);
117118

118119
return JERRY_STANDALONE_EXIT_CODE_FAIL;
119120
}
@@ -130,6 +131,9 @@ main (int argc,
130131

131132
jerry_flag_t flags = JERRY_FLAG_EMPTY;
132133

134+
#ifdef JERRY_ENABLE_LOG
135+
const char *log_file_name = nullptr;
136+
#endif /* JERRY_ENABLE_LOG */
133137
for (i = 1; i < argc; i++)
134138
{
135139
if (!strcmp ("-v", argv[i]))
@@ -159,6 +163,36 @@ main (int argc,
159163
{
160164
flags |= JERRY_FLAG_SHOW_OPCODES;
161165
}
166+
else if (!strcmp ("--log-level", argv[i]))
167+
{
168+
flags |= JERRY_FLAG_ENABLE_LOG;
169+
if (++i < argc && strlen (argv[i]) == 1 && argv[i][0] >='0' && argv[i][0] <= '3')
170+
{
171+
#ifdef JERRY_ENABLE_LOG
172+
jerry_debug_level = argv[i][0] - '0';
173+
#endif /* JERRY_ENABLE_LOG */
174+
}
175+
else
176+
{
177+
JERRY_ERROR_MSG ("Error: wrong format or invalid argument\n");
178+
return JERRY_STANDALONE_EXIT_CODE_FAIL;
179+
}
180+
}
181+
else if (!strcmp ("--log-file", argv[i]))
182+
{
183+
flags |= JERRY_FLAG_ENABLE_LOG;
184+
if (++i < argc)
185+
{
186+
#ifdef JERRY_ENABLE_LOG
187+
log_file_name = argv[i];
188+
#endif /* JERRY_ENABLE_LOG */
189+
}
190+
else
191+
{
192+
JERRY_ERROR_MSG ("Error: wrong format of the arguments\n");
193+
return JERRY_STANDALONE_EXIT_CODE_FAIL;
194+
}
195+
}
162196
else
163197
{
164198
file_names[files_counter++] = argv[i];
@@ -180,6 +214,22 @@ main (int argc,
180214
}
181215
else
182216
{
217+
#ifdef JERRY_ENABLE_LOG
218+
if (log_file_name)
219+
{
220+
jerry_log_file = fopen (log_file_name, "w");
221+
if (jerry_log_file == nullptr)
222+
{
223+
JERRY_ERROR_MSG ("Failed to open log file: %s\n", log_file_name);
224+
return JERRY_STANDALONE_EXIT_CODE_FAIL;
225+
}
226+
}
227+
else
228+
{
229+
jerry_log_file = stdout;
230+
}
231+
#endif /* JERRY_ENABLE_LOG */
232+
183233
jerry_init (flags);
184234

185235
plugin_io_init ();
@@ -201,6 +251,14 @@ main (int argc,
201251

202252
jerry_cleanup ();
203253

254+
#ifdef JERRY_ENABLE_LOG
255+
if (jerry_log_file && jerry_log_file != stdout)
256+
{
257+
fclose (jerry_log_file);
258+
jerry_log_file = nullptr;
259+
}
260+
#endif /* JERRY_ENABLE_LOG */
261+
204262
if (ret_code == JERRY_COMPLETION_CODE_OK)
205263
{
206264
return JERRY_STANDALONE_EXIT_CODE_OK;

0 commit comments

Comments
 (0)