Imported Upstream version 3.3.3
[debian/amanda] / perl / Amanda / Debug.swg
1 /*
2  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 %module "Amanda::Debug"
23 %include "amglue/amglue.swg"
24 %include "exception.i"
25
26 %include "Amanda/Debug.pod"
27
28 %{
29 #include <glib.h>
30 #include "debug.h"
31 %}
32
33 /*
34  * Initialization
35  */
36
37 amglue_export_tag(init,
38     debug_init dbopen dbreopen dbrename dbclose
39     $error_exit_status
40 );
41
42 void    debug_init(void);
43 void    dbopen(char *subdir);
44 void    dbreopen(char *file, char *notation);
45 void    dbrename(char *config, char *subdir);
46 void    dbclose(void);
47
48 int error_exit_status;
49
50 /*
51  * Override die() and warn()
52  */
53 %perlcode %{
54 sub _my_die {
55     # $^S: (from perlvar)
56     #  undef -> parsing module/eval
57     #  1 -> executing an eval
58     #  0 -> otherwise
59     # we *only* want to call critical() in the "otherwise" case
60     if (!defined($^S) or $^S == 1) {
61         die(@_);
62     } else {
63         my ($msg) = @_;
64         chomp $msg;
65         suppress_error_traceback();
66         critical(@_);
67     }
68 };
69 $SIG{__DIE__} = \&_my_die;
70
71 sub _my_warn {
72     my ($msg) = @_;
73     chomp $msg;
74     warning(@_);
75 };
76 $SIG{__WARN__} = \&_my_warn;
77
78 # utility function for test scripts, which want to use the regular
79 # perl mechanisms
80 sub disable_die_override {
81     delete $SIG{__DIE__};
82     delete $SIG{__WARN__};
83 }
84 %}
85
86 /*
87  * Logging
88  */
89
90 %rename(error) error__; /* error() is a macro defined in debug.h .. just avoid that */
91 %inline %{
92 void error__(char *msg) { g_error("%s", msg); }
93 void critical(char *msg) { g_critical("%s", msg); }
94 void warning(char *msg) { g_warning("%s", msg); }
95 void message(char *msg) { g_message("%s", msg); }
96 void info(char *msg) { g_info("%s", msg); }
97 void debug(char *msg) { g_debug("%s", msg); }
98 %}
99
100 amglue_export_tag(logging,
101     error critical warning message info debug
102 );
103
104 void add_amanda_log_handler(amanda_log_handler_t *handler);
105 /* these functions are written as simple global variables, since they are just
106  * function pointers used in add_amanda_log_handler.  Note that the functions
107  * then appear as e.g., $amanda_log_null. */
108 %immutable;
109 amanda_log_handler_t *amanda_log_stderr, *amanda_log_syslog, *amanda_log_null;
110 %mutable;
111
112 amglue_export_tag(logging,
113     add_amanda_log_handler
114     $amanda_log_stderr $amanda_log_syslog $amanda_log_null
115 );
116
117 /* used to suppress the traceback when calling from perl */
118 void suppress_error_traceback(void);
119
120 /*
121  * Advanced
122  */
123
124 int     dbfd(void);
125 char *  dbfn(void);
126 void debug_dup_stderr_to_debug(void);