Esempio n. 1
0
static guint8
fix_expected_colour (guint32 col_mask, guint8 col_expected)
{
  guint32 mask;
  gint last = g_bit_nth_msf (col_mask, -1);
  gint first = g_bit_nth_lsf (col_mask, -1);

  mask = 1 << (last - first + 1);
  mask -= 1;

  g_assert (col_expected == 0x00 || col_expected == 0xff);

  /* this only works because we only check for all-bits-set or no-bits-set */
  return col_expected & mask;
}
Esempio n. 2
0
/**
 * dfl_time_sequence_append:
 * @sequence: a #DflTimeSequence
 * @timestamp: TODO
 *
 * TODO
 *
 * Note that the pointers returned from dfl_time_sequence_append() may change
 * if dfl_time_sequence_append() is called again, due to internal reallocations.
 * The returned pointer is only valid until the next call to
 * dfl_time_sequence_append().
 *
 * Returns: (transfer none): TODO
 * Since: 0.1.0
 */
gpointer
dfl_time_sequence_append (DflTimeSequence *sequence,
                          DflTimestamp     timestamp)
{
  DflTimeSequenceReal *self = (DflTimeSequenceReal *) sequence;
  DflTimestamp last_timestamp;
  gpointer last_element;
  DflTimeSequenceElement *element;

  g_return_val_if_fail (sequence != NULL, NULL);
  g_return_val_if_fail (self->n_elements_valid < G_MAXSIZE, NULL);

  /* Check @timestamp is monotonically increasing. */
  last_element = dfl_time_sequence_get_last_element (sequence, &last_timestamp);
  g_return_val_if_fail (last_element == NULL || timestamp >= last_timestamp,
                       NULL);

  /* Do we need to expand the array first? */
  if (self->n_elements_valid == self->n_elements_allocated)
    {
      self->n_elements_allocated =
        ((gsize) 1 << (g_bit_nth_msf (self->n_elements_allocated, -1) + 1));
      self->elements = g_realloc_n (self->elements, self->n_elements_allocated,
                                    sizeof (DflTimeSequenceElement) +
                                    self->element_size);
    }

  g_assert (self->n_elements_allocated > self->n_elements_valid);

  /* Append the new element. */
  self->n_elements_valid++;

  element = dfl_time_sequence_index (sequence, self->n_elements_valid - 1);
  element->timestamp = timestamp;

  return element->data;
}
Esempio n. 3
0
void
g_logv (const gchar   *log_domain,
	GLogLevelFlags log_level,
	const gchar   *format,
	va_list	       args1)
{
  gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
  gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
  gint i;

  log_level &= G_LOG_LEVEL_MASK;
  if (!log_level)
    return;

  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
    {
      register GLogLevelFlags test_level;

      test_level = 1 << i;
      if (log_level & test_level)
	{
	  guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
	  GLogDomain *domain;
	  GLogFunc log_func;
	  GLogLevelFlags domain_fatal_mask;
	  gpointer data = NULL;
          gboolean masquerade_fatal = FALSE;

	  if (was_fatal)
	    test_level |= G_LOG_FLAG_FATAL;
	  if (was_recursion)
	    test_level |= G_LOG_FLAG_RECURSION;

	  /* check recursion and lookup handler */
	  g_mutex_lock (g_messages_lock);
	  domain = g_log_find_domain_L (log_domain ? log_domain : "");
	  if (depth)
	    test_level |= G_LOG_FLAG_RECURSION;
	  depth++;
	  domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
	  if ((domain_fatal_mask | g_log_always_fatal) & test_level)
	    test_level |= G_LOG_FLAG_FATAL;
	  if (test_level & G_LOG_FLAG_RECURSION)
	    log_func = _g_log_fallback_handler;
	  else
	    log_func = g_log_domain_get_handler_L (domain, test_level, &data);
	  domain = NULL;
	  g_mutex_unlock (g_messages_lock);

	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));

	  /* had to defer debug initialization until we can keep track of recursion */
	  if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
	    {
	      GLogLevelFlags orig_test_level = test_level;

	      _g_debug_init ();
	      if ((domain_fatal_mask | g_log_always_fatal) & test_level)
		test_level |= G_LOG_FLAG_FATAL;
	      if (test_level != orig_test_level)
		{
		  /* need a relookup, not nice, but not too bad either */
		  g_mutex_lock (g_messages_lock);
		  domain = g_log_find_domain_L (log_domain ? log_domain : "");
		  log_func = g_log_domain_get_handler_L (domain, test_level, &data);
		  domain = NULL;
		  g_mutex_unlock (g_messages_lock);
		}
	    }

	  if (test_level & G_LOG_FLAG_RECURSION)
	    {
	      /* we use a stack buffer of fixed size, since we're likely
	       * in an out-of-memory situation
	       */
	      gchar buffer[1025];
              gsize size;
              va_list args2;

              G_VA_COPY (args2, args1);
	      size = _g_vsnprintf (buffer, 1024, format, args2);
              va_end (args2);

	      log_func (log_domain, test_level, buffer, data);
	    }
	  else
	    {
	      gchar *msg;
              va_list args2;

              G_VA_COPY (args2, args1);
              msg = g_strdup_vprintf (format, args2);
              va_end (args2);

	      log_func (log_domain, test_level, msg, data);

              if ((test_level & G_LOG_FLAG_FATAL)
                && !(test_level & G_LOG_LEVEL_ERROR))
                {
                  masquerade_fatal = fatal_log_func
                    && !fatal_log_func (log_domain, test_level, msg, data);
                }

	      g_free (msg);
	    }

	 gchar *locale_msg2 = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);

              MessageBox (NULL, locale_msg2, L"G_LOGV",
                          MB_ICONERROR|MB_SETFOREGROUND);

	  if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
            {
#ifdef G_OS_WIN32
	      gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
	      
	      MessageBox (NULL, locale_msg, NULL,
			  MB_ICONERROR|MB_SETFOREGROUND);
#ifndef G_PLATFORM_WIN32_CE
	      if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
		G_BREAKPOINT ();
	      else
		abort ();
#else
		abort ();
#endif


#else
#if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
	      if (!(test_level & G_LOG_FLAG_RECURSION))
		G_BREAKPOINT ();
	      else
		abort ();
#else /* !G_ENABLE_DEBUG || !SIGTRAP */
	      abort ();
#endif /* !G_ENABLE_DEBUG || !SIGTRAP */
#endif /* !G_OS_WIN32 */
	    }
	  
	  depth--;
	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
	}
    }
}
Esempio n. 4
0
void error_redirect (const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
  gboolean in_recursion;
  gboolean is_fatal;
  char buf[256];

  in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
  is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
  log_level = (GLogLevelFlags) (log_level & G_LOG_LEVEL_MASK);

  if (!message)
    message = "(0) message";

  if (domain)
    strcpy (buf, domain);
  else
    strcpy (buf, "**");
  strcat (buf, "-");

  switch (log_level)
  {
  case G_LOG_LEVEL_ERROR:
    if (in_recursion)
      strcat (buf, "ERROR (recursed) **: ");
    else
      strcat (buf, "ERROR **: ");
    break;
  case G_LOG_LEVEL_CRITICAL:
    if (in_recursion)
      strcat (buf, "CRITICAL (recursed) **: ");
    else
      strcat (buf, "CRITICAL **: ");
    break;
  case G_LOG_LEVEL_WARNING:
    if (in_recursion)
      strcat (buf, "WARNING (recursed) **: ");
    else
      strcat (buf, "WARNING **: ");
    break;
  case G_LOG_LEVEL_MESSAGE:
    if (in_recursion)
      strcat (buf, "Message (recursed): ");
    else
      strcat (buf, "Message: ");
    break;
  case G_LOG_LEVEL_INFO:
    if (in_recursion)
      strcat (buf, "INFO (recursed): ");
    else
      strcat (buf, "INFO: ");
    break;
  case G_LOG_LEVEL_DEBUG:
    if (in_recursion)
      strcat (buf, "DEBUG (recursed): ");
    else
      strcat (buf, "DEBUG: ");
    break;
  default:
    /* we are used for a log level that is not defined by GLib itself,
     * try to make the best out of it.
     */
    if (in_recursion)
      strcat (buf, "LOG (recursed:");
    else
      strcat (buf, "LOG (");
    if (log_level)
    {
      gchar string[] = "0x00): ";
      gchar *p = string + 2;
      guint i;

      i = g_bit_nth_msf (log_level, -1);
      *p = i >> 4;
      p++;
      *p = '0' + (i & 0xf);
      if (*p > '9')
        *p += 'A' - '9' - 1;

      strcat (buf, string);
    } else
      strcat (buf, "): ");
  }
Esempio n. 5
0
gboolean
ews_oab_decompress_patch (const gchar *filename, const gchar *orig_filename,
			  const gchar *output_filename, GError **error)
{
	LzxPatchHeader *lzx_h = NULL;
	guint total_decomp_size = 0;
	FILE *input = NULL, *output = NULL, *orig_input = NULL;
	gboolean ret = TRUE;
	GError *err = NULL;

	input = fopen (filename, "rb");
	if (!input) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the input file");
		ret = FALSE;
		goto exit;
	}

	orig_input = fopen (orig_filename, "rb");
	if (!orig_input) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the reference input file");
		ret = FALSE;
		goto exit;
	}

	output = fopen (output_filename, "wb");
	if (!output) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the output file");
		ret = FALSE;
		goto exit;
	}

	lzx_h = read_patch_headers (input, &err);
	if (!lzx_h) {
		ret = FALSE;
		goto exit;
	}

	/* TODO decompressing multiple lzx_blocks has not been tested yet. Will need to get a setup and test it. */
	do {
		LzxPatchBlockHeader *lzx_b;
		struct lzxd_stream *lzs;
		goffset offset;
		guint ref_size, window_bits;

		lzx_b = read_patch_block_header (input, &err);
		if (err) {
			printf("err block header\n");
			ret = FALSE;
			goto exit;
		}

		/* note the file offset */
		offset = ftell(input);

		/* The window size should be the smallest power of two
		   between 2^17 and 2^25 that is greater than or equal
		   to the sum of the size of the reference data
		   rounded up to a multiple of 32768 and the size of
		   the subject data. */
		ref_size = (lzx_b->source_size + 32767) & ~32767;
		window_bits = g_bit_nth_msf(ref_size + lzx_b->target_size - 1, -1) + 1;

		if (window_bits < 17)
			window_bits = 17;
		else if (window_bits > 25)
			window_bits = 25;

		lzs = ews_lzxd_init (input, output, window_bits,
				 0, 4096, lzx_b->target_size, 1);
		if (!lzs) {
			g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_init)");
			ret = FALSE;
			goto exit;
		}
		if (ews_lzxd_set_reference_data(lzs, orig_input, lzx_b->source_size)) {
			g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_set_reference_data)");
			ret = FALSE;
			goto exit;
		}
		if (ews_lzxd_decompress (lzs, lzs->length) != LZX_ERR_OK) {
			g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_decompress)");
			ret = FALSE;
			goto exit;
		}

		/* Set the fp to beggining of next block. This is a HACK, looks like decompress reads beyond the block.
		 * Since we can identify the next block start from block header, we just reset the offset */
		offset += lzx_b->patch_size;
		fseek (input, offset, SEEK_SET);

		total_decomp_size += lzx_b->target_size;
		g_free (lzx_b);
	} while (total_decomp_size < lzx_h->target_size);

exit:
	if (input)
		fclose (input);

	if (orig_input)
		fclose (orig_input);

	if (output)
		fclose (output);

	if (err) {
		ret = FALSE;
		g_propagate_error (error, err);
		g_unlink (output_filename);
	}

	g_free (lzx_h);

	return ret;
}
Esempio n. 6
0
gboolean
ews_oab_decompress_full (const gchar *filename, const gchar *output_filename,
			 GError **error)
{
	LzxHeader *lzx_h = NULL;
	guint total_decomp_size = 0;
	FILE *input, *output = NULL;
	gboolean ret = TRUE;
	GError *err = NULL;

	input = fopen (filename, "rb");
	if (!input) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the input file");
		ret = FALSE;
		goto exit;
	}

	output = fopen (output_filename, "wb");
	if (!output) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the output file");
		ret = FALSE;
		goto exit;
	}

	lzx_h = read_headers (input, &err);
	if (!lzx_h) {
		ret = FALSE;
		goto exit;
	}

	/* TODO decompressing multiple lzx_blocks has not been tested yet. Will need to get a setup and test it. */
	do {
		LzxBlockHeader *lzx_b;
		struct lzxd_stream *lzs;
		goffset offset;

		lzx_b = read_block_header (input, &err);
		if (err) {
			ret = FALSE;
			goto exit;
		}

		/* note the file offset */
		offset = ftell (input);

		/* lzx_b points to 1, write it directly to file */
		if (lzx_b->flags == 0) {
			gchar *buffer = g_malloc0 (lzx_b->ucomp_size);

			if (!(fread (buffer, 1, lzx_b->ucomp_size, input) == lzx_b->ucomp_size &&
				fwrite (buffer, 1, lzx_b->ucomp_size, output) == lzx_b->ucomp_size)) {
				g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "failed to write data in output file");
				g_free (buffer);
				ret = FALSE;
				goto exit;
			}
			g_free (buffer);
		} else {
			/* The window size should be the smallest power of two between 2^17 and 2^25 that is
			   greater than or equal to the sum of the size of the reference data rounded up to
			   a multiple of 32768 and the size of the subject data. Since we have no reference
			   data, forget that and the rounding. Just the smallest power of two which is large
			   enough to cover the subject data (lzx_b->ucomp_size). */

			guint window_bits = g_bit_nth_msf(lzx_b->ucomp_size - 1, -1) + 1;

			if (window_bits < 17)
				window_bits = 17;
			else if (window_bits > 25)
				window_bits = 25;

			lzs = ews_lzxd_init (input, output, window_bits,
					 0, 4096, lzx_b->ucomp_size, 1);
			if (!lzs) {
				g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_init)");
				ret = FALSE;
				goto exit;
			}
			if (ews_lzxd_decompress (lzs, lzx_b->ucomp_size) != LZX_ERR_OK) {
				g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_decompress)");
				ret = FALSE;
				goto exit;
			}
		}

		/* Set the fp to beggining of next block. This is a HACK, looks like decompress reads beyond the block.
		 * Since we can identify the next block start from block header, we just reset the offset */
		offset += lzx_b->comp_size;
		fseek (input, offset, SEEK_SET);

		total_decomp_size += lzx_b->ucomp_size;
		g_free (lzx_b);
	} while (total_decomp_size < lzx_h->target_size);

exit:
	if (input)
		fclose (input);

	if (output)
		fclose (output);

	if (err) {
		ret = FALSE;
		g_propagate_error (error, err);
		g_unlink (output_filename);
	}

	g_free (lzx_h);

	return ret;
}
Esempio n. 7
0
/* This is essentially a copy of g_log_default_handler from
   gmessages.c in glib-1.2.
 */
void log_default_log_handler (const gchar    *log_domain,
                              GLogLevelFlags  log_level,
                              const gchar    *message,
                              gpointer        unused_data)
{
#ifdef NATIVE_WIN32
        FILE *fd;
#else
        gint fd;
#endif
        gboolean in_recursion;
        gboolean is_fatal;  

        if(!log_domain)
                switch(log_entity_flag) {
                case kLog_none: log_domain = ""; break;
                case kLog_client: log_domain = "client"; break;
                case kLog_server: log_domain = "  server"; break;
                default: log_domain = " ???";
                }
               
        unused_data = NULL;     /* unused: avoid compiler warnings */
        in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
        is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
        log_level &= G_LOG_LEVEL_MASK;
  
        if (!message)
                message = "g_log_default_handler(): (NULL) message";
  
#ifdef NATIVE_WIN32
        /* Use just stdout as stderr is hard to get redirected from the
         * DOS prompt.
         */
        fd = stdout;
#else
        fd = STDERR_FILENO;
#endif
  
        switch (log_level) {
        case G_LOG_LEVEL_ERROR:
                if (log_domain) {
                        write (fd, "\n", 1);
                        write (fd, log_domain, strlen (log_domain));
                        write (fd, "-", 1);
                } else
                        write (fd, "\n** ", 4);
                if (in_recursion)
                        write (fd, "ERROR (recursed) **: ", 21);
                else
                        write (fd, "ERROR **: ", 10);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        case G_LOG_LEVEL_CRITICAL:
                if (log_domain) {
                        write (fd, "\n", 1);
                        write (fd, log_domain, strlen (log_domain));
                        write (fd, "-", 1);
                } else
                        write (fd, "\n** ", 4);
                if (in_recursion)
                        write (fd, "CRITICAL (recursed) **: ", 24);
                else
                        write (fd, "CRITICAL **: ", 13);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        case G_LOG_LEVEL_WARNING:
                if (log_domain) {
                        write (fd, "\n", 1);
                        write (fd, log_domain, strlen (log_domain));
                        write (fd, "-", 1);
                } else
                        write (fd, "\n** ", 4);
                if (in_recursion)
                        write (fd, "WARNING (recursed) **: ", 23);
                else
                        write (fd, "WARNING **: ", 12);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        case G_LOG_LEVEL_MESSAGE:
                if (log_domain) {
                        write (fd, log_domain, strlen (log_domain));
                        write (fd, "-", 1);
                }
                if (in_recursion)
                        write (fd, "Message (recursed): ", 20);
                else
                        write (fd, "Message: ", 9);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        case G_LOG_LEVEL_INFO:
                if (log_domain) {
                        write (fd, log_domain, strlen (log_domain));
                        write (fd, "-", 1);
                }
                if (in_recursion)
                        write (fd, "INFO (recursed): ", 17);
                else
                        write (fd, "INFO: ", 6);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        case G_LOG_LEVEL_DEBUG:
                if (log_domain) {
                        write (fd, log_domain, strlen (log_domain));
                        write (fd, "-", 1);
                }
                if (in_recursion)
                        write (fd, "DEBUG (recursed): ", 18);
                else
                        write (fd, "DEBUG: ", 7);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        default:
                /* we are used for a log level that is not
                 * defined by GLib itself, try to make the
                 * best out of it.
                 */
                if (log_domain) {
                        write (fd, log_domain, strlen (log_domain));
                        if (in_recursion)
                                write (fd, "-LOG (recursed:", 15);
                        else
                                write (fd, "-LOG (", 6);
                } else if (in_recursion)
                        write (fd, "LOG (recursed:", 14);
                else
                        write (fd, "LOG (", 5);
                if (log_level) {
                        gchar string[] = "0x00): ";
                        gchar *p = string + 2;
                        guint i;
	  
                        i = g_bit_nth_msf (log_level, -1);
                        *p = i >> 4;
                        p++;
                        *p = '0' + (i & 0xf);
                        if (*p > '9')
                                *p += 'A' - '9' - 1;
	  
                        write (fd, string, 7);
                } else
                        write (fd, "): ", 3);
                write (fd, message, strlen(message));
                if (is_fatal)
                        write (fd, "\naborting...\n", 13);
                else
                        write (fd, "\n", 1);
                break;
        }
void
g_log_default_handler (const gchar    *log_domain,
		       GLogLevelFlags  log_level,
		       const gchar    *message,
		       gpointer	       unused_data)
{
#ifdef NATIVE_WIN32
  FILE *fd;
#else
  gint fd;
#endif
  gboolean in_recursion;
  gboolean is_fatal;  
  GErrorFunc     local_glib_error_func;
  GWarningFunc   local_glib_warning_func;
  GPrintFunc     local_glib_message_func;

  in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
  is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
  log_level &= G_LOG_LEVEL_MASK;
  
  if (!message)
    message = "g_log_default_handler(): (NULL) message";
  
#ifdef NATIVE_WIN32
  /* Use just stdout as stderr is hard to get redirected from the
   * DOS prompt.
   */
  fd = stdout;
#else
  fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
#endif
  
  g_mutex_lock (g_messages_lock);
  local_glib_error_func = glib_error_func;
  local_glib_warning_func = glib_warning_func;
  local_glib_message_func = glib_message_func;
  g_mutex_unlock (g_messages_lock);

  switch (log_level)
    {
    case G_LOG_LEVEL_ERROR:
      if (!log_domain && local_glib_error_func)
	{
	  /* compatibility code */
	  local_glib_error_func (message);  
	  return;
	}
      /* use write(2) for output, in case we are out of memeory */
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, "\n", 1);
	  write (fd, log_domain, strlen (log_domain));
	  write (fd, "-", 1);
	}
      else
	write (fd, "\n** ", 4);
      if (in_recursion)
	write (fd, "ERROR (recursed) **: ", 21);
      else
	write (fd, "ERROR **: ", 10);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    case G_LOG_LEVEL_CRITICAL:
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, "\n", 1);
	  write (fd, log_domain, strlen (log_domain));
	  write (fd, "-", 1);
	}
      else
	write (fd, "\n** ", 4);
      if (in_recursion)
	write (fd, "CRITICAL (recursed) **: ", 24);
      else
	write (fd, "CRITICAL **: ", 13);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    case G_LOG_LEVEL_WARNING:
      if (!log_domain && local_glib_warning_func)
	{
	  /* compatibility code */
	  local_glib_warning_func (message);
	  return;
	}
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, "\n", 1);
	  write (fd, log_domain, strlen (log_domain));
	  write (fd, "-", 1);
	}
      else
	write (fd, "\n** ", 4);
      if (in_recursion)
	write (fd, "WARNING (recursed) **: ", 23);
      else
	write (fd, "WARNING **: ", 12);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    case G_LOG_LEVEL_MESSAGE:
      if (!log_domain && local_glib_message_func)
	{
	  /* compatibility code */
	  local_glib_message_func (message);
	  return;
	}
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, log_domain, strlen (log_domain));
	  write (fd, "-", 1);
	}
      if (in_recursion)
	write (fd, "Message (recursed): ", 20);
      else
	write (fd, "Message: ", 9);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    case G_LOG_LEVEL_INFO:
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, log_domain, strlen (log_domain));
	  write (fd, "-", 1);
	}
      if (in_recursion)
	write (fd, "INFO (recursed): ", 17);
      else
	write (fd, "INFO: ", 6);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    case G_LOG_LEVEL_DEBUG:
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, log_domain, strlen (log_domain));
	  write (fd, "-", 1);
	}
      if (in_recursion)
	write (fd, "DEBUG (recursed): ", 18);
      else
	write (fd, "DEBUG: ", 7);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    default:
      /* we are used for a log level that is not defined by GLib itself,
       * try to make the best out of it.
       */
      ensure_stdout_valid ();
      if (log_domain)
	{
	  write (fd, log_domain, strlen (log_domain));
	  if (in_recursion)
	    write (fd, "-LOG (recursed:", 15);
	  else
	    write (fd, "-LOG (", 6);
	}
      else if (in_recursion)
	write (fd, "LOG (recursed:", 14);
      else
	write (fd, "LOG (", 5);
      if (log_level)
	{
	  gchar string[] = "0x00): ";
	  gchar *p = string + 2;
	  guint i;
	  
	  i = g_bit_nth_msf (log_level, -1);
	  *p = i >> 4;
	  p++;
	  *p = '0' + (i & 0xf);
	  if (*p > '9')
	    *p += 'A' - '9' - 1;
	  
	  write (fd, string, 7);
	}
      else
	write (fd, "): ", 3);
      write (fd, message, strlen(message));
      if (is_fatal)
	write (fd, "\naborting...\n", 13);
      else
	write (fd, "\n", 1);
      break;
    }
void
g_logv (const gchar    *log_domain,
	GLogLevelFlags	log_level,
	const gchar    *format,
	va_list	        args1)
{
  va_list args2;
  gchar buffer[1025];
  register gint i;
  
  log_level &= G_LOG_LEVEL_MASK;
  if (!log_level)
    return;
  
  /* we use a stack buffer of fixed size, because we might get called
   * recursively.
   */
  G_VA_COPY (args2, args1);
  if (g_printf_string_upper_bound (format, args1) < 1024)
    vsprintf (buffer, format, args2);
  else
    {
      /* since we might be out of memory, we can't use g_vsnprintf(). */
#ifdef  HAVE_VSNPRINTF
      vsnprintf (buffer, 1024, format, args2);
#else	/* !HAVE_VSNPRINTF */
      /* we are out of luck here */
      strncpy (buffer, format, 1024);
#endif	/* !HAVE_VSNPRINTF */
      buffer[1024] = 0;
    }
  va_end (args2);
  
  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
    {
      register GLogLevelFlags test_level;
      
      test_level = 1 << i;
      if (log_level & test_level)
	{
	  guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
	  GLogDomain *domain;
	  GLogFunc log_func;
	  gpointer data = NULL;
	  
	  domain = g_log_find_domain (log_domain ? log_domain : "");
	  
	  if (depth)
	    test_level |= G_LOG_FLAG_RECURSION;
	  
	  depth++;
	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));

	  g_mutex_lock (g_messages_lock);
	  if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) | 
		g_log_always_fatal) & test_level) != 0)
	    test_level |= G_LOG_FLAG_FATAL;  
	  g_mutex_unlock (g_messages_lock);

	  log_func = g_log_domain_get_handler (domain, test_level, &data);
	  log_func (log_domain, test_level, buffer, data);
	  
	  /* *domain can be cluttered now */
	  
	  if (test_level & G_LOG_FLAG_FATAL)
	    abort ();
	  
	  depth--;
	  g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
	}
    }
}
Esempio n. 10
0
static VALUE
rg_s_bit_nth_msf(G_GNUC_UNUSED VALUE self, VALUE mask, VALUE nth_bit)
{
    return INT2NUM(g_bit_nth_msf(NUM2ULONG(mask), NUM2INT(nth_bit)));
}
Esempio n. 11
0
static void gtk_error_redirect (const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
	gboolean in_recursion;
	StringOutputStream buf(256);

	in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
	const gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
	log_level = (GLogLevelFlags) (log_level & G_LOG_LEVEL_MASK);

#ifndef DEBUG
	if (log_level == G_LOG_LEVEL_DEBUG)
		return;
#endif

	if (!message)
		message = "(0) message";

	if (domain)
		buf << domain;
	else
		buf << "**";
	buf << "-";

	switch (log_level) {
	case G_LOG_LEVEL_ERROR:
		if (in_recursion)
			buf << "ERROR (recursed) **: ";
		else
			buf << "ERROR **: ";
		break;
	case G_LOG_LEVEL_CRITICAL:
		if (in_recursion)
			buf << "CRITICAL (recursed) **: ";
		else
			buf << "CRITICAL **: ";
		break;
	case G_LOG_LEVEL_WARNING:
		if (in_recursion)
			buf << "WARNING (recursed) **: ";
		else
			buf << "WARNING **: ";
		break;
	case G_LOG_LEVEL_MESSAGE:
		if (in_recursion)
			buf << "Message (recursed): ";
		else
			buf << "Message: ";
		break;
	case G_LOG_LEVEL_INFO:
		if (in_recursion)
			buf << "INFO (recursed): ";
		else
			buf << "INFO: ";
		break;
	case G_LOG_LEVEL_DEBUG:
		if (in_recursion)
			buf << "DEBUG (recursed): ";
		else
			buf << "DEBUG: ";
		break;
	default:
		/* we are used for a log level that is not defined by GLib itself,
		 * try to make the best out of it. */
		if (in_recursion)
			buf << "LOG (recursed:";
		else
			buf << "LOG (";
		if (log_level) {
			gchar string[] = "0x00): ";
			gchar *p = string + 2;
			const guint i = g_bit_nth_msf(log_level, -1);
			*p++ = i >> 4;
			*p = '0' + (i & 0xf);
			if (*p > '9')
				*p += 'A' - '9' - 1;

			buf << string;
		} else
			buf << "): ";
	}