Skip to content

Commit 512c002

Browse files
committed
Add logging support for linux.
JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov [email protected]
1 parent 6b09e53 commit 512c002

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
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)
@@ -333,6 +334,10 @@ project (Jerry CXX C ASM)
333334
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
334335
add_custom_target(mcu_header_with_script_to_run.${TARGET_NAME} DEPENDS ${MCU_SCRIPT_GENERATED_HEADER})
335336
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_MCU_SCRIPT_HEADER="${MCU_SCRIPT_GENERATED_HEADER}")
337+
elseif("${PLATFORM}" STREQUAL "LINUX")
338+
if("${ENABLE_LOG}" STREQUAL "ON")
339+
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG)
340+
endif()
336341
endif()
337342

338343
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ static bool jerry_api_available;
7070
*/
7171
char jerry_extension_characters_buffer[CONFIG_EXTENSION_CHAR_BUFFER_SIZE];
7272

73+
#ifdef JERRY_ENABLE_LOG
74+
int jerry_debug_level = 0;
75+
FILE *jerry_log_file = nullptr;
76+
#endif
77+
7378
/**
7479
* Assert that it is correct to call API in current state.
7580
*
@@ -1044,6 +1049,13 @@ jerry_api_eval (const char *source_p, /**< source code */
10441049
void
10451050
jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */
10461051
{
1052+
if (flags & (JERRY_FLAG_ENABLE_LOG))
1053+
{
1054+
#ifndef JERRY_ENABLE_LOG
1055+
printf ("Ignoring log options because of '!JERRY_ENABLE_LOG' build configuration.\n");
1056+
#endif
1057+
}
1058+
10471059
if (flags & (JERRY_FLAG_MEM_STATS))
10481060
{
10491061
#ifndef MEM_STATS

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: 28 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,33 @@ 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+
95123
/**
96124
* Mark for unreachable points and unimplemented cases
97125
*/

main-linux.cpp

Lines changed: 58 additions & 0 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

@@ -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 ("--debug-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+
printf ("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+
printf ("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+
printf ("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)