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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*
* Copyright (C) 2003-2005 by the gtk2-perl team (see the file AUTHORS for
* the full list)
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library 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 Library General Public
* License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA.
*
* $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Glib/GLog.xs,v 1.16 2006/03/04 17:17:30 kaffeetisch Exp $
*/
#include "gperl.h"
#include "gperl-private.h" /* for GPERL_SET_CONTEXT */
=head2 GLog
GLib has a message logging mechanism which it uses for the g_return_if_fail()
assertion macros, etc.; it's really versatile and allows you to set various
levels to be fatal and whatnot. Libraries use these for various types of
message reporting.
These functions let you reroute those messages from Perl. By default,
the warning, critical, and message levels go through perl's warn(), and
fatal ones go through croak(). [i'm not sure that these get to croak()
before GLib abort()s on them...]
=over
=cut
#if 0
/* Log level shift offset for user defined
* log levels (0-7 are used by GLib).
*/
#define G_LOG_LEVEL_USER_SHIFT (8)
/* GLib log levels that are considered fatal by default */
#define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
#endif
GType
g_log_level_flags_get_type (void)
{
static GType etype = 0;
if ( etype == 0 ) {
static const GFlagsValue values[] = {
{ G_LOG_FLAG_RECURSION, "G_LOG_FLAG_RECURSION", "recursion" },
{ G_LOG_FLAG_FATAL, "G_LOG_FLAG_FATAL", "fatal" },
{ G_LOG_LEVEL_ERROR, "G_LOG_LEVEL_ERROR", "error" },
{ G_LOG_LEVEL_CRITICAL, "G_LOG_LEVEL_CRITICAL", "critical" },
{ G_LOG_LEVEL_WARNING, "G_LOG_LEVEL_WARNING", "warning" },
{ G_LOG_LEVEL_MESSAGE, "G_LOG_LEVEL_MESSAGE", "message" },
{ G_LOG_LEVEL_INFO, "G_LOG_LEVEL_INFO", "info" },
{ G_LOG_LEVEL_DEBUG, "G_LOG_LEVEL_DEBUG", "debug" },
{ G_LOG_FATAL_MASK, "G_LOG_FATAL_MASK", "fatal-mask" },
{ 0, NULL, NULL }
};
etype = g_flags_register_static ("GLogLevelFlags", values);
}
return etype;
}
SV *
newSVGLogLevelFlags (GLogLevelFlags flags)
{
return gperl_convert_back_flags (g_log_level_flags_get_type (), flags);
}
GLogLevelFlags
SvGLogLevelFlags (SV * sv)
{
return gperl_convert_flags (g_log_level_flags_get_type (), sv);
}
static void
gperl_log_func (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
gperl_callback_invoke ((GPerlCallback *) user_data, NULL,
log_domain, log_level, message);
}
void
gperl_log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
char * desc;
gboolean in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
user_data = user_data; /* unused */
log_level &= G_LOG_LEVEL_MASK;
if (!message)
message = "(NULL) message";
switch (log_level) {
case G_LOG_LEVEL_CRITICAL: desc = "CRITICAL"; break;
case G_LOG_LEVEL_ERROR: desc = "ERROR"; break;
case G_LOG_LEVEL_WARNING: desc = "WARNING"; break;
case G_LOG_LEVEL_MESSAGE: desc = "Message"; break;
default: desc = "LOG";
}
GPERL_SET_CONTEXT;
warn ("%s%s%s %s**: %s",
(log_domain ? log_domain : ""),
(log_domain ? "-" : ""),
desc,
(in_recursion ? "(recursed) " : ""),
message);
/* the standard log handler calls abort() for G_LOG_LEVEL_ERROR
* messages. this is handy for being able to stop gdb on the
* error and get a backtrace. we originally mapped the error
* level stuff to croak(), but this broke the ability to find
* these errors in gdb, and didn't stop the script as expected
* in the perl debugger. so, let's preserve the GLib semantics. */
if (is_fatal)
/* XXX would be nice to get a perl backtrace here, but
* XXX Carp::cluck() doesn't print anything useful here. */
abort ();
}
#define ALL_LOGS (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)
=item gint gperl_handle_logs_for (const gchar * log_domain)
Route all g_logs for I<log_domain> through gperl's log handling. You'll
have to register domains in each binding submodule, because there's no way
we can know about them down here.
And, technically, this traps all the predefined log levels, not any of
the ones you (or your library) may define for yourself.
=cut
gint
gperl_handle_logs_for (const gchar * log_domain)
{
return g_log_set_handler (log_domain, ALL_LOGS,
gperl_log_handler, NULL);
}
=back
=cut
MODULE = Glib::Log PACKAGE = Glib::Log PREFIX = g_log_
=for object Glib::Log A flexible logging mechanism
=cut
BOOT:
gperl_handle_logs_for (NULL);
/* gperl_handle_logs_for ("main"); */
gperl_handle_logs_for ("GLib");
gperl_handle_logs_for ("GLib-GObject");
gperl_register_fundamental (g_log_level_flags_get_type (),
"Glib::LogLevelFlags");
=for flags Glib::LogLevelFlags
=cut
##
## Logging mechanism
##
##guint g_log_set_handler (const gchar *log_domain, GLogLevelFlags log_levels, GLogFunc log_func, gpointer user_data);
=for apidoc
=for arg log_domain name of the domain to handle with this callback.
=arg log_levels (GLogLevelFlags) log levels to handle with this callback
=arg log_func (subroutine) handler function
=cut
guint
g_log_set_handler (class, gchar_ornull * log_domain, SV * log_levels, SV * log_func, SV * user_data=NULL)
PREINIT:
GPerlCallback * callback;
GType param_types[3];
CODE:
param_types[0] = G_TYPE_STRING;
param_types[1] = g_log_level_flags_get_type ();
param_types[2] = G_TYPE_STRING;
callback = gperl_callback_new (log_func, user_data,
3, param_types, G_TYPE_NONE);
RETVAL = g_log_set_handler (log_domain,
SvGLogLevelFlags (log_levels),
gperl_log_func, callback);
/* we have no choice but to leak the callback. */
/* FIXME what about keeping a hash by the ID, and freeing it on
* Glib::Log->remove_handler ($id)? */
/*pcg: would probably take more memory in typical programs... */
OUTPUT:
RETVAL
##void g_log_remove_handler (const gchar *log_domain, guint handler_id);
=for apidoc
=for arg handler_id as returned by C<set_handler>
=cut
void
g_log_remove_handler (class, gchar_ornull *log_domain, guint handler_id);
C_ARGS:
log_domain, handler_id
##void g_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data);
# this is a little ugly, because i didn't want to export a typemap for
# GLogLevelFlags.
MODULE = Glib::Log PACKAGE = Glib PREFIX = g_
=for object Glib::Log
=cut
void g_log (class, gchar_ornull * log_domain, SV * log_level, const gchar *message)
CODE:
g_log (log_domain, SvGLogLevelFlags (log_level), message);
MODULE = Glib::Log PACKAGE = Glib::Log PREFIX = g_log_
SV * g_log_set_fatal_mask (class, const gchar *log_domain, SV * fatal_mask);
CODE:
RETVAL = newSVGLogLevelFlags
(g_log_set_fatal_mask (log_domain,
SvGLogLevelFlags (fatal_mask)));
OUTPUT:
RETVAL
SV * g_log_set_always_fatal (class, SV * fatal_mask);
CODE:
RETVAL = newSVGLogLevelFlags
(g_log_set_always_fatal (SvGLogLevelFlags (fatal_mask)));
OUTPUT:
RETVAL
##
## there are, indeed, some incidences in which it would be handy to have
## perl hooks into the g_log mechanism
##
##ifndef G_LOG_DOMAIN
##define G_LOG_DOMAIN ((gchar*) 0)
##endif /* G_LOG_DOMAIN */
MODULE = Glib::Log PACKAGE = Glib
=for object Glib::Log
=cut
###
### these are of dubious value, but i imagine that they could be useful...
###
##define g_error(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, __VA_ARGS__)
##define g_message(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
##define g_critical(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, __VA_ARGS__)
##define g_warning(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, __VA_ARGS__)
void
error (class, gchar_ornull * domain, const gchar * message)
ALIAS:
error = 0
message = 1
critical = 2
warning = 3
PREINIT:
GLogLevelFlags flags = G_LOG_LEVEL_MESSAGE;
CODE:
switch (ix) {
case 0: flags = G_LOG_LEVEL_ERROR; break;
case 1: flags = G_LOG_LEVEL_MESSAGE; break;
case 2: flags = G_LOG_LEVEL_CRITICAL; break;
case 3: flags = G_LOG_LEVEL_WARNING; break;
}
g_log (domain, flags, message);
##
## these are not needed -- perl's print() and warn() do the job.
##
## typedef void (*GPrintFunc) (const gchar *string);
## void g_print (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
## GPrintFunc g_set_print_handler (GPrintFunc func);
## void g_printerr (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
## GPrintFunc g_set_printerr_handler (GPrintFunc func);
##
##
## the assertion and return macros aren't really useful at all in perl;
## there are native perl replacements for them on CPAN.
##
##define g_assert(expr)
##define g_assert_not_reached()
##define g_return_if_fail(expr)
##define g_return_val_if_fail(expr,val)
##define g_return_if_reached()
##define g_return_val_if_reached(val)
|