Skip to content

Commit 7daeebd

Browse files
committed
Rename jerry_port_log to jerry_port_log_buffer
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]
1 parent d2d30df commit 7daeebd

File tree

17 files changed

+72
-97
lines changed

17 files changed

+72
-97
lines changed

docs/01.CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Enabling this feature provides detailed error messages where available, like lin
155155

156156
### Logging
157157

158-
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.
158+
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.
159159
This feature is disabled by default.
160160

161161
| Options | |

docs/05.PORT-API.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ void jerry_port_context_free (void);
120120
* The implementation can decide whether error and debug messages are logged to
121121
* the console, or saved to a database or to a file.
122122
*
123-
* @param message_p: the message to log.
123+
* @param buffer_p: input buffer
124+
* @param buffer_size: data size
124125
*/
125-
void jerry_port_log (const char *message_p);
126+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
126127
```
127128
128129
```c

jerry-core/api/jerryscript.c

+37-51
Original file line numberDiff line numberDiff line change
@@ -5077,49 +5077,33 @@ jerry_log_set_level (jerry_log_level_t level)
50775077
* Log a zero-terminated string message.
50785078
*
50795079
* @param str_p: message
5080+
* @param str_size: size of message
50805081
*/
50815082
static void
5082-
jerry_log_string (const char *str_p)
5083+
jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size)
50835084
{
5084-
jerry_port_log (str_p);
5085+
jerry_port_log_buffer (str_p, str_size);
50855086

50865087
#if JERRY_DEBUGGER
50875088
if (jerry_debugger_is_connected ())
50885089
{
5089-
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT,
5090-
JERRY_DEBUGGER_OUTPUT_LOG,
5091-
(const uint8_t *) str_p,
5092-
strlen (str_p));
5090+
jerry_debugger_send_string (JERRY_DEBUGGER_OUTPUT_RESULT, JERRY_DEBUGGER_OUTPUT_LOG, str_p, str_size);
50935091
}
50945092
#endif /* JERRY_DEBUGGER */
50955093
} /* jerry_log_string */
50965094

50975095
/**
5098-
* Log a fixed-size string message.
5096+
* Log a zero-terminated number string message.
50995097
*
5100-
* @param str_p: message
5101-
* @param size: size
5102-
* @param buffer_p: buffer to use
5098+
* @param cursor_p: the number string cursor
5099+
* @param buffer_p: buffer used to construct the number string
51035100
*/
51045101
static void
5105-
jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
5102+
jerry_log_cursor (const jerry_char_t *cursor_p, jerry_char_t *buffer_p)
51065103
{
5107-
const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1;
5108-
5109-
while (size > batch_size)
5110-
{
5111-
memcpy (buffer_p, str_p, batch_size);
5112-
buffer_p[batch_size] = '\0';
5113-
jerry_log_string (buffer_p);
5114-
5115-
str_p += batch_size;
5116-
size -= batch_size;
5117-
}
5118-
5119-
memcpy (buffer_p, str_p, size);
5120-
buffer_p[size] = '\0';
5121-
jerry_log_string (buffer_p);
5122-
} /* jerry_log_string_fixed */
5104+
jerry_char_t *tail_p = buffer_p + JERRY_LOG_BUFFER_SIZE - 1;
5105+
jerry_log_string (cursor_p, (jerry_size_t) (tail_p - cursor_p));
5106+
} /* jerry_log_cursor */
51235107

51245108
/**
51255109
* Format an unsigned number.
@@ -5132,11 +5116,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
51325116
*
51335117
* @return formatted number as string
51345118
*/
5135-
static char *
5136-
jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p)
5119+
static jerry_char_t *
5120+
jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p)
51375121
{
5138-
static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
5139-
char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
5122+
static const jerry_char_t digits[] = {
5123+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
5124+
};
5125+
jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
51405126
*(--cursor_p) = '\0';
51415127
uint8_t count = 0;
51425128

@@ -5166,8 +5152,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra
51665152
*
51675153
* @return formatted number as string
51685154
*/
5169-
static char *
5170-
jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
5155+
static jerry_char_t *
5156+
jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p)
51715157
{
51725158
if (num >= 0)
51735159
{
@@ -5178,15 +5164,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
51785164

51795165
if (padding == '0' && width > 0)
51805166
{
5181-
char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
5167+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
51825168
*(--cursor_p) = '-';
51835169
return cursor_p;
51845170
}
51855171

5186-
char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
5172+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
51875173
*(--cursor_p) = '-';
51885174

5189-
char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
5175+
jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
51905176

51915177
while (cursor_p > indent_p)
51925178
{
@@ -5220,17 +5206,17 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52205206
}
52215207

52225208
va_list vl;
5223-
char buffer_p[JERRY_LOG_BUFFER_SIZE];
5224-
uint32_t buffer_index = 0;
5225-
const char *cursor_p = format_p;
5209+
jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE];
5210+
jerry_size_t buffer_index = 0;
5211+
const jerry_char_t *cursor_p = (const jerry_char_t *) format_p;
52265212
va_start (vl, format_p);
52275213

52285214
while (*cursor_p != '\0')
52295215
{
52305216
if (*cursor_p == '%' || buffer_index > JERRY_LOG_BUFFER_SIZE - 2)
52315217
{
52325218
buffer_p[buffer_index] = '\0';
5233-
jerry_log_string (buffer_p);
5219+
jerry_log_string (buffer_p, buffer_index);
52345220
buffer_index = 0;
52355221
}
52365222

@@ -5242,8 +5228,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52425228

52435229
++cursor_p;
52445230
uint8_t width = 0;
5245-
size_t precision = 0;
5246-
char padding = ' ';
5231+
jerry_size_t precision = 0;
5232+
jerry_char_t padding = ' ';
52475233

52485234
if (*cursor_p == '0')
52495235
{
@@ -5263,7 +5249,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52635249
}
52645250
else if (*cursor_p == '.' && *(cursor_p + 1) == '*')
52655251
{
5266-
precision = (size_t) va_arg (vl, int);
5252+
precision = (jerry_size_t) va_arg (vl, int);
52675253
cursor_p += 2;
52685254
}
52695255

@@ -5278,36 +5264,36 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52785264
{
52795265
case 's':
52805266
{
5281-
char *str_p = va_arg (vl, char *);
5267+
jerry_char_t *str_p = va_arg (vl, jerry_char_t *);
52825268

52835269
if (precision == 0)
52845270
{
5285-
jerry_log_string (str_p);
5271+
jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p));
52865272
break;
52875273
}
52885274

5289-
jerry_log_string_fixed (str_p, precision, buffer_p);
5275+
jerry_log_string (str_p, precision);
52905276
break;
52915277
}
52925278
case 'c':
52935279
{
52945280
/* Arguments of types narrower than int are promoted to int for variadic functions */
5295-
buffer_p[buffer_index++] = (char) va_arg (vl, int);
5281+
buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int);
52965282
break;
52975283
}
52985284
case 'd':
52995285
{
5300-
jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p));
5286+
jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p);
53015287
break;
53025288
}
53035289
case 'u':
53045290
{
5305-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p));
5291+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p);
53065292
break;
53075293
}
53085294
case 'x':
53095295
{
5310-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p));
5296+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p);
53115297
break;
53125298
}
53135299
default:
@@ -5321,7 +5307,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53215307
if (buffer_index > 0)
53225308
{
53235309
buffer_p[buffer_index] = '\0';
5324-
jerry_log_string (buffer_p);
5310+
jerry_log_string (buffer_p, buffer_index);
53255311
}
53265312

53275313
va_end (vl);

jerry-core/include/jerryscript-compiler.h

+7
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ void *__cdecl _alloca (size_t _Size);
196196
#define JERRY_VLA(type, name, size) type name[size]
197197
#endif /* !JERRY_VLA */
198198

199+
/**
200+
* Helper to make sure unused parameters, variables, or expressions trigger no compiler warning.
201+
*/
202+
#ifndef JERRY_UNUSED
203+
#define JERRY_UNUSED(x) ((void) (x))
204+
#endif /* !JERRY_UNUSED */
205+
199206
/**
200207
* @}
201208
*/

jerry-core/include/jerryscript-port.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ void jerry_port_context_free (void);
144144
* The implementation can decide whether error and debug messages are logged to
145145
* the console, or saved to a database or to a file.
146146
*
147-
* @param message_p: the message to log.
147+
* @param buffer_p: input buffer that is a zero-terminated UTF-8 string
148+
* @param buffer_size: data size
148149
*/
149-
void jerry_port_log (const char *message_p);
150+
void jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size);
150151

151152
/**
152153
* Print a buffer to standard output

jerry-core/jrt/jrt.h

-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
*/
3131
#define JERRY_BITSINBYTE 8
3232

33-
/*
34-
* Make sure unused parameters, variables, or expressions trigger no compiler warning.
35-
*/
36-
#define JERRY_UNUSED(x) ((void) (x))
37-
3833
#define JERRY_UNUSED_1(_1) JERRY_UNUSED (_1)
3934
#define JERRY_UNUSED_2(_1, _2) JERRY_UNUSED (_1), JERRY_UNUSED_1 (_2)
4035
#define JERRY_UNUSED_3(_1, _2, _3) JERRY_UNUSED (_1), JERRY_UNUSED_2 (_2, _3)

jerry-core/parser/js/common.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ static void
6565
util_print_chars (const uint8_t *char_p, /**< character pointer */
6666
size_t size) /**< size */
6767
{
68-
while (size > 0)
69-
{
70-
JERRY_DEBUG_MSG ("%c", *char_p++);
71-
size--;
72-
}
68+
JERRY_DEBUG_MSG ("%.*s", (int) size, char_p);
7369
} /* util_print_chars */
7470

7571
/**
@@ -81,7 +77,7 @@ util_print_number (ecma_number_t num_p) /**< number to print */
8177
lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
8278
lit_utf8_size_t str_size = ecma_number_to_utf8_string (num_p, str_buf, sizeof (str_buf));
8379
str_buf[str_size] = 0;
84-
JERRY_DEBUG_MSG ("%s", str_buf);
80+
JERRY_DEBUG_MSG ("%.*s", (int) str_size, str_buf);
8581
} /* util_print_number */
8682

8783
#if JERRY_BUILTIN_BIGINT

jerry-ext/common/jext-common.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include "jerryscript-port.h"
2323
#include "jerryscript.h"
2424

25-
/*
26-
* Make sure unused parameters, variables, or expressions trigger no compiler warning.
27-
*/
28-
#define JERRYX_UNUSED(x) ((void) (x))
29-
3025
/*
3126
* Asserts
3227
*
@@ -70,7 +65,7 @@ void JERRY_ATTR_NORETURN jerry_unreachable (const char *file, const char *functi
7065
{ \
7166
if (false) \
7267
{ \
73-
JERRYX_UNUSED (x); \
68+
JERRY_UNUSED (x); \
7469
} \
7570
} while (0)
7671

jerry-ext/debugger/debugger-common.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jerryx_debugger_after_connect (bool success) /**< tells whether the connection
3535
jerry_debugger_transport_close ();
3636
}
3737
#else /* !(defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)) */
38-
JERRYX_UNUSED (success);
38+
JERRY_UNUSED (success);
3939
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
4040
} /* jerryx_debugger_after_connect */
4141

jerry-ext/debugger/debugger-serial.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ jerryx_debugger_serial_create (const char *config) /**< specify the configuratio
402402
bool
403403
jerryx_debugger_serial_create (const char *config)
404404
{
405-
JERRYX_UNUSED (config);
405+
JERRY_UNUSED (config);
406406
return false;
407407
} /* jerryx_debugger_serial_create */
408408

jerry-ext/debugger/debugger-tcp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */
399399
bool
400400
jerryx_debugger_tcp_create (uint16_t port)
401401
{
402-
JERRYX_UNUSED (port);
402+
JERRY_UNUSED (port);
403403
return false;
404404
} /* jerryx_debugger_tcp_create */
405405

jerry-port/common/jerry-port-io.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919

2020
#include "jerryscript-port.h"
2121

22-
/**
23-
* Default implementation of jerry_port_log. Prints log messages to stderr.
24-
*/
2522
void JERRY_ATTR_WEAK
26-
jerry_port_log (const char *message_p) /**< message */
23+
jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
2724
{
28-
fputs (message_p, stderr);
29-
} /* jerry_port_log */
25+
fwrite (buffer_p, 1, buffer_size, stderr);
26+
} /* jerry_port_log_buffer */
3027

3128
void JERRY_ATTR_WEAK
3229
jerry_port_print_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)

targets/baremetal-sdk/espressif/main/jerry-port.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
static const char ESP_JS_TAG[] = "JS";
3232

3333
void
34-
jerry_port_log (const char *message_p)
34+
jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
3535
{
36-
ESP_LOGI (ESP_JS_TAG, "%s", message_p);
37-
} /* jerry_port_log */
36+
ESP_LOGI (ESP_JS_TAG, "%s", buffer_p);
37+
} /* jerry_port_log_buffer */
3838

3939
void
4040
jerry_port_fatal (jerry_fatal_code_t code)

targets/os/mbedos/jerry-port.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ jerry_port_fatal (jerry_fatal_code_t code)
2727
} /* jerry_port_fatal */
2828

2929
void
30-
jerry_port_log (const char *message_p)
30+
jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
3131
{
32-
while (*message_p != '\0')
32+
for (jerry_size_t i = 0; i < buffer_size; ++i)
3333
{
34-
if (*message_p == '\n')
34+
if (buffer_p[i] == '\n')
3535
{
3636
/* add CR for proper display in serial monitors */
3737
fputc ('\r', stderr);
3838
}
3939

40-
fputc (*message_p++, stderr);
40+
fputc (buffer_p[i], stderr);
4141
}
42-
} /* jerry_port_log */
42+
} /* jerry_port_log_buffer */
4343

4444
int32_t
4545
jerry_port_local_tza (double unix_ms)

0 commit comments

Comments
 (0)