File: glog.c

package info (click to toggle)
gtk-gnutella 0.98.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 50,104 kB
  • sloc: ansic: 269,840; sh: 10,787; xml: 1,347; yacc: 819; perl: 199; makefile: 100; python: 23; sed: 16
file content (119 lines) | stat: -rw-r--r-- 2,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2011, Raphael Manfredi
 *
 *----------------------------------------------------------------------
 * This file is part of gtk-gnutella.
 *
 *  gtk-gnutella is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  gtk-gnutella is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with gtk-gnutella; if not, write to the Free Software
 *  Foundation, Inc.:
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *----------------------------------------------------------------------
 */

/**
 * @ingroup lib
 * @file
 *
 * Glib logging remapping support.
 *
 * @author Raphael Manfredi
 * @date 2011
 */

#include "common.h"

#include "glog.h"
#include "log.h"
#include "str.h"
#include "thread.h"

#include "override.h"		/* Must be the last header included */

static gl_log_handler_t handler_cb;
static void *handler_data;

/**
 * Record logging handler.
 */
void
gl_log_set_handler(gl_log_handler_t handler, void *data)
{
	handler_cb = handler;
	handler_data = data;
}

/**
 * Log message.
 */
void
gl_logv(const char *domain, GLogLevelFlags flags, const char *fmt, va_list args)
{
	static str_t *msg;
	static bool logging;

	if (logging) {
		s_minilogv(flags | G_LOG_FLAG_RECURSION, FALSE, fmt, args);
		return;
	}

	/*
	 * This call is thread-unsafe by construction, and supposed to be called
	 * only from the main thread.  This is why it's OK to have a global
	 * ``logging'' variable.
	 */

	logging = TRUE;

	if G_UNLIKELY(NULL == msg)
		msg = str_new_not_leaking(0);

	str_vprintf(msg, fmt, args);

	if (handler_cb != NULL)
		(*handler_cb)(domain, flags, str_2c(msg), handler_data);
	else
		s_minilog(flags, "%s", str_2c(msg));

	logging = FALSE;
}

/**
 * Log message.
 */
void gl_log(const char *domain, GLogLevelFlags flags, const char *format, ...)
{
	va_list args;

	va_start(args, format);
	gl_logv(domain, flags, format, args);
	va_end(args);
}

/**
 * Log fata error.
 *
 * This routine does not return.
 */
void gl_error(const char *domain, const char *format, ...)
{
	va_list args;

	va_start(args, format);
	gl_logv(domain, G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL, format, args);
	va_end(args);

	log_abort();
}

/* vi: set ts=4 sw=4 cindent: */