Fixed 'xmlChar' signedness gcc(1) warnings.
[captive.git] / src / libcaptive / sandbox / server-GLogFunc.c
1 /* $Id$
2  * CORBA/ORBit server side of GLogFunc object, ran by sandbox_parent()
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program 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
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "server-GLogFunc.h"    /* self */
23 #include "sandbox.h"
24 #include "split.h"
25 #include <glib/gmessages.h>
26 #include "captive/macros.h"
27 #include <libxml/tree.h>
28 #include "../client/vfs-parent.h"
29
30
31 static void impl_Captive_GLogFunc_g_log_func
32                 (impl_POA_Captive_GLogFunc *servant,const Captive_GLogMessage *g_log_message,CORBA_Environment *ev);
33
34 static PortableServer_ServantBase__epv impl_Captive_GLogFunc_base_epv={
35         NULL,   /* _private data */
36         NULL,   /* finalize routine */
37         NULL,   /* default_POA routine */
38         };
39 static POA_Captive_GLogFunc__epv impl_Captive_GLogFunc_epv={
40         NULL,   /* _private */
41         (gpointer)&impl_Captive_GLogFunc_g_log_func,
42         };
43 static POA_Captive_GLogFunc__vepv impl_Captive_GLogFunc_vepv={
44         &impl_Captive_GLogFunc_base_epv,
45         &impl_Captive_GLogFunc_epv,
46         };
47
48
49 Captive_GLogFunc impl_Captive_GLogFunc__create
50                 (PortableServer_POA poa,CaptiveVfsParentObject *captive_vfs_parent_object,CORBA_Environment *ev)
51 {
52 Captive_GLogFunc retval;
53 impl_POA_Captive_GLogFunc *newservant;
54 PortableServer_ObjectId *objid;
55
56         captive_new0(newservant);       /* FIXME: leak */
57         newservant->servant.vepv=&impl_Captive_GLogFunc_vepv;
58         newservant->poa=poa;
59         newservant->captive_vfs_parent_object=captive_vfs_parent_object;
60         POA_Captive_GLogFunc__init((PortableServer_Servant)newservant,ev);
61         objid=PortableServer_POA_activate_object(poa,newservant,ev);
62         CORBA_free(objid);
63         retval=PortableServer_POA_servant_to_reference(poa,newservant,ev);
64
65         return retval;
66 }
67
68
69 void impl_Captive_GLogFunc__destroy(impl_POA_Captive_GLogFunc *servant,CORBA_Environment *ev)
70 {
71 PortableServer_ObjectId *objid;
72
73         objid=PortableServer_POA_servant_to_id(servant->poa,servant,ev);
74         PortableServer_POA_deactivate_object(servant->poa,objid,ev);
75         CORBA_free(objid);
76         POA_Captive_GLogFunc__fini((PortableServer_Servant)servant,ev);
77         g_free(servant);
78 }
79
80
81 static void impl_Captive_GLogFunc_g_log_func
82                 (impl_POA_Captive_GLogFunc *servant,const Captive_GLogMessage *g_log_message,CORBA_Environment *ev)
83 {
84 GLogLevelFlags log_level_use;
85 static const struct level_map {
86         GLogLevelFlags from,to;
87         const gchar *name;
88         } level_map[]={
89                 /* Downgrade always-fatal 'ERROR' to 'CRITICAL' level */
90                 { G_LOG_LEVEL_ERROR   ,G_LOG_LEVEL_CRITICAL,"error"    },
91                 { G_LOG_LEVEL_CRITICAL,G_LOG_LEVEL_CRITICAL,"critical" },
92                 { G_LOG_LEVEL_WARNING ,G_LOG_LEVEL_WARNING ,"warning"  },
93                 { G_LOG_LEVEL_MESSAGE ,G_LOG_LEVEL_MESSAGE ,"message"  },
94                 { G_LOG_LEVEL_INFO    ,G_LOG_LEVEL_INFO    ,"info"     },
95                 { G_LOG_LEVEL_DEBUG   ,G_LOG_LEVEL_DEBUG   ,"debug"    },
96                 };
97 static const struct flag_map {
98         GLogLevelFlags from,to;
99         const gchar *name;
100         } flag_map[]={
101                 { G_LOG_FLAG_RECURSION,G_LOG_FLAG_RECURSION,"recursion" },
102                 { G_LOG_FLAG_FATAL    ,0                   ,"fatal"     },
103                 };
104 const struct level_map *levelp;
105 const struct flag_map *flagp;
106
107         for (levelp=level_map;levelp<level_map+G_N_ELEMENTS(level_map);levelp++) {
108                 log_level_use=g_log_message->log_level;
109                 if (!(g_log_message->log_level & levelp->from))
110                         continue;
111                 {
112                 xmlNode *xml_message=NULL;
113
114                         if (servant->captive_vfs_parent_object->corba_bug_log) {
115                                 xml_message=xmlNewTextChild(servant->captive_vfs_parent_object->corba_bug_log,NULL,BAD_CAST "message",BAD_CAST g_log_message->message);
116                                         xmlNewProp(xml_message,BAD_CAST "level",BAD_CAST levelp->name);
117                                 }
118
119                         log_level_use&=~levelp->from;
120                         log_level_use|= levelp->to;
121                         for (flagp=flag_map;flagp<flag_map+G_N_ELEMENTS(flag_map);flagp++) {
122                                 if (!(g_log_message->log_level & flagp->from))
123                                         continue;
124                                 log_level_use&=~flagp->from;
125                                 log_level_use|= flagp->to;
126                                 if (xml_message)
127                                         xmlNewProp(xml_message,BAD_CAST flagp->name,BAD_CAST flagp->name);
128                                 }
129                         }
130                 g_log(G_LOG_DOMAIN,log_level_use,"sandbox: %s%s",
131                                 (!(g_log_message->log_level & G_LOG_FLAG_FATAL) ? "" : "FATAL: "),
132                                 g_log_message->message);
133                 }
134 }
135
136
137 static void impl_Captive_Vfs_init_g_log_func_log_func
138                 (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,const Captive_GLogFunc obj /* user_data */)
139 {
140 CORBA_Environment ev;
141 Captive_GLogMessage g_log_message;
142
143         CORBA_exception_init(&ev);
144         g_log_message.log_level=log_level;
145         g_log_message.message=(CORBA_string)message;
146         Captive_GLogFunc_g_log_func(obj,&g_log_message,&ev);
147         g_assert(validate_CORBA_Environment(&ev));
148         CORBA_exception_free(&ev);
149 }
150
151
152 static gboolean impl_Captive_Vfs_init_g_log_func_handled=FALSE;
153 static guint impl_Captive_Vfs_init_g_log_func_handler_id;
154 static Captive_GLogFunc g_log_func_copy;
155
156
157 /* We MUST NOT touch 'captive_options' as it may not be initialized yet
158  */
159 void impl_Captive_Vfs_init_g_log_func(const Captive_GLogFunc g_log_func,gboolean debug_messages,CORBA_Environment *ev)
160 {
161         g_assert(impl_Captive_Vfs_init_g_log_func_handled==FALSE);
162
163         g_log_func_copy=CORBA_Object_duplicate(g_log_func,ev);
164         if (ev->_major!=CORBA_NO_EXCEPTION)
165                 return;
166
167         impl_Captive_Vfs_init_g_log_func_handler_id=g_log_set_handler(
168                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
169                         0       /* log_levels */
170                                         | 0     /* !G_LOG_FLAG_RECURSION */
171                                         | G_LOG_FLAG_FATAL
172                                         | (G_LOG_LEVEL_MASK & ~(debug_messages ? 0 : 0
173                                                         /* The same mask is in:
174                                                          * libcaptive/sandbox/server-GLogFunc.c
175                                                          * libcaptive/client/init.c
176                                                          */
177                                                         | G_LOG_LEVEL_MESSAGE
178                                                         | G_LOG_LEVEL_INFO
179                                                         | G_LOG_LEVEL_DEBUG)),
180                         (GLogFunc)impl_Captive_Vfs_init_g_log_func_log_func,    /* log_func */
181                         (gpointer)g_log_func_copy);     /* user_data */
182
183         impl_Captive_Vfs_init_g_log_func_handled=TRUE;
184 }
185
186
187 /* Called during child shutdown where our CORBA peer is no longer responding. */
188 void impl_Captive_Vfs_init_g_log_func_disable(void)
189 {
190 CORBA_Environment ev;
191
192         if (!impl_Captive_Vfs_init_g_log_func_handled)
193                 return;
194
195         g_log_remove_handler(
196                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
197                         impl_Captive_Vfs_init_g_log_func_handler_id);   /* handler_id */
198         CORBA_exception_init(&ev);
199         CORBA_Object_release(g_log_func_copy,&ev);
200         g_assert(validate_CORBA_Environment(&ev));
201         CORBA_exception_free(&ev);
202
203         impl_Captive_Vfs_init_g_log_func_handled=FALSE;
204 }