Skip to content

Commit adf2301

Browse files
committed
Implement IO port API
Related issue: #964 Implemented the IO API of Jerry ports. Removed log file from API level. The port implementation should define the destination of log messages. JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 6f1ce8d commit adf2301

File tree

10 files changed

+164
-158
lines changed

10 files changed

+164
-158
lines changed

jerry-core/jerry-api.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_na
162162
*/
163163
#ifdef JERRY_ENABLE_LOG
164164
extern int jerry_debug_level;
165-
extern FILE *jerry_log_file;
166165
#endif /* JERRY_ENABLE_LOG */
167166

168167
/**

jerry-core/jerry-port.h

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ extern "C"
2929
* @{
3030
*/
3131

32-
/**
33-
* Target port functions for console output
34-
*/
35-
int jerry_port_logmsg (FILE *stream, const char *format, ...);
36-
int jerry_port_errormsg (const char *format, ...);
37-
3832
/*
3933
* Termination Port API
4034
*
@@ -71,6 +65,54 @@ typedef enum
7165
*/
7266
void jerry_port_fatal (jerry_fatal_code_t code);
7367

68+
/*
69+
* I/O Port API
70+
*/
71+
72+
/**
73+
* Print a string to the console. The function should implement a printf-like
74+
* interface, where the first argument specifies a format string on how to
75+
* stringify the rest of the parameter list.
76+
*
77+
* This function is only called with strings coming from the executed ECMAScript
78+
* wanting to print something as the result of its normal operation.
79+
*
80+
* It should be the port that decides what a "console" is.
81+
*
82+
* Example: a libc-based port may implement this with vprintf().
83+
*/
84+
void jerry_port_console (const char *format, ...);
85+
86+
/**
87+
* Jerry log levels. The levels are in severity order
88+
* where the most serious levels come first.
89+
*/
90+
typedef enum
91+
{
92+
JERRY_LOG_LEVEL_ERROR, /**< the engine will terminate after the message is printed */
93+
JERRY_LOG_LEVEL_WARNING, /**< a request is aborted, but the engine continues its operation */
94+
JERRY_LOG_LEVEL_DEBUG, /**< debug messages from the engine, low volume */
95+
JERRY_LOG_LEVEL_TRACE /**< detailed info about engine internals, potentially high volume */
96+
} jerry_log_level_t;
97+
98+
/**
99+
* Display or log a debug/error message. The function should implement a printf-like
100+
* interface, where the first argument specifies the log level
101+
* and the second argument specifies a format string on how to stringify the rest
102+
* of the parameter list.
103+
*
104+
* This function is only called with messages coming from the jerry engine as
105+
* the result of some abnormal operation or describing its internal operations
106+
* (e.g., data structure dumps or tracing info).
107+
*
108+
* It should be the port that decides whether error and debug messages are logged to
109+
* the console, or saved to a database or to a file.
110+
*
111+
* Example: a libc-based port may implement this with vfprintf(stderr) or
112+
* vfprintf(logfile), or both, depending on log level.
113+
*/
114+
void jerry_port_log (jerry_log_level_t level, const char *format, ...);
115+
74116
/*
75117
* Date Port API
76118
*/

jerry-core/jerry.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ static const char *wrong_args_msg_p = "wrong type of argument";
8686
*/
8787
int jerry_debug_level = 0;
8888

89-
/**
90-
* File, used for logging
91-
*/
92-
FILE *jerry_log_file = NULL;
93-
9489
#endif /* JERRY_ENABLE_LOG */
9590

9691
/**

jerry-core/jrt/jrt.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch
8686
#define JERRY_LOG(lvl, ...) \
8787
do \
8888
{ \
89-
if (lvl <= jerry_debug_level && jerry_log_file) \
89+
if (lvl <= jerry_debug_level) \
9090
{ \
91-
jerry_port_logmsg (jerry_log_file, __VA_ARGS__); \
91+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, __VA_ARGS__); \
9292
} \
9393
} \
9494
while (0)
@@ -109,8 +109,8 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch
109109
#define JERRY_DDDLOG(...) JERRY_DLOG (__VA_ARGS__)
110110
#endif /* JERRY_ENABLE_LOG */
111111

112-
#define JERRY_ERROR_MSG(...) jerry_port_errormsg (__VA_ARGS__)
113-
#define JERRY_WARNING_MSG(...) JERRY_ERROR_MSG (__VA_ARGS__)
112+
#define JERRY_ERROR_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_ERROR, __VA_ARGS__)
113+
#define JERRY_WARNING_MSG(...) jerry_port_log (JERRY_LOG_LEVEL_WARNING, __VA_ARGS__)
114114

115115
/**
116116
* Mark for unreachable points and unimplemented cases

main-unix.c

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ read_file (const char *file_name,
4848
FILE *file = fopen (file_name, "r");
4949
if (file == NULL)
5050
{
51-
jerry_port_errormsg ("Error: failed to open file: %s\n", file_name);
51+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
5252
return NULL;
5353
}
5454

5555
size_t bytes_read = fread (buffer, 1u, sizeof (buffer), file);
5656
if (!bytes_read)
5757
{
58-
jerry_port_errormsg ("Error: failed to read file: %s\n", file_name);
58+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
5959
fclose (file);
6060
return NULL;
6161
}
@@ -85,7 +85,7 @@ assert_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< f
8585
}
8686
else
8787
{
88-
jerry_port_errormsg ("Script error: assertion failed\n");
88+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script error: assertion failed\n");
8989
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
9090
}
9191
} /* assert_handler */
@@ -114,7 +114,6 @@ print_help (char *name)
114114
" --save-snapshot-for-eval FILE\n"
115115
" --exec-snapshot FILE\n"
116116
" --log-level [0-3]\n"
117-
" --log-file FILE\n"
118117
" --abort-on-fail\n"
119118
"\n",
120119
name);
@@ -126,8 +125,10 @@ main (int argc,
126125
{
127126
if (argc > JERRY_MAX_COMMAND_LINE_ARGS)
128127
{
129-
jerry_port_errormsg ("Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n",
130-
argc, JERRY_MAX_COMMAND_LINE_ARGS);
128+
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
129+
"Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n",
130+
argc,
131+
JERRY_MAX_COMMAND_LINE_ARGS);
131132

132133
return JERRY_STANDALONE_EXIT_CODE_FAIL;
133134
}
@@ -154,9 +155,6 @@ main (int argc,
154155

155156
bool is_repl_mode = false;
156157

157-
#ifdef JERRY_ENABLE_LOG
158-
const char *log_file_name = NULL;
159-
#endif /* JERRY_ENABLE_LOG */
160158
for (i = 1; i < argc; i++)
161159
{
162160
if (!strcmp ("-h", argv[i]) || !strcmp ("--help", argv[i]))
@@ -193,14 +191,14 @@ main (int argc,
193191

194192
if (save_snapshot_file_name_p != NULL)
195193
{
196-
jerry_port_errormsg ("Error: snapshot file name already specified\n");
194+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: snapshot file name already specified\n");
197195
print_usage (argv[0]);
198196
return JERRY_STANDALONE_EXIT_CODE_FAIL;
199197
}
200198

201199
if (++i >= argc)
202200
{
203-
jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]);
201+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]);
204202
print_usage (argv[0]);
205203
return JERRY_STANDALONE_EXIT_CODE_FAIL;
206204
}
@@ -211,7 +209,7 @@ main (int argc,
211209
{
212210
if (++i >= argc)
213211
{
214-
jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]);
212+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]);
215213
print_usage (argv[0]);
216214
return JERRY_STANDALONE_EXIT_CODE_FAIL;
217215
}
@@ -223,35 +221,21 @@ main (int argc,
223221
{
224222
if (++i >= argc)
225223
{
226-
jerry_port_errormsg ("Error: no level specified for %s\n", argv[i - 1]);
224+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no level specified for %s\n", argv[i - 1]);
227225
print_usage (argv[0]);
228226
return JERRY_STANDALONE_EXIT_CODE_FAIL;
229227
}
230228

231229
if (strlen (argv[i]) != 1 || argv[i][0] < '0' || argv[i][0] > '3')
232230
{
233-
jerry_port_errormsg ("Error: wrong format for %s\n", argv[i - 1]);
231+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format for %s\n", argv[i - 1]);
234232
print_usage (argv[0]);
235233
return JERRY_STANDALONE_EXIT_CODE_FAIL;
236234
}
237235

238236
#ifdef JERRY_ENABLE_LOG
239237
flags |= JERRY_INIT_ENABLE_LOG;
240238
jerry_debug_level = argv[i][0] - '0';
241-
#endif /* JERRY_ENABLE_LOG */
242-
}
243-
else if (!strcmp ("--log-file", argv[i]))
244-
{
245-
if (++i >= argc)
246-
{
247-
jerry_port_errormsg ("Error: no file specified for %s\n", argv[i - 1]);
248-
print_usage (argv[0]);
249-
return JERRY_STANDALONE_EXIT_CODE_FAIL;
250-
}
251-
252-
#ifdef JERRY_ENABLE_LOG
253-
flags |= JERRY_INIT_ENABLE_LOG;
254-
log_file_name = argv[i];
255239
#endif /* JERRY_ENABLE_LOG */
256240
}
257241
else if (!strcmp ("--abort-on-fail", argv[i]))
@@ -260,7 +244,7 @@ main (int argc,
260244
}
261245
else if (!strncmp ("-", argv[i], 1))
262246
{
263-
jerry_port_errormsg ("Error: unrecognized option: %s\n", argv[i]);
247+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: unrecognized option: %s\n", argv[i]);
264248
print_usage (argv[0]);
265249
return JERRY_STANDALONE_EXIT_CODE_FAIL;
266250
}
@@ -274,13 +258,15 @@ main (int argc,
274258
{
275259
if (files_counter != 1)
276260
{
277-
jerry_port_errormsg ("Error: --save-snapshot argument works with exactly one script\n");
261+
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
262+
"Error: --save-snapshot argument works with exactly one script\n");
278263
return JERRY_STANDALONE_EXIT_CODE_FAIL;
279264
}
280265

281266
if (exec_snapshots_count != 0)
282267
{
283-
jerry_port_errormsg ("Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n");
268+
jerry_port_log (JERRY_LOG_LEVEL_ERROR,
269+
"Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n");
284270
return JERRY_STANDALONE_EXIT_CODE_FAIL;
285271
}
286272
}
@@ -291,22 +277,6 @@ main (int argc,
291277
is_repl_mode = true;
292278
}
293279

294-
#ifdef JERRY_ENABLE_LOG
295-
if (log_file_name)
296-
{
297-
jerry_log_file = fopen (log_file_name, "w");
298-
if (jerry_log_file == NULL)
299-
{
300-
jerry_port_errormsg ("Error: failed to open log file: %s\n", log_file_name);
301-
return JERRY_STANDALONE_EXIT_CODE_FAIL;
302-
}
303-
}
304-
else
305-
{
306-
jerry_log_file = stdout;
307-
}
308-
#endif /* JERRY_ENABLE_LOG */
309-
310280
jerry_init (flags);
311281

312282
jerry_value_t global_obj_val = jerry_get_global_object ();
@@ -321,7 +291,7 @@ main (int argc,
321291

322292
if (!is_assert_added)
323293
{
324-
jerry_port_errormsg ("Warning: failed to register 'assert' method.");
294+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Warning: failed to register 'assert' method.");
325295
}
326296

327297
jerry_value_t ret_value = jerry_create_undefined ();
@@ -466,14 +436,6 @@ main (int argc,
466436
jerry_release_value (print_function);
467437
}
468438

469-
#ifdef JERRY_ENABLE_LOG
470-
if (jerry_log_file && jerry_log_file != stdout)
471-
{
472-
fclose (jerry_log_file);
473-
jerry_log_file = NULL;
474-
}
475-
#endif /* JERRY_ENABLE_LOG */
476-
477439
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
478440

479441
if (jerry_value_has_error_flag (ret_value))
@@ -489,7 +451,7 @@ main (int argc,
489451
assert (sz == err_str_size);
490452
err_str_buf[err_str_size] = 0;
491453

492-
jerry_port_errormsg ("Script Error: unhandled exception: %s\n", err_str_buf);
454+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: unhandled exception: %s\n", err_str_buf);
493455

494456
jerry_release_value (err_str_val);
495457

targets/default/jerry-port-default-io.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@
1818
#include "jerry-port.h"
1919

2020
/**
21-
* Provide log message to filestream implementation for the engine.
21+
* Provide console message implementation for the engine.
2222
*/
23-
int jerry_port_logmsg (FILE *stream, /**< stream pointer */
24-
const char *format, /**< format string */
25-
...) /**< parameters */
23+
void
24+
jerry_port_console (const char *format, /**< format string */
25+
...) /**< parameters */
2626
{
2727
va_list args;
28-
int count;
2928
va_start (args, format);
30-
count = vfprintf (stream, format, args);
29+
vfprintf (stdout, format, args);
3130
va_end (args);
32-
return count;
33-
} /* jerry_port_logmsg */
31+
} /* jerry_port_console */
3432

3533
/**
36-
* Provide error message to console implementation for the engine.
34+
* Provide log message implementation for the engine.
3735
*/
38-
int jerry_port_errormsg (const char *format, /**< format string */
39-
...) /**< parameters */
36+
void
37+
jerry_port_log (jerry_log_level_t level, /**< log level */
38+
const char *format, /**< format string */
39+
...) /**< parameters */
4040
{
41+
(void) level; /* default port implementation ignores the log level */
42+
4143
va_list args;
42-
int count;
4344
va_start (args, format);
44-
count = vfprintf (stderr, format, args);
45+
vfprintf (stderr, format, args);
4546
va_end (args);
46-
return count;
47-
} /* jerry_port_errormsg */
47+
} /* jerry_port_log */

0 commit comments

Comments
 (0)