| 12
 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
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 
 | /*
 * Copyright (C) 2003-2005, 2009, 2012-2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * $Id$
 */
#include "gperl.h"
#include "gperl-gtypes.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
SV *
newSVGLogLevelFlags (GLogLevelFlags flags)
{
	return gperl_convert_back_flags (GPERL_TYPE_LOG_LEVEL_FLAGS, flags);
}
GLogLevelFlags
SvGLogLevelFlags (SV * sv)
{
	return gperl_convert_flags (GPERL_TYPE_LOG_LEVEL_FLAGS, sv);
}
/* for GLogFunc style, to be invoked by gperl_log_func() below */
static GPerlCallback *
gperl_log_callback_new (SV *log_func, SV *user_data)
{
	GType param_types[3];
	param_types[0] = G_TYPE_STRING;
	param_types[1] = GPERL_TYPE_LOG_LEVEL_FLAGS;
	param_types[2] = G_TYPE_STRING;
	return gperl_callback_new (log_func, user_data,
				   3, param_types, G_TYPE_NONE);
}
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);
}
#if GLIB_CHECK_VERSION (2, 6, 0)
/* the GPerlCallback currently installed through
   g_log_set_default_handler(), or NULL if no such */
static GPerlCallback *gperl_log_default_handler_callback = NULL;
G_LOCK_DEFINE_STATIC (gperl_log_default_handler_callback);
#endif
void
gperl_log_handler (const gchar   *log_domain,
                   GLogLevelFlags log_level,
                   const gchar   *message,
                   gpointer       user_data)
{
        char *desc;
        char *env;
	gboolean in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
	gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
	PERL_UNUSED_VAR (user_data);
	log_level &= G_LOG_LEVEL_MASK;
	if (!message)
		message = "(NULL) message";
	switch (log_level) {
                case G_LOG_LEVEL_ERROR:    desc = "ERROR";    break;
                case G_LOG_LEVEL_CRITICAL: desc = "CRITICAL"; break;
                case G_LOG_LEVEL_WARNING:  desc = "WARNING";  break;
                case G_LOG_LEVEL_MESSAGE:  desc = "Message";  break;
                case G_LOG_LEVEL_INFO:     desc = "INFO";     break;
                case G_LOG_LEVEL_DEBUG:    desc = "DEBUG";    break;
                default: desc = "LOG";
	}
        /* GLib will automatically skip debug messages unless the
         * G_MESSAGES_DEBUG environment variable is set to either
         * "all" or a colon-separated list of log domains that include
         * the domain used for the message.
         */
        if (log_level & (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)) {
                const char *env = g_getenv ("G_MESSAGES_DEBUG");
                if (env == NULL)
                        return;
                if (strcmp (env, "all") != 0 &&
                    (log_domain == NULL || strstr (env, log_domain) == NULL)) {
                        return;
                }
        }
	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 (GPERL_TYPE_LOG_LEVEL_FLAGS,
	                            "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
$log_func will be called as
    &$log_func ($log_domain, $log_levels, $message, $user_data);
where $log_domain is the name requested and $log_levels is a
Glib::LogLevelFlags of level and flags being reported.
=cut
guint
g_log_set_handler (class, gchar_ornull * log_domain, SV * log_levels, SV * log_func, SV * user_data=NULL)
    PREINIT:
	GPerlCallback * callback;
    CODE:
	callback = gperl_log_callback_new (log_func, user_data);
	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
=for apidoc __function__
=for signature Glib::Log::default_handler ($log_domain, $log_level, $message, ...)
=for arg ... possible "userdata" argument ignored
The arguments are the same as taken by the function for set_handler or
set_default_handler.
=cut
void g_log_default_handler (const gchar *log_domain, SV *log_level, const gchar *message, ...);
    CODE:
	g_log_default_handler (log_domain, SvGLogLevelFlags(log_level),
			       message, NULL);
#if GLIB_CHECK_VERSION (2, 6, 0)
##GLogFunc g_log_set_default_handler (GLogFunc log_func, gpointer user_data);
=for apidoc
=for signature prev_log_func = Glib::Log->set_default_handler ($log_func, $user_data)
=arg log_func (subroutine) handler function or undef
Install log_func as the default log handler.  log_func is called for
anything which doesn't otherwise have a handler (either
Glib::Log->set_handler, or the L<Glib::xsapi|Glib::xsapi>
gperl_handle_logs_for),
    &$log_func ($log_domain, $log_levels, $message, $user_data)
where $log_domain is a string, and $log_levels is a
Glib::LogLevelFlags of level and flags being reported.
If log_func is \&Glib::Log::default_handler or undef then Glib's
default handler is set.
The return value from C<set_default_handler> is the previous handler.
This is \&Glib::Log::default_handler for Glib's default, otherwise a
Perl function previously installed.  If the handler is some other
non-Perl function then currently the return is undef, but perhaps that
will change to some wrapped thing, except that without associated
userdata there's very little which could be done with it (it couldn't
be reinstalled later without its userdata).
=cut
SV *
g_log_set_default_handler (class, SV * log_func, SV * user_data=NULL)
    PREINIT:
	GLogFunc new_func = &g_log_default_handler;
	GLogFunc old_func;
	GPerlCallback *new_callback = NULL;
	GPerlCallback *old_callback;
    CODE:
	if (gperl_sv_is_defined (log_func)) {
		/* check for log_func == \&Glib::Log::default_handler and
		 * turn that into g_log_default_handler() directly, rather
		 * than making a callback into perl and out again.  This is
		 * mainly an optimization, but if something weird has
		 * happened then the direct C function will be much more
		 * likely to work.
		 */
		HV *st;
		GV *gv;
		CV *cv = sv_2cv(log_func, &st, &gv, 0);
		if (cv && CvXSUB(cv) == XS_Glib__Log_default_handler) {
			/* new_func already initialized to
			 * g_log_default_handler above
                         */
		} else {
			new_func = gperl_log_func;
			new_callback = gperl_log_callback_new
				(log_func, user_data);
		}
	}
	G_LOCK (gperl_log_default_handler_callback);
	old_func = g_log_set_default_handler (new_func, new_callback);
	old_callback = gperl_log_default_handler_callback;
	gperl_log_default_handler_callback = new_callback;
	G_UNLOCK (gperl_log_default_handler_callback);
	RETVAL = &PL_sv_undef;
	if (old_func == g_log_default_handler) {
		CV *cv = get_cv ("Glib::Log::default_handler", 0);
		assert (cv);
		RETVAL = newRV_inc ((SV*) cv);
		SvREFCNT_inc (RETVAL);
	} else if (old_func == gperl_log_func) {
		RETVAL = old_callback->func;
		SvREFCNT_inc (RETVAL);
	}
	if (old_callback) {
		gperl_callback_destroy (old_callback);
	}
    OUTPUT:
	RETVAL
#endif
# 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), "%s", 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__)
##define g_info(...)     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
##define g_debug(...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, __VA_ARGS__)
void
error (class, gchar_ornull * domain, const gchar * message)
    ALIAS:
        error = 0
        critical = 1
        warning = 2
        message = 3
        info = 4
        debug = 5
    PREINIT:
	GLogLevelFlags flags = G_LOG_LEVEL_MESSAGE;
    CODE:
	switch (ix) {
                case 0: flags = G_LOG_LEVEL_ERROR; break;
                case 1: flags = G_LOG_LEVEL_CRITICAL; break;
                case 2: flags = G_LOG_LEVEL_WARNING; break;
                case 3: flags = G_LOG_LEVEL_MESSAGE; break;
                case 4: flags = G_LOG_LEVEL_INFO; break;
                case 5: flags = G_LOG_LEVEL_DEBUG; break;
	}
	g_log (domain, flags, "%s", 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)
 |