~noskcaj/ubuntu/trusty/cogl/1.16.2

« back to all changes in this revision

Viewing changes to deps/glib/gmessages.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha, Jeremy Bicha, Rico Tzschichholz
  • Date: 2013-02-26 16:43:25 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130226164325-t4z9rylpa20v0p6q
Tags: 1.13.4-0ubuntu1
[ Jeremy Bicha ]
* New upstream release
  - soname bump
* debian/control.in:
  - Bump minimum glib to 2.32
  - Drop obsolete breaks/replaces
  - Bump libclutter-1.0-dev breaks for soname transition
* debian/libcogl-dev.install:
  - Add some missing files

[ Rico Tzschichholz ]
* debian/control.in:
  - Build-depend on libxrandr-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GLIB - Library of useful routines for C programming
 
2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
/*
 
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 
22
 * file for a list of people on the GLib Team.  See the ChangeLog
 
23
 * files for a list of changes.  These files are distributed with
 
24
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
 
25
 */
 
26
 
 
27
/*
 
28
 * MT safe
 
29
 */
 
30
 
 
31
/**
 
32
 * SECTION:warnings
 
33
 * @Title: Message Output and Debugging Functions
 
34
 * @Short_description: functions to output messages and help debug applications
 
35
 *
 
36
 * These functions provide support for outputting messages.
 
37
 *
 
38
 * The <function>g_return</function> family of macros (g_return_if_fail(),
 
39
 * g_return_val_if_fail(), g_return_if_reached(), g_return_val_if_reached())
 
40
 * should only be used for programming errors, a typical use case is
 
41
 * checking for invalid parameters at the beginning of a public function.
 
42
 * They should not be used if you just mean "if (error) return", they
 
43
 * should only be used if you mean "if (bug in program) return".
 
44
 * The program behavior is generally considered undefined after one
 
45
 * of these checks fails. They are not intended for normal control
 
46
 * flow, only to give a perhaps-helpful warning before giving up.
 
47
 */
 
48
 
 
49
#include "config.h"
 
50
 
 
51
#include <stdlib.h>
 
52
#include <stdarg.h>
 
53
#include <stdio.h>
 
54
#include <string.h>
 
55
#ifdef HAVE_UNISTD_H
 
56
#include <unistd.h>
 
57
#endif
 
58
#include <signal.h>
 
59
#include <locale.h>
 
60
#include <errno.h>
 
61
 
 
62
#include "gmessages.h"
 
63
 
 
64
#include "gbacktrace.h"
 
65
#include "gconvert.h"
 
66
#include "gdebug.h"
 
67
#include "gmem.h"
 
68
#include "gprintfint.h"
 
69
#include "gtestutils.h"
 
70
#include "gthread.h"
 
71
#include "gthreadprivate.h"
 
72
#include "gstrfuncs.h"
 
73
#include "gstring.h"
 
74
 
 
75
#ifdef G_OS_WIN32
 
76
#include <process.h>            /* For getpid() */
 
77
#include <io.h>
 
78
#  define STRICT                /* Strict typing, please */
 
79
#  define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
 
80
#  include <windows.h>
 
81
#  undef STRICT
 
82
#endif
 
83
 
 
84
 
 
85
/* --- structures --- */
 
86
typedef struct _GLogDomain      GLogDomain;
 
87
typedef struct _GLogHandler     GLogHandler;
 
88
struct _GLogDomain
 
89
{
 
90
  gchar         *log_domain;
 
91
  GLogLevelFlags fatal_mask;
 
92
  GLogHandler   *handlers;
 
93
  GLogDomain    *next;
 
94
};
 
95
struct _GLogHandler
 
96
{
 
97
  guint          id;
 
98
  GLogLevelFlags log_level;
 
99
  GLogFunc       log_func;
 
100
  gpointer       data;
 
101
  GLogHandler   *next;
 
102
};
 
103
 
 
104
 
 
105
/* --- variables --- */
 
106
static GMutex        *g_messages_lock = NULL;
 
107
static GLogDomain    *g_log_domains = NULL;
 
108
static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
 
109
static GPrintFunc     glib_print_func = NULL;
 
110
static GPrintFunc     glib_printerr_func = NULL;
 
111
static GPrivate      *g_log_depth = NULL;
 
112
static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
 
113
static GLogFunc       default_log_func = g_log_default_handler;
 
114
static gpointer       default_log_data = NULL;
 
115
static GTestLogFatalFunc fatal_log_func = NULL;
 
116
static gpointer          fatal_log_data;
 
117
 
 
118
/* --- functions --- */
 
119
#ifdef G_OS_WIN32
 
120
#  define STRICT
 
121
#  include <windows.h>
 
122
#  undef STRICT
 
123
static gboolean win32_keep_fatal_message = FALSE;
 
124
 
 
125
/* This default message will usually be overwritten. */
 
126
/* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
 
127
 * called with huge strings, is it?
 
128
 */
 
129
static gchar  fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
 
130
static gchar *fatal_msg_ptr = fatal_msg_buf;
 
131
 
 
132
#undef write
 
133
static inline int
 
134
dowrite (int          fd,
 
135
         const void  *buf,
 
136
         unsigned int len)
 
137
{
 
138
  if (win32_keep_fatal_message)
 
139
    {
 
140
      memcpy (fatal_msg_ptr, buf, len);
 
141
      fatal_msg_ptr += len;
 
142
      *fatal_msg_ptr = 0;
 
143
      return len;
 
144
    }
 
145
 
 
146
  write (fd, buf, len);
 
147
 
 
148
  return len;
 
149
}
 
150
#define write(fd, buf, len) dowrite(fd, buf, len)
 
151
 
 
152
#endif
 
153
 
 
154
static void
 
155
write_string (int          fd,
 
156
              const gchar *string)
 
157
{
 
158
  write (fd, string, strlen (string));
 
159
}
 
160
 
 
161
static void
 
162
g_messages_prefixed_init (void)
 
163
{
 
164
  static gboolean initialized = FALSE;
 
165
 
 
166
  if (!initialized)
 
167
    {
 
168
      const gchar *val;
 
169
 
 
170
      initialized = TRUE;
 
171
      val = g_getenv ("G_MESSAGES_PREFIXED");
 
172
      
 
173
      if (val)
 
174
        {
 
175
          const GDebugKey keys[] = {
 
176
            { "error", G_LOG_LEVEL_ERROR },
 
177
            { "critical", G_LOG_LEVEL_CRITICAL },
 
178
            { "warning", G_LOG_LEVEL_WARNING },
 
179
            { "message", G_LOG_LEVEL_MESSAGE },
 
180
            { "info", G_LOG_LEVEL_INFO },
 
181
            { "debug", G_LOG_LEVEL_DEBUG }
 
182
          };
 
183
          
 
184
          g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
 
185
        }
 
186
    }
 
187
}
 
188
 
 
189
static GLogDomain*
 
190
g_log_find_domain_L (const gchar *log_domain)
 
191
{
 
192
  register GLogDomain *domain;
 
193
  
 
194
  domain = g_log_domains;
 
195
  while (domain)
 
196
    {
 
197
      if (strcmp (domain->log_domain, log_domain) == 0)
 
198
        return domain;
 
199
      domain = domain->next;
 
200
    }
 
201
  return NULL;
 
202
}
 
203
 
 
204
static GLogDomain*
 
205
g_log_domain_new_L (const gchar *log_domain)
 
206
{
 
207
  register GLogDomain *domain;
 
208
 
 
209
  domain = g_new (GLogDomain, 1);
 
210
  domain->log_domain = g_strdup (log_domain);
 
211
  domain->fatal_mask = G_LOG_FATAL_MASK;
 
212
  domain->handlers = NULL;
 
213
  
 
214
  domain->next = g_log_domains;
 
215
  g_log_domains = domain;
 
216
  
 
217
  return domain;
 
218
}
 
219
 
 
220
static void
 
221
g_log_domain_check_free_L (GLogDomain *domain)
 
222
{
 
223
  if (domain->fatal_mask == G_LOG_FATAL_MASK &&
 
224
      domain->handlers == NULL)
 
225
    {
 
226
      register GLogDomain *last, *work;
 
227
      
 
228
      last = NULL;  
 
229
 
 
230
      work = g_log_domains;
 
231
      while (work)
 
232
        {
 
233
          if (work == domain)
 
234
            {
 
235
              if (last)
 
236
                last->next = domain->next;
 
237
              else
 
238
                g_log_domains = domain->next;
 
239
              g_free (domain->log_domain);
 
240
              g_free (domain);
 
241
              break;
 
242
            }
 
243
          last = work;
 
244
          work = last->next;
 
245
        }  
 
246
    }
 
247
}
 
248
 
 
249
static GLogFunc
 
250
g_log_domain_get_handler_L (GLogDomain  *domain,
 
251
                            GLogLevelFlags log_level,
 
252
                            gpointer    *data)
 
253
{
 
254
  if (domain && log_level)
 
255
    {
 
256
      register GLogHandler *handler;
 
257
      
 
258
      handler = domain->handlers;
 
259
      while (handler)
 
260
        {
 
261
          if ((handler->log_level & log_level) == log_level)
 
262
            {
 
263
              *data = handler->data;
 
264
              return handler->log_func;
 
265
            }
 
266
          handler = handler->next;
 
267
        }
 
268
    }
 
269
 
 
270
  *data = default_log_data;
 
271
  return default_log_func;
 
272
}
 
273
 
 
274
GLogLevelFlags
 
275
g_log_set_always_fatal (GLogLevelFlags fatal_mask)
 
276
{
 
277
  GLogLevelFlags old_mask;
 
278
 
 
279
  /* restrict the global mask to levels that are known to glib
 
280
   * since this setting applies to all domains
 
281
   */
 
282
  fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
 
283
  /* force errors to be fatal */
 
284
  fatal_mask |= G_LOG_LEVEL_ERROR;
 
285
  /* remove bogus flag */
 
286
  fatal_mask &= ~G_LOG_FLAG_FATAL;
 
287
 
 
288
  g_mutex_lock (g_messages_lock);
 
289
  old_mask = g_log_always_fatal;
 
290
  g_log_always_fatal = fatal_mask;
 
291
  g_mutex_unlock (g_messages_lock);
 
292
 
 
293
  return old_mask;
 
294
}
 
295
 
 
296
GLogLevelFlags
 
297
g_log_set_fatal_mask (const gchar   *log_domain,
 
298
                      GLogLevelFlags fatal_mask)
 
299
{
 
300
  GLogLevelFlags old_flags;
 
301
  register GLogDomain *domain;
 
302
  
 
303
  if (!log_domain)
 
304
    log_domain = "";
 
305
  
 
306
  /* force errors to be fatal */
 
307
  fatal_mask |= G_LOG_LEVEL_ERROR;
 
308
  /* remove bogus flag */
 
309
  fatal_mask &= ~G_LOG_FLAG_FATAL;
 
310
  
 
311
  g_mutex_lock (g_messages_lock);
 
312
 
 
313
  domain = g_log_find_domain_L (log_domain);
 
314
  if (!domain)
 
315
    domain = g_log_domain_new_L (log_domain);
 
316
  old_flags = domain->fatal_mask;
 
317
  
 
318
  domain->fatal_mask = fatal_mask;
 
319
  g_log_domain_check_free_L (domain);
 
320
 
 
321
  g_mutex_unlock (g_messages_lock);
 
322
 
 
323
  return old_flags;
 
324
}
 
325
 
 
326
guint
 
327
g_log_set_handler (const gchar   *log_domain,
 
328
                   GLogLevelFlags log_levels,
 
329
                   GLogFunc       log_func,
 
330
                   gpointer       user_data)
 
331
{
 
332
  static guint handler_id = 0;
 
333
  GLogDomain *domain;
 
334
  GLogHandler *handler;
 
335
  
 
336
  g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
 
337
  g_return_val_if_fail (log_func != NULL, 0);
 
338
  
 
339
  if (!log_domain)
 
340
    log_domain = "";
 
341
 
 
342
  handler = g_new (GLogHandler, 1);
 
343
 
 
344
  g_mutex_lock (g_messages_lock);
 
345
 
 
346
  domain = g_log_find_domain_L (log_domain);
 
347
  if (!domain)
 
348
    domain = g_log_domain_new_L (log_domain);
 
349
  
 
350
  handler->id = ++handler_id;
 
351
  handler->log_level = log_levels;
 
352
  handler->log_func = log_func;
 
353
  handler->data = user_data;
 
354
  handler->next = domain->handlers;
 
355
  domain->handlers = handler;
 
356
 
 
357
  g_mutex_unlock (g_messages_lock);
 
358
  
 
359
  return handler_id;
 
360
}
 
361
 
 
362
GLogFunc
 
363
g_log_set_default_handler (GLogFunc log_func,
 
364
                           gpointer user_data)
 
365
{
 
366
  GLogFunc old_log_func;
 
367
  
 
368
  g_mutex_lock (g_messages_lock);
 
369
  old_log_func = default_log_func;
 
370
  default_log_func = log_func;
 
371
  default_log_data = user_data;
 
372
  g_mutex_unlock (g_messages_lock);
 
373
  
 
374
  return old_log_func;
 
375
}
 
376
 
 
377
/**
 
378
 * g_test_log_set_fatal_handler:
 
379
 * @log_func: the log handler function.
 
380
 * @user_data: data passed to the log handler.
 
381
 *
 
382
 * Installs a non-error fatal log handler which can be
 
383
 * used to decide whether log messages which are counted
 
384
 * as fatal abort the program.
 
385
 *
 
386
 * The use case here is that you are running a test case
 
387
 * that depends on particular libraries or circumstances
 
388
 * and cannot prevent certain known critical or warning
 
389
 * messages. So you install a handler that compares the
 
390
 * domain and message to precisely not abort in such a case.
 
391
 *
 
392
 * Note that the handler is reset at the beginning of
 
393
 * any test case, so you have to set it inside each test
 
394
 * function which needs the special behavior.
 
395
 *
 
396
 * This handler has no effect on g_error messages.
 
397
 *
 
398
 * Since: 2.22
 
399
 **/
 
400
void
 
401
g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
 
402
                              gpointer          user_data)
 
403
{
 
404
  g_mutex_lock (g_messages_lock);
 
405
  fatal_log_func = log_func;
 
406
  fatal_log_data = user_data;
 
407
  g_mutex_unlock (g_messages_lock);
 
408
}
 
409
 
 
410
void
 
411
g_log_remove_handler (const gchar *log_domain,
 
412
                      guint        handler_id)
 
413
{
 
414
  register GLogDomain *domain;
 
415
  
 
416
  g_return_if_fail (handler_id > 0);
 
417
  
 
418
  if (!log_domain)
 
419
    log_domain = "";
 
420
  
 
421
  g_mutex_lock (g_messages_lock);
 
422
  domain = g_log_find_domain_L (log_domain);
 
423
  if (domain)
 
424
    {
 
425
      GLogHandler *work, *last;
 
426
      
 
427
      last = NULL;
 
428
      work = domain->handlers;
 
429
      while (work)
 
430
        {
 
431
          if (work->id == handler_id)
 
432
            {
 
433
              if (last)
 
434
                last->next = work->next;
 
435
              else
 
436
                domain->handlers = work->next;
 
437
              g_log_domain_check_free_L (domain); 
 
438
              g_mutex_unlock (g_messages_lock);
 
439
              g_free (work);
 
440
              return;
 
441
            }
 
442
          last = work;
 
443
          work = last->next;
 
444
        }
 
445
    } 
 
446
  g_mutex_unlock (g_messages_lock);
 
447
  g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
 
448
             G_STRLOC, handler_id, log_domain);
 
449
}
 
450
 
 
451
void
 
452
g_logv (const gchar   *log_domain,
 
453
        GLogLevelFlags log_level,
 
454
        const gchar   *format,
 
455
        va_list        args1)
 
456
{
 
457
  gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
 
458
  gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
 
459
  gint i;
 
460
 
 
461
  log_level &= G_LOG_LEVEL_MASK;
 
462
  if (!log_level)
 
463
    return;
 
464
 
 
465
  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
 
466
    {
 
467
      register GLogLevelFlags test_level;
 
468
 
 
469
      test_level = 1 << i;
 
470
      if (log_level & test_level)
 
471
        {
 
472
          guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
 
473
          GLogDomain *domain;
 
474
          GLogFunc log_func;
 
475
          GLogLevelFlags domain_fatal_mask;
 
476
          gpointer data = NULL;
 
477
          gboolean masquerade_fatal = FALSE;
 
478
 
 
479
          if (was_fatal)
 
480
            test_level |= G_LOG_FLAG_FATAL;
 
481
          if (was_recursion)
 
482
            test_level |= G_LOG_FLAG_RECURSION;
 
483
 
 
484
          /* check recursion and lookup handler */
 
485
          g_mutex_lock (g_messages_lock);
 
486
          domain = g_log_find_domain_L (log_domain ? log_domain : "");
 
487
          if (depth)
 
488
            test_level |= G_LOG_FLAG_RECURSION;
 
489
          depth++;
 
490
          domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
 
491
          if ((domain_fatal_mask | g_log_always_fatal) & test_level)
 
492
            test_level |= G_LOG_FLAG_FATAL;
 
493
          if (test_level & G_LOG_FLAG_RECURSION)
 
494
            log_func = _g_log_fallback_handler;
 
495
          else
 
496
            log_func = g_log_domain_get_handler_L (domain, test_level, &data);
 
497
          domain = NULL;
 
498
          g_mutex_unlock (g_messages_lock);
 
499
 
 
500
          g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
 
501
 
 
502
          /* had to defer debug initialization until we can keep track of recursion */
 
503
          if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
 
504
            {
 
505
              GLogLevelFlags orig_test_level = test_level;
 
506
 
 
507
              _g_debug_init ();
 
508
              if ((domain_fatal_mask | g_log_always_fatal) & test_level)
 
509
                test_level |= G_LOG_FLAG_FATAL;
 
510
              if (test_level != orig_test_level)
 
511
                {
 
512
                  /* need a relookup, not nice, but not too bad either */
 
513
                  g_mutex_lock (g_messages_lock);
 
514
                  domain = g_log_find_domain_L (log_domain ? log_domain : "");
 
515
                  log_func = g_log_domain_get_handler_L (domain, test_level, &data);
 
516
                  domain = NULL;
 
517
                  g_mutex_unlock (g_messages_lock);
 
518
                }
 
519
            }
 
520
 
 
521
          if (test_level & G_LOG_FLAG_RECURSION)
 
522
            {
 
523
              /* we use a stack buffer of fixed size, since we're likely
 
524
               * in an out-of-memory situation
 
525
               */
 
526
              gchar buffer[1025];
 
527
              gsize size G_GNUC_UNUSED;
 
528
              va_list args2;
 
529
 
 
530
              G_VA_COPY (args2, args1);
 
531
              size = _g_vsnprintf (buffer, 1024, format, args2);
 
532
              va_end (args2);
 
533
 
 
534
              log_func (log_domain, test_level, buffer, data);
 
535
            }
 
536
          else
 
537
            {
 
538
              gchar *msg;
 
539
              va_list args2;
 
540
 
 
541
              G_VA_COPY (args2, args1);
 
542
              msg = g_strdup_vprintf (format, args2);
 
543
              va_end (args2);
 
544
 
 
545
              log_func (log_domain, test_level, msg, data);
 
546
 
 
547
              if ((test_level & G_LOG_FLAG_FATAL)
 
548
                && !(test_level & G_LOG_LEVEL_ERROR))
 
549
                {
 
550
                  masquerade_fatal = fatal_log_func
 
551
                    && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
 
552
                }
 
553
 
 
554
              g_free (msg);
 
555
            }
 
556
 
 
557
          if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
 
558
            {
 
559
#ifdef G_OS_WIN32
 
560
              gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
 
561
              
 
562
              MessageBox (NULL, locale_msg, NULL,
 
563
                          MB_ICONERROR|MB_SETFOREGROUND);
 
564
              if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
 
565
                G_BREAKPOINT ();
 
566
              else
 
567
                abort ();
 
568
#else
 
569
              if (!(test_level & G_LOG_FLAG_RECURSION))
 
570
                G_BREAKPOINT ();
 
571
              else
 
572
                abort ();
 
573
#endif /* !G_OS_WIN32 */
 
574
            }
 
575
          
 
576
          depth--;
 
577
          g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
 
578
        }
 
579
    }
 
580
}
 
581
 
 
582
void
 
583
g_log (const gchar   *log_domain,
 
584
       GLogLevelFlags log_level,
 
585
       const gchar   *format,
 
586
       ...)
 
587
{
 
588
  va_list args;
 
589
  
 
590
  va_start (args, format);
 
591
  g_logv (log_domain, log_level, format, args);
 
592
  va_end (args);
 
593
}
 
594
 
 
595
void
 
596
g_return_if_fail_warning (const char *log_domain,
 
597
                          const char *pretty_function,
 
598
                          const char *expression)
 
599
{
 
600
  g_log (log_domain,
 
601
         G_LOG_LEVEL_CRITICAL,
 
602
         "%s: assertion `%s' failed",
 
603
         pretty_function,
 
604
         expression);
 
605
}
 
606
 
 
607
void
 
608
g_warn_message (const char     *domain,
 
609
                const char     *file,
 
610
                int             line,
 
611
                const char     *func,
 
612
                const char     *warnexpr)
 
613
{
 
614
  char *s, lstr[32];
 
615
  g_snprintf (lstr, 32, "%d", line);
 
616
  if (warnexpr)
 
617
    s = g_strconcat ("(", file, ":", lstr, "):",
 
618
                     func, func[0] ? ":" : "",
 
619
                     " runtime check failed: (", warnexpr, ")", NULL);
 
620
  else
 
621
    s = g_strconcat ("(", file, ":", lstr, "):",
 
622
                     func, func[0] ? ":" : "",
 
623
                     " ", "code should not be reached", NULL);
 
624
  g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
 
625
  g_free (s);
 
626
}
 
627
 
 
628
void
 
629
g_assert_warning (const char *log_domain,
 
630
                  const char *file,
 
631
                  const int   line,
 
632
                  const char *pretty_function,
 
633
                  const char *expression)
 
634
{
 
635
  g_log (log_domain,
 
636
         G_LOG_LEVEL_ERROR,
 
637
         expression 
 
638
         ? "file %s: line %d (%s): assertion failed: (%s)"
 
639
         : "file %s: line %d (%s): should not be reached",
 
640
         file, 
 
641
         line, 
 
642
         pretty_function,
 
643
         expression);
 
644
  abort ();
 
645
}
 
646
 
 
647
#define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
 
648
                            (wc == 0x7f) || \
 
649
                            (wc >= 0x80 && wc < 0xa0)))
 
650
     
 
651
/* For a radix of 8 we need at most 3 output bytes for 1 input
 
652
 * byte. Additionally we might need up to 2 output bytes for the
 
653
 * readix prefix and 1 byte for the trailing NULL.
 
654
 */
 
655
#define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
 
656
 
 
657
static void
 
658
format_unsigned (gchar  *buf,
 
659
                 gulong  num,
 
660
                 guint   radix)
 
661
{
 
662
  gulong tmp;
 
663
  gchar c;
 
664
  gint i, n;
 
665
 
 
666
  /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
 
667
 
 
668
  if (radix != 8 && radix != 10 && radix != 16)
 
669
    {
 
670
      *buf = '\000';
 
671
      return;
 
672
    }
 
673
  
 
674
  if (!num)
 
675
    {
 
676
      *buf++ = '0';
 
677
      *buf = '\000';
 
678
      return;
 
679
    } 
 
680
  
 
681
  if (radix == 16)
 
682
    {
 
683
      *buf++ = '0';
 
684
      *buf++ = 'x';
 
685
    }
 
686
  else if (radix == 8)
 
687
    {
 
688
      *buf++ = '0';
 
689
    }
 
690
        
 
691
  n = 0;
 
692
  tmp = num;
 
693
  while (tmp)
 
694
    {
 
695
      tmp /= radix;
 
696
      n++;
 
697
    }
 
698
 
 
699
  i = n;
 
700
 
 
701
  /* Again we can't use g_assert; actually this check should _never_ fail. */
 
702
  if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
 
703
    {
 
704
      *buf = '\000';
 
705
      return;
 
706
    }
 
707
 
 
708
  while (num)
 
709
    {
 
710
      i--;
 
711
      c = (num % radix);
 
712
      if (c < 10)
 
713
        buf[i] = c + '0';
 
714
      else
 
715
        buf[i] = c + 'a' - 10;
 
716
      num /= radix;
 
717
    }
 
718
  
 
719
  buf[n] = '\000';
 
720
}
 
721
 
 
722
/* string size big enough to hold level prefix */
 
723
#define STRING_BUFFER_SIZE      (FORMAT_UNSIGNED_BUFSIZE + 32)
 
724
 
 
725
#define ALERT_LEVELS            (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
 
726
 
 
727
static int
 
728
mklevel_prefix (gchar          level_prefix[STRING_BUFFER_SIZE],
 
729
                GLogLevelFlags log_level)
 
730
{
 
731
  gboolean to_stdout = TRUE;
 
732
 
 
733
  /* we may not call _any_ GLib functions here */
 
734
 
 
735
  switch (log_level & G_LOG_LEVEL_MASK)
 
736
    {
 
737
    case G_LOG_LEVEL_ERROR:
 
738
      strcpy (level_prefix, "ERROR");
 
739
      to_stdout = FALSE;
 
740
      break;
 
741
    case G_LOG_LEVEL_CRITICAL:
 
742
      strcpy (level_prefix, "CRITICAL");
 
743
      to_stdout = FALSE;
 
744
      break;
 
745
    case G_LOG_LEVEL_WARNING:
 
746
      strcpy (level_prefix, "WARNING");
 
747
      to_stdout = FALSE;
 
748
      break;
 
749
    case G_LOG_LEVEL_MESSAGE:
 
750
      strcpy (level_prefix, "Message");
 
751
      to_stdout = FALSE;
 
752
      break;
 
753
    case G_LOG_LEVEL_INFO:
 
754
      strcpy (level_prefix, "INFO");
 
755
      break;
 
756
    case G_LOG_LEVEL_DEBUG:
 
757
      strcpy (level_prefix, "DEBUG");
 
758
      break;
 
759
    default:
 
760
      if (log_level)
 
761
        {
 
762
          strcpy (level_prefix, "LOG-");
 
763
          format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
 
764
        }
 
765
      else
 
766
        strcpy (level_prefix, "LOG");
 
767
      break;
 
768
    }
 
769
  if (log_level & G_LOG_FLAG_RECURSION)
 
770
    strcat (level_prefix, " (recursed)");
 
771
  if (log_level & ALERT_LEVELS)
 
772
    strcat (level_prefix, " **");
 
773
 
 
774
#ifdef G_OS_WIN32
 
775
  win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
 
776
#endif
 
777
  return to_stdout ? 1 : 2;
 
778
}
 
779
 
 
780
void
 
781
_g_log_fallback_handler (const gchar   *log_domain,
 
782
                         GLogLevelFlags log_level,
 
783
                         const gchar   *message,
 
784
                         gpointer       unused_data)
 
785
{
 
786
  gchar level_prefix[STRING_BUFFER_SIZE];
 
787
#ifndef G_OS_WIN32
 
788
  gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
 
789
#endif
 
790
  int fd;
 
791
 
 
792
  /* we cannot call _any_ GLib functions in this fallback handler,
 
793
   * which is why we skip UTF-8 conversion, etc.
 
794
   * since we either recursed or ran out of memory, we're in a pretty
 
795
   * pathologic situation anyways, what we can do is giving the
 
796
   * the process ID unconditionally however.
 
797
   */
 
798
 
 
799
  fd = mklevel_prefix (level_prefix, log_level);
 
800
  if (!message)
 
801
    message = "(NULL) message";
 
802
 
 
803
#ifndef G_OS_WIN32
 
804
  format_unsigned (pid_string, getpid (), 10);
 
805
#endif
 
806
 
 
807
  if (log_domain)
 
808
    write_string (fd, "\n");
 
809
  else
 
810
    write_string (fd, "\n** ");
 
811
 
 
812
#ifndef G_OS_WIN32
 
813
  write_string (fd, "(process:");
 
814
  write_string (fd, pid_string);
 
815
  write_string (fd, "): ");
 
816
#endif
 
817
 
 
818
  if (log_domain)
 
819
    {
 
820
      write_string (fd, log_domain);
 
821
      write_string (fd, "-");
 
822
    }
 
823
  write_string (fd, level_prefix);
 
824
  write_string (fd, ": ");
 
825
  write_string (fd, message);
 
826
}
 
827
 
 
828
void
 
829
g_log_default_handler (const gchar   *log_domain,
 
830
                       GLogLevelFlags log_level,
 
831
                       const gchar   *message,
 
832
                       gpointer       unused_data)
 
833
{
 
834
  gchar level_prefix[STRING_BUFFER_SIZE], *string;
 
835
  GString *gstring;
 
836
  int fd;
 
837
 
 
838
  /* we can be called externally with recursion for whatever reason */
 
839
  if (log_level & G_LOG_FLAG_RECURSION)
 
840
    {
 
841
      _g_log_fallback_handler (log_domain, log_level, message, unused_data);
 
842
      return;
 
843
    }
 
844
 
 
845
  g_messages_prefixed_init ();
 
846
 
 
847
  fd = mklevel_prefix (level_prefix, log_level);
 
848
 
 
849
  gstring = g_string_new (NULL);
 
850
  if (log_level & ALERT_LEVELS)
 
851
    g_string_append (gstring, "\n");
 
852
  if (!log_domain)
 
853
    g_string_append (gstring, "** ");
 
854
 
 
855
  if ((g_log_msg_prefix & log_level) == log_level)
 
856
    {
 
857
      const gchar *prg_name = g_get_prgname ();
 
858
      
 
859
      if (!prg_name)
 
860
        g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
 
861
      else
 
862
        g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
 
863
    }
 
864
 
 
865
  if (log_domain)
 
866
    {
 
867
      g_string_append (gstring, log_domain);
 
868
      g_string_append_c (gstring, '-');
 
869
    }
 
870
  g_string_append (gstring, level_prefix);
 
871
 
 
872
  g_string_append (gstring, ": ");
 
873
  if (!message)
 
874
    g_string_append (gstring, "(NULL) message");
 
875
  else
 
876
    {
 
877
      GString *msg;
 
878
 
 
879
      msg = g_string_new (message);
 
880
 
 
881
      g_string_append (gstring, msg->str);      /* assume UTF-8 */
 
882
 
 
883
      g_string_free (msg, TRUE);
 
884
    }
 
885
  g_string_append (gstring, "\n");
 
886
 
 
887
  string = g_string_free (gstring, FALSE);
 
888
 
 
889
  write_string (fd, string);
 
890
  g_free (string);
 
891
}
 
892
 
 
893
/**
 
894
 * g_set_print_handler:
 
895
 * @func: the new print handler
 
896
 *
 
897
 * Sets the print handler.
 
898
 *
 
899
 * Any messages passed to g_print() will be output via
 
900
 * the new handler. The default handler simply outputs
 
901
 * the message to stdout. By providing your own handler
 
902
 * you can redirect the output, to a GTK+ widget or a
 
903
 * log file for example.
 
904
 *
 
905
 * Returns: the old print handler
 
906
 */
 
907
GPrintFunc
 
908
g_set_print_handler (GPrintFunc func)
 
909
{
 
910
  GPrintFunc old_print_func;
 
911
 
 
912
  g_mutex_lock (g_messages_lock);
 
913
  old_print_func = glib_print_func;
 
914
  glib_print_func = func;
 
915
  g_mutex_unlock (g_messages_lock);
 
916
 
 
917
  return old_print_func;
 
918
}
 
919
 
 
920
/**
 
921
 * g_print:
 
922
 * @format: the message format. See the printf() documentation
 
923
 * @...: the parameters to insert into the format string
 
924
 *
 
925
 * Outputs a formatted message via the print handler.
 
926
 * The default print handler simply outputs the message to stdout.
 
927
 *
 
928
 * g_print() should not be used from within libraries for debugging
 
929
 * messages, since it may be redirected by applications to special
 
930
 * purpose message windows or even files. Instead, libraries should
 
931
 * use g_log(), or the convenience functions g_message(), g_warning()
 
932
 * and g_error().
 
933
 */
 
934
void
 
935
g_print (const gchar *format,
 
936
         ...)
 
937
{
 
938
  va_list args;
 
939
  gchar *string;
 
940
  GPrintFunc local_glib_print_func;
 
941
 
 
942
  g_return_if_fail (format != NULL);
 
943
 
 
944
  va_start (args, format);
 
945
  string = g_strdup_vprintf (format, args);
 
946
  va_end (args);
 
947
 
 
948
  g_mutex_lock (g_messages_lock);
 
949
  local_glib_print_func = glib_print_func;
 
950
  g_mutex_unlock (g_messages_lock);
 
951
 
 
952
  if (local_glib_print_func)
 
953
    local_glib_print_func (string);
 
954
  else
 
955
    {
 
956
      fputs (string, stdout); /* assume UTF-8 */
 
957
      fflush (stdout);
 
958
    }
 
959
  g_free (string);
 
960
}
 
961
 
 
962
/**
 
963
 * g_set_printerr_handler:
 
964
 * @func: the new error message handler
 
965
 *
 
966
 * Sets the handler for printing error messages.
 
967
 *
 
968
 * Any messages passed to g_printerr() will be output via
 
969
 * the new handler. The default handler simply outputs the
 
970
 * message to stderr. By providing your own handler you can
 
971
 * redirect the output, to a GTK+ widget or a log file for
 
972
 * example.
 
973
 *
 
974
 * Returns: the old error message handler
 
975
 */
 
976
GPrintFunc
 
977
g_set_printerr_handler (GPrintFunc func)
 
978
{
 
979
  GPrintFunc old_printerr_func;
 
980
 
 
981
  g_mutex_lock (g_messages_lock);
 
982
  old_printerr_func = glib_printerr_func;
 
983
  glib_printerr_func = func;
 
984
  g_mutex_unlock (g_messages_lock);
 
985
 
 
986
  return old_printerr_func;
 
987
}
 
988
 
 
989
/**
 
990
 * g_printerr:
 
991
 * @format: the message format. See the printf() documentation
 
992
 * @...: the parameters to insert into the format string
 
993
 *
 
994
 * Outputs a formatted message via the error message handler.
 
995
 * The default handler simply outputs the message to stderr.
 
996
 *
 
997
 * g_printerr() should not be used from within libraries.
 
998
 * Instead g_log() should be used, or the convenience functions
 
999
 * g_message(), g_warning() and g_error().
 
1000
 */
 
1001
void
 
1002
g_printerr (const gchar *format,
 
1003
            ...)
 
1004
{
 
1005
  va_list args;
 
1006
  gchar *string;
 
1007
  GPrintFunc local_glib_printerr_func;
 
1008
 
 
1009
  g_return_if_fail (format != NULL);
 
1010
 
 
1011
  va_start (args, format);
 
1012
  string = g_strdup_vprintf (format, args);
 
1013
  va_end (args);
 
1014
 
 
1015
  g_mutex_lock (g_messages_lock);
 
1016
  local_glib_printerr_func = glib_printerr_func;
 
1017
  g_mutex_unlock (g_messages_lock);
 
1018
 
 
1019
  if (local_glib_printerr_func)
 
1020
    local_glib_printerr_func (string);
 
1021
  else
 
1022
    {
 
1023
      fputs (string, stderr); /* assume UTF-8 */
 
1024
      fflush (stderr);
 
1025
    }
 
1026
  g_free (string);
 
1027
}
 
1028
 
 
1029
gsize
 
1030
g_printf_string_upper_bound (const gchar *format,
 
1031
                             va_list      args)
 
1032
{
 
1033
  gchar c;
 
1034
  return _g_vsnprintf (&c, 1, format, args) + 1;
 
1035
}
 
1036
 
 
1037
void
 
1038
_g_messages_thread_init_nomessage (void)
 
1039
{
 
1040
  g_messages_lock = g_mutex_new ();
 
1041
  g_log_depth = g_private_new (NULL);
 
1042
  g_messages_prefixed_init ();
 
1043
  _g_debug_init ();
 
1044
}
 
1045
 
 
1046
gboolean _g_debug_initialized = FALSE;
 
1047
guint _g_debug_flags = 0;
 
1048
 
 
1049
void
 
1050
_g_debug_init (void) 
 
1051
{
 
1052
  const gchar *val;
 
1053
  
 
1054
  _g_debug_initialized = TRUE;
 
1055
  
 
1056
  val = g_getenv ("G_DEBUG");
 
1057
  if (val != NULL)
 
1058
    {
 
1059
      const GDebugKey keys[] = {
 
1060
        {"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
 
1061
        {"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
 
1062
      };
 
1063
      
 
1064
      _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
 
1065
    }
 
1066
  
 
1067
  if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS) 
 
1068
    {
 
1069
      GLogLevelFlags fatal_mask;
 
1070
      
 
1071
      fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
 
1072
      fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
 
1073
      g_log_set_always_fatal (fatal_mask);
 
1074
    }
 
1075
  
 
1076
  if (_g_debug_flags & G_DEBUG_FATAL_CRITICALS) 
 
1077
    {
 
1078
      GLogLevelFlags fatal_mask;
 
1079
      
 
1080
      fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
 
1081
      fatal_mask |= G_LOG_LEVEL_CRITICAL;
 
1082
      g_log_set_always_fatal (fatal_mask);
 
1083
    }
 
1084
}