Cosmetic: Comment updated.
[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
28
29 static void impl_Captive_GLogFunc_g_log_func
30                 (impl_POA_Captive_GLogFunc *servant,const Captive_GLogMessage *g_log_message,CORBA_Environment *ev);
31
32 static PortableServer_ServantBase__epv impl_Captive_GLogFunc_base_epv={
33         NULL,   /* _private data */
34         NULL,   /* finalize routine */
35         NULL,   /* default_POA routine */
36         };
37 static POA_Captive_GLogFunc__epv impl_Captive_GLogFunc_epv={
38         NULL,   /* _private */
39         (gpointer)&impl_Captive_GLogFunc_g_log_func,
40         };
41 static POA_Captive_GLogFunc__vepv impl_Captive_GLogFunc_vepv={
42         &impl_Captive_GLogFunc_base_epv,
43         &impl_Captive_GLogFunc_epv,
44         };
45
46
47 Captive_GLogFunc impl_Captive_GLogFunc__create(PortableServer_POA poa,CORBA_Environment *ev)
48 {
49 Captive_GLogFunc retval;
50 impl_POA_Captive_GLogFunc *newservant;
51 PortableServer_ObjectId *objid;
52
53         captive_new0(newservant);       /* FIXME: leak */
54         newservant->servant.vepv=&impl_Captive_GLogFunc_vepv;
55         newservant->poa=poa;
56         POA_Captive_GLogFunc__init((PortableServer_Servant)newservant,ev);
57         objid=PortableServer_POA_activate_object(poa,newservant,ev);
58         CORBA_free(objid);
59         retval=PortableServer_POA_servant_to_reference(poa,newservant,ev);
60
61         return retval;
62 }
63
64
65 void impl_Captive_GLogFunc__destroy(impl_POA_Captive_GLogFunc *servant,CORBA_Environment *ev)
66 {
67 PortableServer_ObjectId *objid;
68
69         objid=PortableServer_POA_servant_to_id(servant->poa,servant,ev);
70         PortableServer_POA_deactivate_object(servant->poa,objid,ev);
71         CORBA_free(objid);
72         POA_Captive_GLogFunc__fini((PortableServer_Servant)servant,ev);
73         g_free(servant);
74 }
75
76
77 static void impl_Captive_GLogFunc_g_log_func
78                 (impl_POA_Captive_GLogFunc *servant,const Captive_GLogMessage *g_log_message,CORBA_Environment *ev)
79 {
80 GLogLevelFlags log_level_use;
81 gboolean fatal=FALSE;
82
83         log_level_use=g_log_message->log_level;
84         /* Downgrade always-fatal 'ERROR' to 'CRITICAL' level */
85         if (log_level_use&G_LOG_LEVEL_ERROR) {
86                 log_level_use=(log_level_use|G_LOG_LEVEL_CRITICAL)&~G_LOG_LEVEL_ERROR;
87                 fatal=TRUE;
88                 }
89         if (log_level_use&G_LOG_FLAG_FATAL) {
90                 log_level_use&=~G_LOG_FLAG_FATAL;
91                 fatal=TRUE;
92                 }
93
94         g_log(G_LOG_DOMAIN,log_level_use,"sandbox: %s%s",(!fatal ? "" : "FATAL: "),g_log_message->message);
95 }
96
97
98 static void impl_Captive_Vfs_init_g_log_func_log_func
99                 (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,const Captive_GLogFunc obj /* user_data */)
100 {
101 CORBA_Environment ev;
102 Captive_GLogMessage g_log_message;
103
104         CORBA_exception_init(&ev);
105         g_log_message.log_level=log_level;
106         g_log_message.message=(CORBA_string)message;
107         Captive_GLogFunc_g_log_func(obj,&g_log_message,&ev);
108         g_assert(validate_CORBA_Environment(&ev));
109         CORBA_exception_free(&ev);
110 }
111
112
113 static gboolean impl_Captive_Vfs_init_g_log_func_handled=FALSE;
114 static guint impl_Captive_Vfs_init_g_log_func_handler_id;
115 static Captive_GLogFunc g_log_func_copy;
116
117
118 /* We MUST NOT touch 'captive_options' as it may not be initialized yet
119  */
120 void impl_Captive_Vfs_init_g_log_func(const Captive_GLogFunc g_log_func,gboolean debug_messages,CORBA_Environment *ev)
121 {
122         g_assert(impl_Captive_Vfs_init_g_log_func_handled==FALSE);
123
124         g_log_func_copy=CORBA_Object_duplicate(g_log_func,ev);
125         if (ev->_major!=CORBA_NO_EXCEPTION)
126                 return;
127
128         impl_Captive_Vfs_init_g_log_func_handler_id=g_log_set_handler(
129                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
130                         0       /* log_levels */
131                                         | 0     /* !G_LOG_FLAG_RECURSION */
132                                         | G_LOG_FLAG_FATAL
133                                         | (G_LOG_LEVEL_MASK & ~(debug_messages ? 0 : 0
134                                                         /* The same mask is in:
135                                                          * libcaptive/sandbox/server-GLogFunc.c
136                                                          * libcaptive/client/init.c
137                                                          * libcaptive/client/vfs.c
138                                                          */
139                                                         | G_LOG_LEVEL_MESSAGE
140                                                         | G_LOG_LEVEL_INFO
141                                                         | G_LOG_LEVEL_DEBUG)),
142                         (GLogFunc)impl_Captive_Vfs_init_g_log_func_log_func,    /* log_func */
143                         (gpointer)g_log_func_copy);     /* user_data */
144
145         impl_Captive_Vfs_init_g_log_func_handled=TRUE;
146 }
147
148
149 /* Called during child shutdown where our CORBA peer is no longer responding. */
150 void impl_Captive_Vfs_init_g_log_func_disable(void)
151 {
152 CORBA_Environment ev;
153
154         if (!impl_Captive_Vfs_init_g_log_func_handled)
155                 return;
156
157         g_log_remove_handler(
158                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
159                         impl_Captive_Vfs_init_g_log_func_handler_id);   /* handler_id */
160         CORBA_exception_init(&ev);
161         CORBA_Object_release(g_log_func_copy,&ev);
162         g_assert(validate_CORBA_Environment(&ev));
163         CORBA_exception_free(&ev);
164
165         impl_Captive_Vfs_init_g_log_func_handled=FALSE;
166 }