Skip to content

Commit 5f59ba0

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 JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
1 parent 3d35c0a commit 5f59ba0

File tree

16 files changed

+72
-98
lines changed

16 files changed

+72
-98
lines changed

docs/01.CONFIGURATION.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ void jerry_port_context_free (void);
119119
*
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.
122+
*
123+
* @param buffer_p: input buffer
124+
* @param buffer_size: data size
122125
*/
123-
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);
124127
```
125128
126129
```c

jerry-core/api/jerryscript.c

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5078,49 +5078,33 @@ jerry_log_set_level (jerry_log_level_t level)
50785078
* Log a zero-terminated string message.
50795079
*
50805080
* @param str_p: message
5081+
* @param str_size: size of message
50815082
*/
50825083
static void
5083-
jerry_log_string (const char *str_p)
5084+
jerry_log_string (const jerry_char_t *str_p, jerry_size_t str_size)
50845085
{
5085-
jerry_port_log (str_p);
5086+
jerry_port_log_buffer (str_p, str_size);
50865087

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

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

51255109
/**
51265110
* Format an unsigned number.
@@ -5133,11 +5117,13 @@ jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
51335117
*
51345118
* @return formatted number as string
51355119
*/
5136-
static char *
5137-
jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p)
5120+
static jerry_char_t *
5121+
jerry_format_unsigned (unsigned int num, uint8_t width, jerry_char_t padding, uint8_t radix, jerry_char_t *buffer_p)
51385122
{
5139-
static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
5140-
char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
5123+
static const jerry_char_t digits[] = {
5124+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
5125+
};
5126+
jerry_char_t *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
51415127
*(--cursor_p) = '\0';
51425128
uint8_t count = 0;
51435129

@@ -5167,8 +5153,8 @@ jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t ra
51675153
*
51685154
* @return formatted number as string
51695155
*/
5170-
static char *
5171-
jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
5156+
static jerry_char_t *
5157+
jerry_format_int (int num, uint8_t width, jerry_char_t padding, jerry_char_t *buffer_p)
51725158
{
51735159
if (num >= 0)
51745160
{
@@ -5179,15 +5165,15 @@ jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
51795165

51805166
if (padding == '0' && width > 0)
51815167
{
5182-
char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
5168+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
51835169
*(--cursor_p) = '-';
51845170
return cursor_p;
51855171
}
51865172

5187-
char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
5173+
jerry_char_t *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
51885174
*(--cursor_p) = '-';
51895175

5190-
char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
5176+
jerry_char_t *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
51915177

51925178
while (cursor_p > indent_p)
51935179
{
@@ -5221,17 +5207,17 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52215207
}
52225208

52235209
va_list vl;
5224-
char buffer_p[JERRY_LOG_BUFFER_SIZE];
5225-
uint32_t buffer_index = 0;
5226-
const char *cursor_p = format_p;
5210+
jerry_char_t buffer_p[JERRY_LOG_BUFFER_SIZE];
5211+
jerry_size_t buffer_index = 0;
5212+
const jerry_char_t *cursor_p = (const jerry_char_t *) format_p;
52275213
va_start (vl, format_p);
52285214

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

@@ -5243,8 +5229,8 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52435229

52445230
++cursor_p;
52455231
uint8_t width = 0;
5246-
size_t precision = 0;
5247-
char padding = ' ';
5232+
jerry_size_t precision = 0;
5233+
jerry_char_t padding = ' ';
52485234

52495235
if (*cursor_p == '0')
52505236
{
@@ -5264,7 +5250,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52645250
}
52655251
else if (*cursor_p == '.' && *(cursor_p + 1) == '*')
52665252
{
5267-
precision = (size_t) va_arg (vl, int);
5253+
precision = (jerry_size_t) va_arg (vl, int);
52685254
cursor_p += 2;
52695255
}
52705256

@@ -5279,36 +5265,36 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52795265
{
52805266
case 's':
52815267
{
5282-
char *str_p = va_arg (vl, char *);
5268+
jerry_char_t *str_p = va_arg (vl, jerry_char_t *);
52835269

52845270
if (precision == 0)
52855271
{
5286-
jerry_log_string (str_p);
5272+
jerry_log_string (str_p, (jerry_size_t) strlen ((const char *) str_p));
52875273
break;
52885274
}
52895275

5290-
jerry_log_string_fixed (str_p, precision, buffer_p);
5276+
jerry_log_string (str_p, precision);
52915277
break;
52925278
}
52935279
case 'c':
52945280
{
52955281
/* Arguments of types narrower than int are promoted to int for variadic functions */
5296-
buffer_p[buffer_index++] = (char) va_arg (vl, int);
5282+
buffer_p[buffer_index++] = (jerry_char_t) va_arg (vl, int);
52975283
break;
52985284
}
52995285
case 'd':
53005286
{
5301-
jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p));
5287+
jerry_log_cursor (jerry_format_int (va_arg (vl, int), width, padding, buffer_p), buffer_p);
53025288
break;
53035289
}
53045290
case 'u':
53055291
{
5306-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p));
5292+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p), buffer_p);
53075293
break;
53085294
}
53095295
case 'x':
53105296
{
5311-
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p));
5297+
jerry_log_cursor (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p), buffer_p);
53125298
break;
53135299
}
53145300
default:
@@ -5322,7 +5308,7 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
53225308
if (buffer_index > 0)
53235309
{
53245310
buffer_p[buffer_index] = '\0';
5325-
jerry_log_string (buffer_p);
5311+
jerry_log_string (buffer_p, buffer_index);
53265312
}
53275313

53285314
va_end (vl);

jerry-core/include/jerryscript-compiler.h

Lines changed: 7 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ void jerry_port_context_free (void);
143143
*
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.
146+
*
147+
* @param buffer_p: input buffer that is a zero-terminated UTF-8 string
148+
* @param buffer_size: data size
146149
*/
147-
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);
148151

149152
/**
150153
* Print a buffer to standard output

jerry-core/jrt/jrt.h

Lines changed: 0 additions & 5 deletions
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-ext/common/jext-common.h

Lines changed: 1 addition & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ jerryx_debugger_tcp_create (uint16_t port) /**< listening port */
423423
bool
424424
jerryx_debugger_tcp_create (uint16_t port)
425425
{
426-
JERRYX_UNUSED (port);
426+
JERRY_UNUSED (port);
427427
return false;
428428
} /* jerryx_debugger_tcp_create */
429429

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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030

3131
static const char ESP_JS_TAG[] = "JS";
3232

33-
/**
34-
* Provide log message implementation for the engine.
35-
*/
3633
void
37-
jerry_port_log (const char *message_p) /**< message */
34+
jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
3835
{
39-
ESP_LOGI (ESP_JS_TAG, "%s", message_p);
40-
} /* jerry_port_log */
36+
ESP_LOGI (ESP_JS_TAG, "%s", buffer_p);
37+
} /* jerry_port_log_buffer */
4138

4239
/**
4340
* Implementation of jerry_port_fatal.

targets/os/mbedos/jerry-port.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,20 @@ jerry_port_fatal (jerry_fatal_code_t code)
2929
exit ((int) code);
3030
} /* jerry_port_fatal */
3131

32-
/**
33-
* Provide log message implementation for the engine.
34-
*/
3532
void
36-
jerry_port_log (const char *message_p) /**< message */
33+
jerry_port_log_buffer (const jerry_char_t *buffer_p, jerry_size_t buffer_size)
3734
{
38-
while (*message_p != '\0')
35+
for (jerry_size_t i = 0; i < buffer_size; ++i)
3936
{
40-
if (*message_p == '\n')
37+
if (buffer_p[i] == '\n')
4138
{
4239
/* add CR for proper display in serial monitors */
4340
fputc ('\r', stderr);
4441
}
4542

46-
fputc (*message_p++, stderr);
43+
fputc (buffer_p[i], stderr);
4744
}
48-
} /* jerry_port_log */
45+
} /* jerry_port_log_buffer */
4946

5047
/**
5148
* Dummy function to get the time zone adjustment.

targets/os/nuttx/jerry-port.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919

2020
#include "jerryscript-port.h"
2121

22-
/**
23-
* Default implementation of jerry_port_log. Prints log messages to stderr.
24-
*/
2522
void
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-
(void) message_p;
29-
} /* jerry_port_log */
25+
JERRY_UNUSED (buffer_p);
26+
JERRY_UNUSED (buffer_size);
27+
} /* jerry_port_log_buffer */
3028

3129
/**
3230
* Read a line from standard input as a zero-terminated string.

0 commit comments

Comments
 (0)