Ejemplo n.º 1
0
static gpointer
log_thread (gpointer data)
{
  GDataInputStream *data_stream = data;
  gboolean istty;
  gboolean iserror;
  gboolean closing = FALSE;
  gchar *line;
  gsize len;

  g_assert (G_IS_DATA_INPUT_STREAM (data_stream));

  iserror = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data_stream), "IS_STDERR"));
  istty = isatty (iserror ? STDERR_FILENO : STDOUT_FILENO);

again:

  while ((line = g_data_input_stream_read_line_utf8 (data_stream, &len, NULL, NULL)))
    {
      /*
       * TODO: I'd like to have time information here too.
       */

      if (iserror)
        {
          if (istty)
            {
              /* TODO: color red */
              g_printerr ("%s\n", line);
            }
          else
            {
              g_printerr ("%s\n", line);
            }
        }
      else
        {
          g_print ("%s\n", line);
        }
      g_free (line);
    }

  if (!closing)
    {
      if (g_atomic_int_get (&build_done))
        closing = TRUE;

      /* one final attempt to flush the logs */
      g_usleep (G_USEC_PER_SEC / 20);
      goto again;
    }

  return NULL;
}
Ejemplo n.º 2
0
char *
g_data_input_stream_read_upto (GDataInputStream  *stream,
                               const gchar       *stop_chars,
                               gssize             stop_chars_len,
                               gsize             *length,
                               GCancellable      *cancellable,
                               GError           **error)
{
  GBufferedInputStream *bstream;
  gsize checked;
  gssize found_pos;
  gssize res;
  char *data_until;

  g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);

  if (stop_chars_len < 0)
    stop_chars_len = strlen (stop_chars);

  bstream = G_BUFFERED_INPUT_STREAM (stream);

  checked = 0;

  while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
    {
      if (g_buffered_input_stream_get_available (bstream) ==
          g_buffered_input_stream_get_buffer_size (bstream))
        g_buffered_input_stream_set_buffer_size (bstream,
                                                 2 * g_buffered_input_stream_get_buffer_size (bstream));

      res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
      if (res < 0)
        return NULL;
      if (res == 0)
        {
          /* End of stream */
          if (g_buffered_input_stream_get_available (bstream) == 0)
            {
              if (length)
                *length = 0;
              return NULL;
            }
          else
            {
              found_pos = checked;
              break;
            }
        }
    }

  data_until = g_malloc (found_pos + 1);

  res = g_input_stream_read (G_INPUT_STREAM (stream),
                             data_until,
                             found_pos,
                             NULL, NULL);
  if (length)
    *length = (gsize)found_pos;
  g_warn_if_fail (res == found_pos);
  data_until[found_pos] = 0;

  return data_until;
}