diff --git a/docs/01.CONFIGURATION.md b/docs/01.CONFIGURATION.md index 1a263e73fa..506d280bfb 100644 --- a/docs/01.CONFIGURATION.md +++ b/docs/01.CONFIGURATION.md @@ -155,7 +155,7 @@ Enabling this feature provides detailed error messages where available, like lin ### Logging -This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log` port API function to print relevant log messages. +This option can be used to enable log messages during runtime. When enabled the engine will use the `jerry_port_log_buffer` port API function to print relevant log messages. This feature is disabled by default. | Options | | diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index 9feecf2565..015bbb6f0c 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -119,8 +119,11 @@ void jerry_port_context_free (void); * * The implementation can decide whether error and debug messages are logged to * the console, or saved to a database or to a file. + * + * @param buffer_p: input buffer + * @param buffer_size: data size */ -void jerry_port_log (const char *message_p); +void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size); ``` ```c diff --git a/jerry-core/api/jerryscript.c b/jerry-core/api/jerryscript.c index 5a874a3f32..aea81a19b8 100644 --- a/jerry-core/api/jerryscript.c +++ b/jerry-core/api/jerryscript.c @@ -5083,49 +5083,33 @@ jerry_log_set_level (jerry_log_level_t level) * Log a zero-terminated string message. * * @param str_p: message + * @param str_size: size of message */ static void -jerry_log_string (const char *str_p) +jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size) { - jerry_port_log (str_p); + jerry_port_log_buffer (str_p, str_size); #if JERRY_DEBUGGER if (jerry_debugger_is_connected ()) { - jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, - JERRY_DEBUGGER_OUTPUT_LOG, - (const uint8_t *) str_p, - strlen (str_p)); + jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, JERRY_DEBUGGER_OUTPUT_LOG, str_p, str_size); } #endif /* JERRY_DEBUGGER */ } /* jerry_log_string */ /** - * Log a fixed-size string message. + * Log a zero-terminated number string message. * - * @param str_p: message - * @param size: size - * @param buffer_p: buffer to use + * @param cursor_p: the number string cursor + * @param buffer_p: buffer used to construct the number string */ static void -jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p) +jerry_log_cursor (const jerry_char_t *cursor_p, jerry_char_t *buffer_p) { - const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1; - - while (size > batch_size) - { - memcpy (buffer_p, str_p, batch_size); - buffer_p[batch_size] = '\0'; - jerry_log_string (buffer_p); - - str_p += batch_size; - size -= batch_size; - } - - memcpy (buffer_p, str_p, size); - buffer_p[size] = '\0'; - jerry_log_string (buffer_p); -} /* jerry_log_string_fixed */ + jerry_char_t *tail_p = buffer_p + JERRY_LOG_BUFFER_SIZE - 1; + jerry_log_string (cursor_p, (jerry_size_t) (tail_p - cursor_p)); +} /* jerry_log_cursor */ /** * Format an unsigned number. @@ -5138,11 +5122,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p) * * @return formatted number as string */ -static char * -jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p) +static jerry_char_t * +jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p) { - static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE; + static const jerry_char_t digits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE; *(--cursor_p) = '\0'; uint8_t count = 0; @@ -5172,8 +5158,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra * * @return formatted number as string */ -static char * -jerry_format_int (int num, uint8_t width, char padding, char *buffer_p) +static jerry_char_t * +jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p) { if (num >= 0) { @@ -5184,15 +5170,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p) if (padding == '0' && width > 0) { - char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p); + jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p); *(--cursor_p) = '-'; return cursor_p; } - char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p); + jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p); *(--cursor_p) = '-'; - char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1; + jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1; while (cursor_p > indent_p) { @@ -5226,9 +5212,9 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) } va_list vl; - char buffer_p[JERRY_LOG_BUFFER_SIZE]; - uint32_t buffer_index = 0; - const char *cursor_p = format_p; + jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE]; + jerry_size_t buffer_index = 0; + const jerry_char_t *cursor_p = (const jerry_char_t *) format_p; va_start (vl, format_p); while (*cursor_p != '\0') @@ -5236,7 +5222,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) if (*cursor_p == '%' || buffer_index > JERRY_LOG_BUFFER_SIZE - 2) { buffer_p[buffer_index] = '\0'; - jerry_log_string (buffer_p); + jerry_log_string (buffer_p, buffer_index); buffer_index = 0; } @@ -5248,8 +5234,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) ++cursor_p; uint8_t width = 0; - size_t precision = 0; - char padding = ' '; + jerry_size_t precision = 0; + jerry_char_t padding = ' '; if (*cursor_p == '0') { @@ -5269,7 +5255,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) } else if (*cursor_p == '.' && *(cursor_p + 1) == '*') { - precision = (size_t) va_arg (vl, int); + precision = (jerry_size_t) va_arg (vl, int); cursor_p += 2; } @@ -5284,36 +5270,36 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) { case 's': { - char *str_p = va_arg (vl, char *); + jerry_char_t *str_p = va_arg (vl, jerry_char_t *); if (precision == 0) { - jerry_log_string (str_p); + jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p)); break; } - jerry_log_string_fixed (str_p, precision, buffer_p); + jerry_log_string (str_p, precision); break; } case 'c': { /* Arguments of types narrower than int are promoted to int for variadic functions */ - buffer_p[buffer_index++] = (char) va_arg (vl, int); + buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int); break; } case 'd': { - jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p)); + jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p); break; } case 'u': { - jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p)); + jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p); break; } case 'x': { - jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p)); + jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p); break; } default: @@ -5327,7 +5313,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...) if (buffer_index > 0) { buffer_p[buffer_index] = '\0'; - jerry_log_string (buffer_p); + jerry_log_string (buffer_p, buffer_index); } va_end (vl); diff --git a/jerry-core/include/jerryscript-compiler.h b/jerry-core/include/jerryscript-compiler.h index 4732f010f7..41117d237e 100644 --- a/jerry-core/include/jerryscript-compiler.h +++ b/jerry-core/include/jerryscript-compiler.h @@ -185,6 +185,13 @@ void *__cdecl _alloca (size_t _Size); #define JERRY_VLA(type, name, size) type name[size] #endif /* !JERRY_VLA */ +/** + * Helper to make sure unused parameters, variables, or expressions trigger no compiler warning. + */ +#ifndef JERRY_UNUSED +#define JERRY_UNUSED(x) ((void) (x)) +#endif /* !JERRY_UNUSED */ + /** * @} */ diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index 420ecd9bdf..cdcf9af7be 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -144,8 +144,11 @@ void jerry_port_context_free (void); * * The implementation can decide whether error and debug messages are logged to * the console, or saved to a database or to a file. + * + * @param buffer_p: input buffer that is a zero-terminated UTF-8 string + * @param buffer_size: data size */ -void jerry_port_log (const char *message_p); +void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size); /** * Print a buffer to standard output diff --git a/jerry-core/jrt/jrt.h b/jerry-core/jrt/jrt.h index 2c1f308d96..9c18653f81 100644 --- a/jerry-core/jrt/jrt.h +++ b/jerry-core/jrt/jrt.h @@ -30,11 +30,6 @@ */ #define JERRY_BITSINBYTE 8 -/* - * Make sure unused parameters, variables, or expressions trigger no compiler warning. - */ -#define JERRY_UNUSED(x) ((void) (x)) - #define JERRY_UNUSED_1(_1) JERRY_UNUSED (_1) #define JERRY_UNUSED_2(_1, _2) JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2) #define JERRY_UNUSED_3(_1, _2, _3) JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3) diff --git a/jerry-core/parser/js/common.c b/jerry-core/parser/js/common.c index eafd167704..26287fc425 100644 --- a/jerry-core/parser/js/common.c +++ b/jerry-core/parser/js/common.c @@ -65,11 +65,7 @@ static void util_print_chars (const uint8_t *char_p, /**< character pointer */ size_t size) /**< size */ { - while (size > 0) - { - JERRY_DEBUG_MSG ("%c", *char_p++); - size--; - } + JERRY_DEBUG_MSG ("%.*s", (int) size, char_p); } /* util_print_chars */ /** @@ -81,7 +77,7 @@ util_print_number (ecma_number_t num_p) /**< number to print */ lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER]; lit_utf8_size_t str_size = ecma_number_to_utf8_string (num_p, str_buf, sizeof (str_buf)); str_buf[str_size] = 0; - JERRY_DEBUG_MSG ("%s", str_buf); + JERRY_DEBUG_MSG ("%.*s", (int) str_size, str_buf); } /* util_print_number */ #if JERRY_BUILTIN_BIGINT diff --git a/jerry-ext/common/jext-common.h b/jerry-ext/common/jext-common.h index 946de890ba..afba6f35f9 100644 --- a/jerry-ext/common/jext-common.h +++ b/jerry-ext/common/jext-common.h @@ -22,11 +22,6 @@ #include "jerryscript-port.h" #include "jerryscript.h" -/* - * Make sure unused parameters, variables, or expressions trigger no compiler warning. - */ -#define JERRYX_UNUSED(x) ((void) (x)) - /* * Asserts * @@ -70,7 +65,7 @@ void JERRY_ATTR_NORETURN jerry_unreachable (const char *file, const char *functi { \ if (false) \ { \ - JERRYX_UNUSED (x); \ + JERRY_UNUSED (x); \ } \ } while (0) diff --git a/jerry-ext/debugger/debugger-common.c b/jerry-ext/debugger/debugger-common.c index 9b03f2239b..3f2915df95 100644 --- a/jerry-ext/debugger/debugger-common.c +++ b/jerry-ext/debugger/debugger-common.c @@ -35,7 +35,7 @@ jerryx_debugger_after_connect (bool success) /**< tells whether the connection jerry_debugger_transport_close (); } #else /* !(defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)) */ - JERRYX_UNUSED (success); + JERRY_UNUSED (success); #endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */ } /* jerryx_debugger_after_connect */ diff --git a/jerry-ext/debugger/debugger-serial.c b/jerry-ext/debugger/debugger-serial.c index 3a4215f087..8a0c5ff204 100644 --- a/jerry-ext/debugger/debugger-serial.c +++ b/jerry-ext/debugger/debugger-serial.c @@ -402,7 +402,7 @@ jerryx_debugger_serial_create (const char *config) /**< specify the configuratio bool jerryx_debugger_serial_create (const char *config) { - JERRYX_UNUSED (config); + JERRY_UNUSED (config); return false; } /* jerryx_debugger_serial_create */ diff --git a/jerry-ext/debugger/debugger-tcp.c b/jerry-ext/debugger/debugger-tcp.c index 8483470559..76cbc9a12d 100644 --- a/jerry-ext/debugger/debugger-tcp.c +++ b/jerry-ext/debugger/debugger-tcp.c @@ -399,7 +399,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */ bool jerryx_debugger_tcp_create (uint16_t port) { - JERRYX_UNUSED (port); + JERRY_UNUSED (port); return false; } /* jerryx_debugger_tcp_create */ diff --git a/jerry-port/common/jerry-port-io.c b/jerry-port/common/jerry-port-io.c index 26ae7c4445..eaa6633ca7 100644 --- a/jerry-port/common/jerry-port-io.c +++ b/jerry-port/common/jerry-port-io.c @@ -19,14 +19,11 @@ #include "jerryscript-port.h" -/** - * Default implementation of jerry_port_log. Prints log messages to stderr. - */ void JERRY_ATTR_WEAK -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - fputs (message_p, stderr); -} /* jerry_port_log */ + fwrite (buffer_p, 1, buffer_size, stderr); +} /* jerry_port_log_buffer */ void JERRY_ATTR_WEAK jerry_port_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) diff --git a/targets/baremetal-sdk/espressif/main/jerry-port.c b/targets/baremetal-sdk/espressif/main/jerry-port.c index 6655b21874..e79dc9cbb2 100644 --- a/targets/baremetal-sdk/espressif/main/jerry-port.c +++ b/targets/baremetal-sdk/espressif/main/jerry-port.c @@ -30,14 +30,11 @@ static const char ESP_JS_TAG[] = "JS"; -/** - * Provide log message implementation for the engine. - */ void -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - ESP_LOGI (ESP_JS_TAG, "%s", message_p); -} /* jerry_port_log */ + ESP_LOGI (ESP_JS_TAG, "%s", buffer_p); +} /* jerry_port_log_buffer */ /** * Implementation of jerry_port_fatal. diff --git a/targets/os/mbedos/jerry-port.cpp b/targets/os/mbedos/jerry-port.cpp index 9ba1383c48..cf7bc6f550 100644 --- a/targets/os/mbedos/jerry-port.cpp +++ b/targets/os/mbedos/jerry-port.cpp @@ -29,23 +29,20 @@ jerry_port_fatal (jerry_fatal_code_t code) exit ((int) code); } /* jerry_port_fatal */ -/** - * Provide log message implementation for the engine. - */ void -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - while (*message_p != '\0') + for (jerry_size_t i = 0; i < buffer_size; ++i) { - if (*message_p == '\n') + if (buffer_p[i] == '\n') { /* add CR for proper display in serial monitors */ fputc ('\r', stderr); } - fputc (*message_p++, stderr); + fputc (buffer_p[i], stderr); } -} /* jerry_port_log */ +} /* jerry_port_log_buffer */ /** * Dummy function to get the time zone adjustment. diff --git a/targets/os/nuttx/jerry-port.c b/targets/os/nuttx/jerry-port.c index c4f65a44a6..f90c918826 100644 --- a/targets/os/nuttx/jerry-port.c +++ b/targets/os/nuttx/jerry-port.c @@ -19,14 +19,12 @@ #include "jerryscript-port.h" -/** - * Default implementation of jerry_port_log. Prints log messages to stderr. - */ void -jerry_port_log (const char *message_p) /**< message */ +jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size) { - (void) message_p; -} /* jerry_port_log */ + JERRY_UNUSED (buffer_p); + JERRY_UNUSED (buffer_size); +} /* jerry_port_log_buffer */ /** * Read a line from standard input as a zero-terminated string. diff --git a/tests/unit-core/test-common.h b/tests/unit-core/test-common.h index b6d83491c5..b34166d96c 100644 --- a/tests/unit-core/test-common.h +++ b/tests/unit-core/test-common.h @@ -25,8 +25,6 @@ #include "jerryscript-port.h" -#define JERRY_UNUSED(x) ((void) (x)) - #define TEST_ASSERT(x) \ do \ { \ diff --git a/tests/unit-ext/test-common.h b/tests/unit-ext/test-common.h index 5941f46184..562e9470cc 100644 --- a/tests/unit-ext/test-common.h +++ b/tests/unit-ext/test-common.h @@ -20,8 +20,6 @@ #define ARRAY_SIZE(array) ((unsigned long) (sizeof (array) / sizeof ((array)[0]))) -#define JERRY_UNUSED(x) ((void) (x)) - #define TEST_ASSERT(x) \ do \ { \