Skip to content

Commit 9929e64

Browse files
committed
Fix logging related issues
PR jerryscript-project#4907 moved the log level into the engine context, however this caused issues with logging without the engine being initialized. This commit reverts the log level to be a static variable. This commit also implements missing format specifiers for jerry_log. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
1 parent aead4b7 commit 9929e64

File tree

6 files changed

+200
-40
lines changed

6 files changed

+200
-40
lines changed

jerry-core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ set(SOURCE_CORE_FILES
300300
jmem/jmem-heap.c
301301
jmem/jmem-poolman.c
302302
jrt/jrt-fatals.c
303+
jrt/jrt-logging.c
303304
lit/lit-char-helpers.c
304305
lit/lit-magic-strings.c
305306
lit/lit-strings.c

jerry-core/api/jerryscript.c

Lines changed: 142 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5154,7 +5154,7 @@ jerry_validate_string (const jerry_char_t *buffer_p, /**< string buffer */
51545154
void
51555155
jerry_log_set_level (jerry_log_level_t level)
51565156
{
5157-
JERRY_CONTEXT (log_level) = level;
5157+
jerry_jrt_set_log_level (level);
51585158
} /* jerry_log_set_level */
51595159

51605160
/**
@@ -5184,41 +5184,126 @@ jerry_log_string (const char *str_p)
51845184
} /* jerry_log_string */
51855185

51865186
/**
5187-
* Log an unsigned number.
5187+
* Log a fixed-size string message.
5188+
*
5189+
* @param str_p: message
5190+
* @param size: size
5191+
* @param buffer_p: buffer to use
5192+
*/
5193+
static void
5194+
jerry_log_string_fixed (const char *str_p, size_t size, char *buffer_p)
5195+
{
5196+
const size_t batch_size = JERRY_LOG_BUFFER_SIZE - 1;
5197+
5198+
while (size > batch_size)
5199+
{
5200+
memcpy (buffer_p, str_p, batch_size);
5201+
buffer_p[batch_size] = '\0';
5202+
jerry_log_string (buffer_p);
5203+
5204+
str_p += batch_size;
5205+
size -= batch_size;
5206+
}
5207+
5208+
memcpy (buffer_p, str_p, size);
5209+
buffer_p[size] = '\0';
5210+
jerry_log_string (buffer_p);
5211+
} /* jerry_log_string_fixed */
5212+
5213+
/**
5214+
* Format an unsigned number.
51885215
*
51895216
* @param num: number
5217+
* @param width: minimum width of the number
5218+
* @param padding: padding to be used when extending to minimum width
5219+
* @param radix: number radix
51905220
* @param buffer_p: buffer used to construct the number string
5221+
*
5222+
* @return formatted number as string
51915223
*/
5192-
static void
5193-
jerry_log_unsigned (unsigned int num, char *buffer_p)
5224+
static char *
5225+
jerry_format_unsigned (unsigned int num, uint8_t width, char padding, uint8_t radix, char *buffer_p)
51945226
{
5227+
static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
51955228
char *cursor_p = buffer_p + JERRY_LOG_BUFFER_SIZE;
51965229
*(--cursor_p) = '\0';
5230+
uint8_t count = 0;
51975231

5198-
while (num > 0)
5232+
do
51995233
{
5200-
*(--cursor_p) = (char) ((num % 10) + '0');
5201-
num /= 10;
5234+
*(--cursor_p) = digits[num % radix];
5235+
num /= radix;
5236+
count++;
5237+
} while (num > 0);
5238+
5239+
while (count < width)
5240+
{
5241+
*(--cursor_p) = padding;
5242+
count++;
52025243
}
52035244

5204-
jerry_log_string (cursor_p);
5205-
} /* jerry_log_unsigned */
5245+
return cursor_p;
5246+
} /* jerry_format_unsigned */
5247+
5248+
/**
5249+
* Format an integer number.
5250+
*
5251+
* @param num: number
5252+
* @param width: minimum width of the number
5253+
* @param padding: padding to be used when extending to minimum width
5254+
* @param buffer_p: buffer used to construct the number string
5255+
*
5256+
* @return formatted number as string
5257+
*/
5258+
static char *
5259+
jerry_format_int (int num, uint8_t width, char padding, char *buffer_p)
5260+
{
5261+
if (num >= 0)
5262+
{
5263+
return jerry_format_unsigned ((unsigned) num, width, padding, 10, buffer_p);
5264+
}
5265+
5266+
num = -num;
5267+
5268+
if (padding == '0' && width > 0)
5269+
{
5270+
char *cursor_p = jerry_format_unsigned ((unsigned) num, --width, padding, 10, buffer_p);
5271+
*(--cursor_p) = '-';
5272+
return cursor_p;
5273+
}
5274+
5275+
char *cursor_p = jerry_format_unsigned ((unsigned) num, 0, ' ', 10, buffer_p);
5276+
*(--cursor_p) = '-';
5277+
5278+
char *indent_p = buffer_p + JERRY_LOG_BUFFER_SIZE - width - 1;
5279+
5280+
while (cursor_p > indent_p)
5281+
{
5282+
*(--cursor_p) = ' ';
5283+
}
5284+
5285+
return cursor_p;
5286+
} /* jerry_foramt_int */
52065287

52075288
/**
52085289
* Log a zero-terminated formatted message with the specified log level.
52095290
*
52105291
* Supported format specifiers:
52115292
* %s: zero-terminated string
52125293
* %c: character
5213-
* %u: unsigned int
5294+
* %u: unsigned integer
5295+
* %d: decimal integer
5296+
* %x: unsigned hexadecimal
5297+
*
5298+
* Width and padding sub-modifiers are also supported.
52145299
*
52155300
* @param format_p: format string
52165301
* @param level: message log level
52175302
*/
52185303
void
52195304
jerry_log (jerry_log_level_t level, const char *format_p, ...)
52205305
{
5221-
if (level > JERRY_CONTEXT (log_level))
5306+
if (level > jerry_jrt_get_log_level ())
52225307
{
52235308
return;
52245309
}
@@ -5245,18 +5330,52 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52455330
}
52465331

52475332
++cursor_p;
5333+
uint8_t width = 0;
5334+
size_t precision = 0;
5335+
char padding = ' ';
5336+
5337+
if (*cursor_p == '0')
5338+
{
5339+
padding = '0';
5340+
cursor_p++;
5341+
}
5342+
5343+
if (lit_char_is_decimal_digit ((ecma_char_t) *cursor_p))
5344+
{
5345+
width = (uint8_t) (*cursor_p - '0');
5346+
cursor_p++;
5347+
}
5348+
else if (*cursor_p == '*')
5349+
{
5350+
width = (uint8_t) JERRY_MAX (va_arg (vl, int), 10);
5351+
cursor_p++;
5352+
}
5353+
else if (*cursor_p == '.' && *(cursor_p + 1) == '*')
5354+
{
5355+
precision = (size_t) va_arg (vl, int);
5356+
cursor_p += 2;
5357+
}
52485358

52495359
if (*cursor_p == '\0')
52505360
{
52515361
buffer_p[buffer_index++] = '%';
52525362
break;
52535363
}
52545364

5365+
/* The buffer is always flushed before a substitution, and can be reused to for formatting. */
52555366
switch (*cursor_p++)
52565367
{
52575368
case 's':
52585369
{
5259-
jerry_log_string (va_arg (vl, char *));
5370+
char *str_p = va_arg (vl, char *);
5371+
5372+
if (precision == 0)
5373+
{
5374+
jerry_log_string (str_p);
5375+
break;
5376+
}
5377+
5378+
jerry_log_string_fixed (str_p, precision, buffer_p);
52605379
break;
52615380
}
52625381
case 'c':
@@ -5265,10 +5384,19 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
52655384
buffer_p[buffer_index++] = (char) va_arg (vl, int);
52665385
break;
52675386
}
5387+
case 'd':
5388+
{
5389+
jerry_log_string (jerry_format_int (va_arg (vl, int), width, padding, buffer_p));
5390+
break;
5391+
}
52685392
case 'u':
52695393
{
5270-
/* The buffer is always flushed before a substitution, and can be reused to print the number. */
5271-
jerry_log_unsigned (va_arg (vl, unsigned int), buffer_p);
5394+
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 10, buffer_p));
5395+
break;
5396+
}
5397+
case 'x':
5398+
{
5399+
jerry_log_string (jerry_format_unsigned (va_arg (vl, unsigned int), width, padding, 16, buffer_p));
52725400
break;
52735401
}
52745402
default:

jerry-core/jcontext/jcontext.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ struct jerry_context_t
169169
uint32_t lit_magic_string_ex_count; /**< external magic strings count */
170170
uint32_t jerry_init_flags; /**< run-time configuration flags */
171171
uint32_t status_flags; /**< run-time flags (the top 8 bits are used for passing class parsing options) */
172-
jerry_log_level_t log_level; /**< current log level */
173172

174173
#if (JERRY_GC_MARK_LIMIT != 0)
175174
uint32_t ecma_gc_mark_recursion_limit; /**< GC mark recursion limit */

jerry-core/jmem/jmem-heap.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -738,32 +738,21 @@ jmem_heap_stats_print (void)
738738

739739
JERRY_DEBUG_MSG ("Heap stats:\n");
740740
#if !JERRY_SYSTEM_ALLOCATOR
741-
JERRY_DEBUG_MSG (" Heap size = %zu bytes\n", heap_stats->size);
741+
JERRY_DEBUG_MSG (" Heap size = %u bytes\n", (unsigned) heap_stats->size);
742742
#endif /* !JERRY_SYSTEM_ALLOCATOR */
743-
JERRY_DEBUG_MSG (" Allocated = %zu bytes\n"
744-
" Peak allocated = %zu bytes\n"
745-
" Waste = %zu bytes\n"
746-
" Peak waste = %zu bytes\n"
747-
" Allocated byte code data = %zu bytes\n"
748-
" Peak allocated byte code data = %zu bytes\n"
749-
" Allocated string data = %zu bytes\n"
750-
" Peak allocated string data = %zu bytes\n"
751-
" Allocated object data = %zu bytes\n"
752-
" Peak allocated object data = %zu bytes\n"
753-
" Allocated property data = %zu bytes\n"
754-
" Peak allocated property data = %zu bytes\n",
755-
heap_stats->allocated_bytes,
756-
heap_stats->peak_allocated_bytes,
757-
heap_stats->waste_bytes,
758-
heap_stats->peak_waste_bytes,
759-
heap_stats->byte_code_bytes,
760-
heap_stats->peak_byte_code_bytes,
761-
heap_stats->string_bytes,
762-
heap_stats->peak_string_bytes,
763-
heap_stats->object_bytes,
764-
heap_stats->peak_object_bytes,
765-
heap_stats->property_bytes,
766-
heap_stats->peak_property_bytes);
743+
744+
JERRY_DEBUG_MSG (" Allocated = %u bytes\n", (unsigned) heap_stats->allocated_bytes);
745+
JERRY_DEBUG_MSG (" Peak allocated = %u bytes\n", (unsigned) heap_stats->peak_allocated_bytes);
746+
JERRY_DEBUG_MSG (" Waste = %u bytes\n", (unsigned) heap_stats->waste_bytes);
747+
JERRY_DEBUG_MSG (" Peak waste = %u bytes\n", (unsigned) heap_stats->peak_waste_bytes);
748+
JERRY_DEBUG_MSG (" Allocated byte code data = %u bytes\n", (unsigned) heap_stats->byte_code_bytes);
749+
JERRY_DEBUG_MSG (" Peak allocated byte code data = %u bytes\n", (unsigned) heap_stats->peak_byte_code_bytes);
750+
JERRY_DEBUG_MSG (" Allocated string data = %u bytes\n", (unsigned) heap_stats->string_bytes);
751+
JERRY_DEBUG_MSG (" Peak allocated string data = %u bytes\n", (unsigned) heap_stats->peak_string_bytes);
752+
JERRY_DEBUG_MSG (" Allocated object data = %u bytes\n", (unsigned) heap_stats->object_bytes);
753+
JERRY_DEBUG_MSG (" Peak allocated object data = %u bytes\n", (unsigned) heap_stats->peak_object_bytes);
754+
JERRY_DEBUG_MSG (" Allocated property data = %u bytes\n", (unsigned) heap_stats->property_bytes);
755+
JERRY_DEBUG_MSG (" Peak allocated property data = %u bytes\n", (unsigned) heap_stats->peak_property_bytes);
767756
} /* jmem_heap_stats_print */
768757

769758
/**

jerry-core/jrt/jrt-logging.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include <jrt.h>
17+
18+
static jerry_log_level_t jerry_log_level = JERRY_LOG_LEVEL_ERROR;
19+
20+
/**
21+
* Get current log level
22+
*
23+
* @return log level
24+
*/
25+
jerry_log_level_t
26+
jerry_jrt_get_log_level (void)
27+
{
28+
return jerry_log_level;
29+
} /* jerry_jrt_get_log_level */
30+
31+
/**
32+
* Set log level
33+
*
34+
* @param level: new log level
35+
*/
36+
void
37+
jerry_jrt_set_log_level (jerry_log_level_t level)
38+
{
39+
jerry_log_level = level;
40+
} /* jerry_jrt_set_log_level */

jerry-core/jrt/jrt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ void JERRY_ATTR_NORETURN jerry_unreachable (const char *file, const char *functi
117117
*/
118118
void JERRY_ATTR_NORETURN jerry_fatal (jerry_fatal_code_t code);
119119

120+
jerry_log_level_t jerry_jrt_get_log_level (void);
121+
void jerry_jrt_set_log_level (jerry_log_level_t level);
122+
120123
/*
121124
* Logging
122125
*/

0 commit comments

Comments
 (0)