コード例 #1
0
ファイル: default-io.c プロジェクト: drashti304/TizenRT
/**
 * Default implementation of jerry_port_log. Prints log message to a buffer
 * then prints the buffer to the standard error with 'fprintf' if message
 * level is less than or equal to the set log level.
 * Additionally, sends the message to the debugger client.
 *
 * Note:
 *      Changing the log level from JERRY_LOG_LEVEL_ERROR is only possible if
 *      the port implementation library is compiled without the
 *      DISABLE_EXTRA_API macro.
 */
void
jerry_port_log (jerry_log_level_t level, /**< log level */
                const char *format, /**< format string */
                ...)  /**< parameters */
{
  if (level <= JERRY_PORT_DEFAULT_LOG_LEVEL)
  {
    va_list args;
    va_start (args, format);
#ifdef JERRY_DEBUGGER
    char buffer[256];
    int length = 0;
    length = vsnprintf (buffer, 255, format, args);
    buffer[length] = '\0';
    fprintf (stderr, "%s", buffer);
    jerry_char_t *jbuffer = (jerry_char_t *) buffer;
    jerry_debugger_send_output (jbuffer, (jerry_size_t) length, (uint8_t) (level + 2));
#else /* If jerry-debugger isn't defined, libc is turned on */
    vfprintf (stderr, format, args);
#endif /* JERRY_DEBUGGER */
    va_end (args);
  }
} /* jerry_port_log */
コード例 #2
0
/**
 * Provide a 'print' implementation for scripts.
 *
 * The routine converts all of its arguments to strings and outputs them
 * char-by-char using jerryx_port_handler_print_char.
 *
 * The NUL character is output as "\u0000", other characters are output
 * bytewise.
 *
 * Note:
 *      This implementation does not use standard C `printf` to print its
 *      output. This allows more flexibility but also extends the core
 *      JerryScript engine port API. Applications that want to use
 *      `jerryx_handler_print` must ensure that their port implementation also
 *      provides `jerryx_port_handler_print_char`.
 *
 * @return undefined - if all arguments could be converted to strings,
 *         error - otherwise.
 */
jerry_value_t
jerryx_handler_print (const jerry_value_t func_obj_val, /**< function object */
                      const jerry_value_t this_p, /**< this arg */
                      const jerry_value_t args_p[], /**< function arguments */
                      const jerry_length_t args_cnt) /**< number of function arguments */
{
  (void) func_obj_val; /* unused */
  (void) this_p; /* unused */

  static const char *null_str = "\\u0000";

  jerry_value_t ret_val = jerry_create_undefined ();

  for (jerry_length_t arg_index = 0;
       jerry_value_is_undefined (ret_val) && arg_index < args_cnt;
       arg_index++)
  {
    jerry_value_t str_val = jerry_value_to_string (args_p[arg_index]);

    if (!jerry_value_is_error (str_val))
    {
      if (arg_index != 0)
      {
        jerryx_port_handler_print_char (' ');
      }

      jerry_size_t substr_size;
      jerry_length_t substr_pos = 0;
      jerry_char_t substr_buf[256];

      while ((substr_size = jerry_substring_to_char_buffer (str_val,
                                                            substr_pos,
                                                            substr_pos + 256,
                                                            substr_buf,
                                                            256)) != 0)
      {
#ifdef JERRY_DEBUGGER
        jerry_debugger_send_output (substr_buf, substr_size, JERRY_DEBUGGER_OUTPUT_OK);
#endif /* JERRY_DEBUGGER */
        for (jerry_size_t chr_index = 0; chr_index < substr_size; chr_index++)
        {
          char chr = (char) substr_buf[chr_index];
          if (chr == '\0')
          {
            for (jerry_size_t null_index = 0; null_str[null_index] != 0; null_index++)
            {
              jerryx_port_handler_print_char (null_str[null_index]);
            }
          }
          else
          {
            jerryx_port_handler_print_char (chr);
          }
        }

        substr_pos += substr_size;
      }

      jerry_release_value (str_val);
    }
    else
    {
      ret_val = str_val;
    }
  }

  jerryx_port_handler_print_char ('\n');

  return ret_val;
} /* jerryx_handler_print */