From 8fbde244e90db5d52a3ede9ab1604f1f61a84a43 Mon Sep 17 00:00:00 2001 From: SaeHie Park Date: Mon, 30 Nov 2015 18:42:10 +0900 Subject: [PATCH] Move log message output(fprintf, puthcar) to target code Related issue #752 JerryScript-DCO-1.0-Signed-off-by: SaeHie Park saehie.park@samsung.com --- jerry-core/jerry-port.h | 47 ++++++++++++++++++++++ jerry-core/jerry.h | 1 + jerry-core/jrt/jrt.h | 4 +- jerry-core/lit/lit-strings.cpp | 4 +- jerry-core/parser/js/jsp-early-error.h | 2 +- jerry-core/vm/pretty-printer.cpp | 4 +- main-darwin.cpp | 1 + main-linux.cpp | 1 + main-mcu.cpp | 1 + main.h | 54 ++++++++++++++++++++++++++ tests/unit/test-api.cpp | 35 +++++++++++++++++ tests/unit/test-parser.cpp | 8 ++++ 12 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 jerry-core/jerry-port.h create mode 100644 main.h diff --git a/jerry-core/jerry-port.h b/jerry-core/jerry-port.h new file mode 100644 index 000000000..f31a0c049 --- /dev/null +++ b/jerry-core/jerry-port.h @@ -0,0 +1,47 @@ +/* Copyright 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JERRY_PORT_H +#define JERRY_PORT_H + +#include + +#ifdef __cplusplus +# define EXTERN_C "C" +#else /* !__cplusplus */ +# define EXTERN_C +#endif /* !__cplusplus */ + +/** \addtogroup jerry Jerry engine port + * @{ + */ + +/** + * Target port functions for console output + */ +extern EXTERN_C +int jerry_port_logmsg (FILE* stream, const char* format, ...); + +extern EXTERN_C +int jerry_port_errormsg (const char* format, ...); + +extern EXTERN_C +int jerry_port_putchar (int c); + +/** + * @} + */ + +#endif /* !JERRY_API_H */ diff --git a/jerry-core/jerry.h b/jerry-core/jerry.h index 3d979a78b..4551e7d57 100644 --- a/jerry-core/jerry.h +++ b/jerry-core/jerry.h @@ -20,6 +20,7 @@ #include #include "jerry-api.h" +#include "jerry-port.h" /** \addtogroup jerry Jerry engine interface * @{ diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index 6dc4c1913..5745ec40f 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -99,7 +99,7 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch { \ if (lvl <= jerry_debug_level && jerry_log_file) \ { \ - fprintf (jerry_log_file, __VA_ARGS__); \ + jerry_port_logmsg (jerry_log_file, __VA_ARGS__); \ } \ } \ while (0) @@ -120,7 +120,7 @@ extern void __noreturn jerry_unimplemented (const char *, const char *, const ch #define JERRY_DDDLOG(...) JERRY_DLOG (__VA_ARGS__) #endif /* !JERRY_ENABLE_LOG */ -#define JERRY_ERROR_MSG(...) fprintf (stderr, __VA_ARGS__) +#define JERRY_ERROR_MSG(...) jerry_port_errormsg (__VA_ARGS__) #define JERRY_WARNING_MSG(...) JERRY_ERROR_MSG (__VA_ARGS__) /** diff --git a/jerry-core/lit/lit-strings.cpp b/jerry-core/lit/lit-strings.cpp index 93c4a81f9..e86f9dcaf 100644 --- a/jerry-core/lit/lit-strings.cpp +++ b/jerry-core/lit/lit-strings.cpp @@ -1005,11 +1005,11 @@ lit_put_ecma_char (ecma_char_t ecma_char) /**< code unit */ { if (ecma_char <= LIT_UTF8_1_BYTE_CODE_POINT_MAX) { - putchar (ecma_char); + jerry_port_putchar (ecma_char); } else { FIXME ("Support unicode characters printing."); - putchar ('_'); + jerry_port_putchar ('_'); } } /* lit_put_ecma_char */ diff --git a/jerry-core/parser/js/jsp-early-error.h b/jerry-core/parser/js/jsp-early-error.h index b3b595872..430397d6d 100644 --- a/jerry-core/parser/js/jsp-early-error.h +++ b/jerry-core/parser/js/jsp-early-error.h @@ -27,7 +27,7 @@ lexer_dump_line (line); \ printf ("\n"); \ for (size_t i = 0; i < column; i++) { \ - putchar (' '); \ + jerry_port_putchar (' '); \ } \ printf ("^\n"); \ printf ("%s: Ln %lu, Col %lu: ", TYPE, (unsigned long) (line + 1), (unsigned long) (column + 1)); \ diff --git a/jerry-core/vm/pretty-printer.cpp b/jerry-core/vm/pretty-printer.cpp index 04c28dc85..cbe41f979 100644 --- a/jerry-core/vm/pretty-printer.cpp +++ b/jerry-core/vm/pretty-printer.cpp @@ -123,7 +123,7 @@ pp_printf (const char *format, vm_instr_t instr, lit_cpointer_t lit_ids[], vm_in { if (*format != '%') { - putchar (*format); + jerry_port_putchar (*format); format++; continue; } @@ -144,7 +144,7 @@ pp_printf (const char *format, vm_instr_t instr, lit_cpointer_t lit_ids[], vm_in } default: { - putchar ('%'); + jerry_port_putchar ('%'); continue; } } diff --git a/main-darwin.cpp b/main-darwin.cpp index a5f741887..7a38f7452 100644 --- a/main-darwin.cpp +++ b/main-darwin.cpp @@ -19,6 +19,7 @@ #include "jerry.h" #include "jrt/jrt.h" +#include "main.h" /** * Maximum command line arguments number diff --git a/main-linux.cpp b/main-linux.cpp index e25ea99c5..6805b8195 100644 --- a/main-linux.cpp +++ b/main-linux.cpp @@ -19,6 +19,7 @@ #include "jerry.h" #include "jrt/jrt.h" +#include "main.h" /** * Maximum command line arguments number diff --git a/main-mcu.cpp b/main-mcu.cpp index 428b6ef43..56aec57f0 100644 --- a/main-mcu.cpp +++ b/main-mcu.cpp @@ -14,6 +14,7 @@ */ #include "jerry.h" +#include "main.h" /** * Standalone Jerry exit codes diff --git a/main.h b/main.h new file mode 100644 index 000000000..d171e1f8b --- /dev/null +++ b/main.h @@ -0,0 +1,54 @@ +/* Copyright 2014-2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MAIN_H +#define MAIN_H + +/** + * Provide log message to filestream implementation for the engine. + */ +int jerry_port_logmsg (FILE* stream, const char* format, ...) +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stream, format, args); + va_end (args); + return count; +} + +/** + * Provide error message to console implementation for the engine. + */ +int jerry_port_errormsg (const char* format, ...) +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stderr, format, args); + va_end (args); + return count; +} + + +/** + * Provide output character to console implementation for the engine. + */ +int jerry_port_putchar (int c) +{ + return putchar (c); +} + +#endif /* !MAIN_H */ diff --git a/tests/unit/test-api.cpp b/tests/unit/test-api.cpp index 0feebd176..75adbc4d7 100644 --- a/tests/unit/test-api.cpp +++ b/tests/unit/test-api.cpp @@ -737,3 +737,38 @@ main (void) return 0; } + +/** + * Provide log message to filestream implementation for the engine. + */ +int jerry_port_logmsg (FILE* stream, const char* format, ...) +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stream, format, args); + va_end (args); + return count; +} + +/** + * Provide error message to console implementation for the engine. + */ +int jerry_port_errormsg (const char* format, ...) +{ + va_list args; + int count; + va_start (args, format); + count = vfprintf (stderr, format, args); + va_end (args); + return count; +} + + +/** + * Provide output character to console implementation for the engine. + */ +int jerry_port_putchar (int c) +{ + return putchar (c); +} diff --git a/tests/unit/test-parser.cpp b/tests/unit/test-parser.cpp index 5ab05552b..0656ae0f3 100644 --- a/tests/unit/test-parser.cpp +++ b/tests/unit/test-parser.cpp @@ -147,3 +147,11 @@ main (int __attr_unused___ argc, return 0; } /* main */ + +/** + * Provide output character to console implementation for the engine. + */ +int jerry_port_putchar (int c) +{ + return putchar (c); +}