Skip to content

Commit

Permalink
Rename jerry_port_log to jerry_port_log_buffer
Browse files Browse the repository at this point in the history
Add buffer_size parameter for function jerry_port_log_buffer, so that jerry_port_log_buffer
won't need calculate strlen when printing

Optimize util_print_chars use %.*s so that the number of strlen call is reduced.

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
  • Loading branch information
lygstate committed Dec 12, 2024
1 parent 8d44eed commit 0bcd765
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 104 deletions.
2 changes: 1 addition & 1 deletion docs/01.CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
5 changes: 4 additions & 1 deletion docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 37 additions & 51 deletions jerry-core/api/jerryscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -5226,17 +5212,17 @@ 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')
{
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;
}

Expand All @@ -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')
{
Expand All @@ -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;
}

Expand All @@ -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:
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions jerry-core/include/jerryscript-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
* @}
*/
Expand Down
5 changes: 4 additions & 1 deletion jerry-core/include/jerryscript-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions jerry-core/jrt/jrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions jerry-core/parser/js/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions jerry-ext/common/jext-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion jerry-ext/debugger/debugger-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion jerry-ext/debugger/debugger-serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion jerry-ext/debugger/debugger-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
9 changes: 3 additions & 6 deletions jerry-port/common/jerry-port-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions targets/baremetal-sdk/espressif/main/jerry-port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 5 additions & 8 deletions targets/os/mbedos/jerry-port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 0bcd765

Please sign in to comment.