~registry/glib/glib-2-30

« back to all changes in this revision

Viewing changes to glib/gmessages.c

  • Committer: Owen Taylor
  • Date: 1998-12-15 05:28:02 UTC
  • Revision ID: git-v1:931ea952650b013b834041b91b0c37a748ffd449
This commit merges the glib-threads branch into the main
branch. See the ChangeLog for details of the changes.

In brief overview:

 - The set of threading functions can be set
 - A default implementation is provided in -lgthread
 - All static data structures are locked using these
   functions if g_thread_init() is called.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * Boston, MA 02111-1307, USA.
18
18
 */
19
19
 
 
20
/* 
 
21
 * MT safe
 
22
 */
 
23
 
20
24
#ifdef HAVE_CONFIG_H
21
25
#include <config.h>
22
26
#endif
31
35
#endif
32
36
 
33
37
#ifdef NATIVE_WIN32
34
 
/* Just use stdio. If we're out of memroy, we're hosed anyway. */
 
38
/* Just use stdio. If we're out of memory, we're hosed anyway. */
35
39
#undef write
36
40
 
37
41
static inline int
67
71
 
68
72
 
69
73
/* --- variables --- */
 
74
 
 
75
static GMutex* g_messages_lock = NULL;
 
76
 
70
77
const gchar          *g_log_domain_glib = "GLib";
71
78
static GLogDomain    *g_log_domains = NULL;
72
79
static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
76
83
static GWarningFunc   glib_warning_func = NULL;
77
84
static GPrintFunc     glib_message_func = NULL;
78
85
 
 
86
static GPrivate* g_log_depth = NULL;
 
87
 
79
88
 
80
89
/* --- functions --- */
81
90
static inline GLogDomain*
82
91
g_log_find_domain (const gchar    *log_domain)
83
92
{
84
93
  register GLogDomain *domain;
85
 
 
 
94
  
 
95
  g_mutex_lock (g_messages_lock);
86
96
  domain = g_log_domains;
87
97
  while (domain)
88
98
    {
89
99
      if (strcmp (domain->log_domain, log_domain) == 0)
90
 
        return domain;
 
100
        {
 
101
          g_mutex_unlock (g_messages_lock);
 
102
          return domain;
 
103
        }
91
104
      domain = domain->next;
92
105
    }
 
106
  g_mutex_unlock (g_messages_lock);
93
107
  return NULL;
94
108
}
95
109
 
102
116
  domain->log_domain = g_strdup (log_domain);
103
117
  domain->fatal_mask = G_LOG_FATAL_MASK;
104
118
  domain->handlers = NULL;
 
119
  
 
120
  g_mutex_lock (g_messages_lock);
105
121
  domain->next = g_log_domains;
106
122
  g_log_domains = domain;
 
123
  g_mutex_unlock (g_messages_lock);
107
124
  
108
125
  return domain;
109
126
}
116
133
    {
117
134
      register GLogDomain *last, *work;
118
135
      
119
 
      last = NULL;
 
136
      last = NULL;  
 
137
 
 
138
      g_mutex_lock (g_messages_lock);
120
139
      work = g_log_domains;
121
140
      while (work)
122
141
        {
131
150
              break;
132
151
            }
133
152
          work = work->next;
134
 
        }
 
153
        }  
 
154
      g_mutex_unlock (g_messages_lock);
135
155
    }
136
156
}
137
157
 
170
190
  /* remove bogus flag */
171
191
  fatal_mask &= ~G_LOG_FLAG_FATAL;
172
192
 
 
193
  g_mutex_lock (g_messages_lock);
173
194
  old_mask = g_log_always_fatal;
174
195
  g_log_always_fatal = fatal_mask;
 
196
  g_mutex_unlock (g_messages_lock);
175
197
 
176
198
  return old_mask;
177
199
}
223
245
    domain = g_log_domain_new (log_domain);
224
246
  
225
247
  handler = g_new (GLogHandler, 1);
 
248
  g_mutex_lock (g_messages_lock);
226
249
  handler->id = ++handler_id;
 
250
  g_mutex_unlock (g_messages_lock);
227
251
  handler->log_level = log_levels;
228
252
  handler->log_func = log_func;
229
253
  handler->data = user_data;
311
335
      test_level = 1 << i;
312
336
      if (log_level & test_level)
313
337
        {
314
 
          static guint g_log_depth = 0;
 
338
          guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
315
339
          GLogDomain *domain;
316
340
          GLogFunc log_func;
317
341
          gpointer data = NULL;
318
342
          
319
343
          domain = g_log_find_domain (log_domain ? log_domain : "");
320
344
          
321
 
          if (g_log_depth++)
 
345
          if (depth)
322
346
            test_level |= G_LOG_FLAG_RECURSION;
323
347
          
324
 
          if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) | g_log_always_fatal) &
325
 
               test_level) != 0)
326
 
            test_level |= G_LOG_FLAG_FATAL;
 
348
          depth++;
 
349
          g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
 
350
 
 
351
          g_mutex_lock (g_messages_lock);
 
352
          if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) | 
 
353
                g_log_always_fatal) & test_level) != 0)
 
354
            test_level |= G_LOG_FLAG_FATAL;  
 
355
          g_mutex_unlock (g_messages_lock);
 
356
 
327
357
          log_func = g_log_domain_get_handler (domain, test_level, &data);
328
358
          log_func (log_domain, test_level, buffer, data);
329
359
          
332
362
          if (test_level & G_LOG_FLAG_FATAL)
333
363
            abort ();
334
364
          
335
 
          g_log_depth--;
 
365
          depth--;
 
366
          g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
336
367
        }
337
368
    }
338
369
}
362
393
  gint fd;
363
394
#endif
364
395
  gboolean in_recursion;
365
 
  gboolean is_fatal;
366
 
  
 
396
  gboolean is_fatal;  
 
397
  GErrorFunc     local_glib_error_func;
 
398
  GWarningFunc   local_glib_warning_func;
 
399
  GPrintFunc     local_glib_message_func;
 
400
 
367
401
  in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
368
402
  is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
369
403
  log_level &= G_LOG_LEVEL_MASK;
380
414
  fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
381
415
#endif
382
416
  
 
417
  g_mutex_lock (g_messages_lock);
 
418
  local_glib_error_func = glib_error_func;
 
419
  local_glib_warning_func = glib_warning_func;
 
420
  local_glib_message_func = glib_message_func;
 
421
  g_mutex_unlock (g_messages_lock);
 
422
 
383
423
  switch (log_level)
384
424
    {
385
425
    case G_LOG_LEVEL_ERROR:
386
 
      if (!log_domain && glib_error_func)
 
426
      if (!log_domain && local_glib_error_func)
387
427
        {
388
428
          /* compatibility code */
389
 
          glib_error_func (message);
 
429
          local_glib_error_func (message);  
390
430
          return;
391
431
        }
392
432
      /* use write(2) for output, in case we are out of memeory */
428
468
        write (fd, "\n", 1);
429
469
      break;
430
470
    case G_LOG_LEVEL_WARNING:
431
 
      if (!log_domain && glib_warning_func)
 
471
      if (!log_domain && local_glib_warning_func)
432
472
        {
433
473
          /* compatibility code */
434
 
          glib_warning_func (message);
 
474
          local_glib_warning_func (message);
435
475
          return;
436
476
        }
437
477
      if (log_domain)
453
493
        write (fd, "\n", 1);
454
494
      break;
455
495
    case G_LOG_LEVEL_MESSAGE:
456
 
      if (!log_domain && glib_message_func)
 
496
      if (!log_domain && local_glib_message_func)
457
497
        {
458
498
          /* compatibility code */
459
 
          glib_message_func (message);
 
499
          local_glib_message_func (message);
460
500
          return;
461
501
        }
462
502
      if (log_domain)
553
593
{
554
594
  GPrintFunc old_print_func;
555
595
  
 
596
  g_mutex_lock (g_messages_lock);
556
597
  old_print_func = glib_print_func;
557
598
  glib_print_func = func;
 
599
  g_mutex_unlock (g_messages_lock);
558
600
  
559
601
  return old_print_func;
560
602
}
565
607
{
566
608
  va_list args;
567
609
  gchar *string;
 
610
  GPrintFunc local_glib_print_func;
568
611
  
569
612
  g_return_if_fail (format != NULL);
570
613
  
572
615
  string = g_strdup_vprintf (format, args);
573
616
  va_end (args);
574
617
  
575
 
  if (glib_print_func)
576
 
    glib_print_func (string);
 
618
  g_mutex_lock (g_messages_lock);
 
619
  local_glib_print_func = glib_print_func;
 
620
  g_mutex_unlock (g_messages_lock);
 
621
 
 
622
  if (local_glib_print_func)
 
623
    local_glib_print_func (string);
577
624
  else
578
625
    {
579
626
      fputs (string, stdout);
587
634
{
588
635
  GPrintFunc old_printerr_func;
589
636
  
 
637
  g_mutex_lock (g_messages_lock);
590
638
  old_printerr_func = glib_printerr_func;
591
639
  glib_printerr_func = func;
 
640
  g_mutex_unlock (g_messages_lock);
592
641
  
593
642
  return old_printerr_func;
594
643
}
599
648
{
600
649
  va_list args;
601
650
  gchar *string;
 
651
  GPrintFunc local_glib_printerr_func;
602
652
  
603
653
  g_return_if_fail (format != NULL);
604
654
  
606
656
  string = g_strdup_vprintf (format, args);
607
657
  va_end (args);
608
658
  
609
 
  if (glib_printerr_func)
610
 
    glib_printerr_func (string);
 
659
  g_mutex_lock (g_messages_lock);
 
660
  local_glib_printerr_func = glib_printerr_func;
 
661
  g_mutex_unlock (g_messages_lock);
 
662
 
 
663
  if (local_glib_printerr_func)
 
664
    local_glib_printerr_func (string);
611
665
  else
612
666
    {
613
667
      fputs (string, stderr);
622
676
{
623
677
  GErrorFunc old_error_func;
624
678
  
 
679
  g_mutex_lock (g_messages_lock);
625
680
  old_error_func = glib_error_func;
626
681
  glib_error_func = func;
627
 
  
 
682
  g_mutex_unlock (g_messages_lock);
 
683
 
628
684
  return old_error_func;
629
685
}
630
686
 
634
690
{
635
691
  GWarningFunc old_warning_func;
636
692
  
 
693
  g_mutex_lock (g_messages_lock);
637
694
  old_warning_func = glib_warning_func;
638
695
  glib_warning_func = func;
 
696
  g_mutex_unlock (g_messages_lock);
639
697
  
640
698
  return old_warning_func;
641
699
}
646
704
{
647
705
  GPrintFunc old_message_func;
648
706
  
 
707
  g_mutex_lock (g_messages_lock);
649
708
  old_message_func = glib_message_func;
650
709
  glib_message_func = func;
 
710
  g_mutex_unlock (g_messages_lock);
651
711
  
652
712
  return old_message_func;
653
713
}
 
714
 
 
715
void
 
716
g_messages_init (void)
 
717
{
 
718
  g_messages_lock = g_mutex_new();
 
719
  g_log_depth = g_private_new(NULL);
 
720
}