1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include <stdarg.h> 17425bb815Sopenharmony_ci#include <stdlib.h> 18425bb815Sopenharmony_ci#include <string.h> 19425bb815Sopenharmony_ci 20425bb815Sopenharmony_ci#include "jerryscript-port.h" 21425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 22425bb815Sopenharmony_ci#include "jerryscript-debugger.h" 23425bb815Sopenharmony_ci 24425bb815Sopenharmony_ci#ifndef DISABLE_EXTRA_API 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_ci/** 27425bb815Sopenharmony_ci * Actual log level 28425bb815Sopenharmony_ci */ 29425bb815Sopenharmony_cistatic jerry_log_level_t jerry_port_default_log_level = JERRY_LOG_LEVEL_ERROR; 30425bb815Sopenharmony_ci 31425bb815Sopenharmony_ci#define JERRY_PORT_DEFAULT_LOG_LEVEL jerry_port_default_log_level 32425bb815Sopenharmony_ci 33425bb815Sopenharmony_ci/** 34425bb815Sopenharmony_ci * Get the log level 35425bb815Sopenharmony_ci * 36425bb815Sopenharmony_ci * @return current log level 37425bb815Sopenharmony_ci * 38425bb815Sopenharmony_ci * Note: 39425bb815Sopenharmony_ci * This function is only available if the port implementation library is 40425bb815Sopenharmony_ci * compiled without the DISABLE_EXTRA_API macro. 41425bb815Sopenharmony_ci */ 42425bb815Sopenharmony_cijerry_log_level_t 43425bb815Sopenharmony_cijerry_port_default_get_log_level (void) 44425bb815Sopenharmony_ci{ 45425bb815Sopenharmony_ci return jerry_port_default_log_level; 46425bb815Sopenharmony_ci} /* jerry_port_default_get_log_level */ 47425bb815Sopenharmony_ci 48425bb815Sopenharmony_ci/** 49425bb815Sopenharmony_ci * Set the log level 50425bb815Sopenharmony_ci * 51425bb815Sopenharmony_ci * Note: 52425bb815Sopenharmony_ci * This function is only available if the port implementation library is 53425bb815Sopenharmony_ci * compiled without the DISABLE_EXTRA_API macro. 54425bb815Sopenharmony_ci */ 55425bb815Sopenharmony_civoid 56425bb815Sopenharmony_cijerry_port_default_set_log_level (jerry_log_level_t level) /**< log level */ 57425bb815Sopenharmony_ci{ 58425bb815Sopenharmony_ci jerry_port_default_log_level = level; 59425bb815Sopenharmony_ci} /* jerry_port_default_set_log_level */ 60425bb815Sopenharmony_ci 61425bb815Sopenharmony_ci#else /* DISABLE_EXTRA_API */ 62425bb815Sopenharmony_ci#define JERRY_PORT_DEFAULT_LOG_LEVEL JERRY_LOG_LEVEL_ERROR 63425bb815Sopenharmony_ci#endif /* !DISABLE_EXTRA_API */ 64425bb815Sopenharmony_ci 65425bb815Sopenharmony_ci/** 66425bb815Sopenharmony_ci * Default implementation of jerry_port_log. Prints log message to the standard 67425bb815Sopenharmony_ci * error with 'vfprintf' if message log level is less than or equal to the 68425bb815Sopenharmony_ci * current log level. 69425bb815Sopenharmony_ci * 70425bb815Sopenharmony_ci * If debugger support is enabled, printing happens first to an in-memory buffer, 71425bb815Sopenharmony_ci * which is then sent both to the standard error and to the debugger client. 72425bb815Sopenharmony_ci * 73425bb815Sopenharmony_ci * Note: 74425bb815Sopenharmony_ci * Changing the log level from JERRY_LOG_LEVEL_ERROR is only possible if 75425bb815Sopenharmony_ci * the port implementation library is compiled without the 76425bb815Sopenharmony_ci * DISABLE_EXTRA_API macro. 77425bb815Sopenharmony_ci */ 78425bb815Sopenharmony_civoid 79425bb815Sopenharmony_cijerry_port_log (jerry_log_level_t level, /**< message log level */ 80425bb815Sopenharmony_ci const char *format, /**< format string */ 81425bb815Sopenharmony_ci ...) /**< parameters */ 82425bb815Sopenharmony_ci{ 83425bb815Sopenharmony_ci if (level <= JERRY_PORT_DEFAULT_LOG_LEVEL) 84425bb815Sopenharmony_ci { 85425bb815Sopenharmony_ci va_list args; 86425bb815Sopenharmony_ci va_start (args, format); 87425bb815Sopenharmony_ci#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) 88425bb815Sopenharmony_ci if (jerry_debugger_is_connected()) 89425bb815Sopenharmony_ci { 90425bb815Sopenharmony_ci int length = vsnprintf (NULL, 0, format, args); 91425bb815Sopenharmony_ci va_end (args); 92425bb815Sopenharmony_ci va_start (args, format); 93425bb815Sopenharmony_ci 94425bb815Sopenharmony_ci JERRY_VLA (char, buffer, length + 1); 95425bb815Sopenharmony_ci vsnprintf (buffer, (size_t) length + 1, format, args); 96425bb815Sopenharmony_ci 97425bb815Sopenharmony_ci fprintf (stderr, "[JERRYSCRIPT]%s", buffer); 98425bb815Sopenharmony_ci jerry_debugger_send_log (level, (jerry_char_t *) buffer, (jerry_size_t) length); 99425bb815Sopenharmony_ci } 100425bb815Sopenharmony_ci else 101425bb815Sopenharmony_ci { 102425bb815Sopenharmony_ci vfprintf (stderr, format, args); 103425bb815Sopenharmony_ci } 104425bb815Sopenharmony_ci#else /* If jerry-debugger isn't defined, libc is turned on */ 105425bb815Sopenharmony_ci vfprintf (stderr, format, args); 106425bb815Sopenharmony_ci#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */ 107425bb815Sopenharmony_ci va_end (args); 108425bb815Sopenharmony_ci } 109425bb815Sopenharmony_ci} /* jerry_port_log */ 110425bb815Sopenharmony_ci 111425bb815Sopenharmony_ci#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) 112425bb815Sopenharmony_ci 113425bb815Sopenharmony_ci#define DEBUG_BUFFER_SIZE (256) 114425bb815Sopenharmony_cistatic char debug_buffer[DEBUG_BUFFER_SIZE]; 115425bb815Sopenharmony_cistatic int debug_buffer_index = 0; 116425bb815Sopenharmony_ci 117425bb815Sopenharmony_ci#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */ 118425bb815Sopenharmony_ci 119425bb815Sopenharmony_ci/** 120425bb815Sopenharmony_ci * Default implementation of jerry_port_print_char. Uses 'putchar' to 121425bb815Sopenharmony_ci * print a single character to standard output. 122425bb815Sopenharmony_ci */ 123425bb815Sopenharmony_civoid 124425bb815Sopenharmony_cijerry_port_print_char (char c) /**< the character to print */ 125425bb815Sopenharmony_ci{ 126425bb815Sopenharmony_ci putchar (c); 127425bb815Sopenharmony_ci 128425bb815Sopenharmony_ci#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) 129425bb815Sopenharmony_ci debug_buffer[debug_buffer_index++] = c; 130425bb815Sopenharmony_ci 131425bb815Sopenharmony_ci if ((debug_buffer_index == DEBUG_BUFFER_SIZE) || (c == '\n')) 132425bb815Sopenharmony_ci { 133425bb815Sopenharmony_ci jerry_debugger_send_output ((jerry_char_t *) debug_buffer, (jerry_size_t) debug_buffer_index); 134425bb815Sopenharmony_ci debug_buffer_index = 0; 135425bb815Sopenharmony_ci } 136425bb815Sopenharmony_ci#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */ 137425bb815Sopenharmony_ci} /* jerry_port_print_char */ 138