Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / src / server-logging.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* server-logging.c
3  *
4  * Copyright (C) 2003  Novell, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * Author: Chris Toshok <toshok@ximian.com>
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "server-logging.h"
28
29 \f
30 #define PARENT_TYPE bonobo_event_source_get_type ()
31 static BonoboEventSourceClass *parent_class = NULL;
32
33 typedef struct {
34         char *domain;
35         guint id;
36 } ServerLoggingHandler;
37
38 struct _ServerLoggingPrivate {
39         GSList *handlers;
40 };
41
42 \f
43 static void
44 server_logging_dispose (GObject *object)
45 {
46         ServerLogging *logging = SERVER_LOGGING (object);
47         ServerLoggingPrivate *priv;
48         GSList *l;
49         
50         priv = logging->priv;
51         
52         for (l = priv->handlers; l; l = l->next) {
53                 ServerLoggingHandler *handler = l->data;
54                 
55                 g_log_remove_handler (handler->domain, handler->id);
56                 g_free (handler->domain);
57                 g_free (handler);
58         }
59         g_slist_free (priv->handlers);
60         priv->handlers = NULL;
61
62         G_OBJECT_CLASS (parent_class)->dispose (object);
63 }
64
65 static void
66 server_logging_finalize (GObject *object)
67 {
68         ServerLogging *logging = SERVER_LOGGING (object);
69
70         g_free (logging->priv);
71
72         G_OBJECT_CLASS (parent_class)->finalize (object);
73 }
74
75 static void
76 server_logging_class_init (ServerLoggingClass *klass)
77 {
78         GObjectClass *object_class;
79
80         parent_class = g_type_class_peek_parent (klass);
81
82         object_class = G_OBJECT_CLASS (klass);
83         object_class->dispose = server_logging_dispose;
84         object_class->finalize = server_logging_finalize;
85 }
86
87 static void
88 server_logging_init (ServerLogging *logging)
89 {
90         ServerLoggingPrivate *priv;
91         
92         priv = g_new0 (ServerLoggingPrivate, 1);
93
94         logging->priv = priv;
95 }
96
97 \f
98 ServerLogging *
99 server_logging_new (void)
100 {
101         return g_object_new (SERVER_TYPE_LOGGING, NULL);
102 }
103
104 \f
105 BONOBO_TYPE_FUNC_FULL (ServerLogging,
106                        GNOME_Evolution_DataServer_Logging,
107                        PARENT_TYPE,
108                        server_logging)
109
110 \f
111 static void
112 server_log_handler(const gchar *domain,
113                    GLogLevelFlags flags,
114                    const gchar *msg,
115                    gpointer user_data)
116 {
117         ServerLogging *logging = SERVER_LOGGING (user_data);
118         BonoboEventSource *es = BONOBO_EVENT_SOURCE (logging);
119         CORBA_Environment ev;
120         CORBA_any value;
121         GNOME_Evolution_DataServer_Logging_LogEvent log_event;
122
123         printf ("in server_log_handler\n");
124
125         if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR)
126                 log_event.level = GNOME_Evolution_DataServer_Logging_Error;
127         else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL)
128                 log_event.level = GNOME_Evolution_DataServer_Logging_Critical;
129         else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING)
130                 log_event.level = GNOME_Evolution_DataServer_Logging_Warning;
131         else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE)
132                 log_event.level = GNOME_Evolution_DataServer_Logging_Warning;
133         else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO)
134                 log_event.level = GNOME_Evolution_DataServer_Logging_Info;
135         else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG)
136                 log_event.level = GNOME_Evolution_DataServer_Logging_Debug;
137
138         log_event.domain = (CORBA_char *) domain;
139         log_event.message = (CORBA_char *) msg;
140
141         value._type = TC_GNOME_Evolution_DataServer_Logging_LogEvent;
142         value._value = &log_event;
143
144         if (bonobo_event_source_has_listener (es, "log_event")) {
145                 CORBA_exception_init (&ev);
146                 bonobo_event_source_notify_listeners (es, "log_event", &value, &ev); 
147                 CORBA_exception_free (&ev);
148         }
149
150         g_log_default_handler (domain, flags, msg, NULL);
151 }
152
153 void
154 server_logging_register_domain (ServerLogging *logging,
155                                 const char *domain)
156 {
157         ServerLoggingPrivate *priv;
158         guint handler_id;
159
160         priv = logging->priv;
161         
162         handler_id = g_log_set_handler(domain, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
163                                        server_log_handler, logging);
164
165         if (handler_id) {
166                 ServerLoggingHandler *handler;
167
168                 handler = g_new0 (ServerLoggingHandler, 1);
169                 handler->domain = g_strdup (domain);
170                 handler->id = handler_id;
171                 
172                 priv->handlers = g_slist_prepend (priv->handlers, handler);
173         }
174 }