Imported Upstream version 3.1.0
[debian/amanda] / common-src / debug.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: debug.h 6789 2007-06-18 20:18:52Z dustin $
28  *
29  * Logging support
30  */
31
32 /* this file is included from amanda.h; there is no need to include
33  * it explicitly in source files. */
34
35 #ifndef AMANDA_DEBUG_H
36 #define AMANDA_DEBUG_H
37
38 /*
39  * GENERAL LOGGING
40  */
41
42 /* Amanda uses glib's logging facilities.  See
43  *  http://developer.gnome.org/doc/API/2.2/glib/glib-Message-Logging.html
44  *
45  * Note that log output will go to stderr until debug_open is called.
46  *
47  * The error levels are assigned as follows:
48  *  g_error -- errors that should dump core (will not return)
49  *  g_critical -- fatal errors, exiting with exit status in 
50  *    error_exit_status() (will not return)
51  *  g_warning -- non-fatal problems
52  *  g_message -- normal status information
53  *  g_info -- helpful extra details, but not verbose
54  *  g_debug -- debug messages
55  */
56
57 /* g_debug was introduced in glib 2.6, so define it here for systems where
58  * it is lacking.  g_info doesn't exist even in glib 2.13, but maybe it will
59  * be invented soon..
60  */
61
62 #ifndef g_debug
63 #define g_debug(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, __VA_ARGS__)
64 #endif
65
66 #ifndef g_info
67 #define g_info(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
68 #endif
69
70 /* Initialize the debugging interface.  This is the "high-level"
71  * initialization function; older and lower-level applications can call
72  * dbopen() and friends directly.
73  *
74  * This function sets up debug logging and error-handling according to
75  * the current process name, type, and context, as defined in util.
76  */
77 void debug_init(void);
78
79 /*
80  * ADDITIONAL LOGGING
81  */
82
83 /* Amanda implements a rudimentary method of distributing log information to
84  * arbitrary consumers.  Some consumers are available here, and other modules
85  * (e.g., server-src/logfile.c) may implement them, too.
86  *
87  * If amanda_log_handler has never been called, then the default disposition
88  * depends on the application context: amanda_log_stderr is always used, and
89  * amanda_log_syslog is used in the 'daemon' context.  The 'scriptutil' context
90  * does not do any debug logging.
91  */
92
93 /* prototype for log-handling functions; these will be called with only a single
94  * bit set in the log_level. */
95 typedef void (amanda_log_handler_t)(GLogLevelFlags log_level, const gchar *message);
96
97 /* add an amanda_log_handler_t to the list of handlers */
98 void add_amanda_log_handler(amanda_log_handler_t *hdlr);
99
100 /* log ERROR, CRITICAL, and WARNING messages to syslog */
101 void amanda_log_syslog(GLogLevelFlags log_level, const gchar *message);
102
103 /* log ERROR and CRITICAL to stderr */
104 void amanda_log_stderr(GLogLevelFlags log_level, const gchar *message);
105
106 /* log nothing */
107 void amanda_log_null(GLogLevelFlags log_level, const gchar *message);
108
109 /*
110  * FATAL ERROR HANDLING
111  */
112
113 /* for compatibility; these should eventually be substituted throughout
114  * the codebase.  Extra calls to exit() and abort() should be optimized
115  * away, and are there only for stupid compilers. */
116 #define errordump(...) do { g_error(__VA_ARGS__); abort(); } while (0)
117 #define error(...) do { g_critical(__VA_ARGS__); exit(error_exit_status); } while (0)
118
119 /* The process exit status that will be given when error()
120  * or errordump() is called.
121  */
122 extern int error_exit_status;
123
124 /*
125  * DEBUG LOGGING
126  */
127
128 /* short names */
129 #define dbopen(a)       debug_open(a)
130 #define dbreopen(a,b)   debug_reopen(a,b)
131 #define dbrename(a,b)   debug_rename(a,b)
132 #define dbclose()       debug_close()
133 #define dbprintf        debug_printf
134 #define dbfd()          debug_fd()
135 #define dbfp()          debug_fp()
136 #define dbfn()          debug_fn()
137
138 /* constants for db(re)open */
139 #define DBG_SUBDIR_SERVER  "server"
140 #define DBG_SUBDIR_CLIENT  "client"
141 #define DBG_SUBDIR_AMANDAD "amandad"
142
143 /* Open the debugging log in the given subdirectory.  Once 
144  * this function is called, debug logging is available.
145  *
146  * The debugging file is created in the given subdirectory of the
147  * amanda debugging directory, with a filename based on the current
148  * process name (from get_pname).
149  *
150  * @param subdir: subdirectory in which to create the debug file.
151  * This is usually one of the DBG_SUBDIR_* constants.  
152  */
153 void    debug_open(char *subdir);
154
155 /* Re-open a previously debug_close()d debug file, given by 
156  * filename, optionally adding a notation as to why it was
157  * reopened.
158  *
159  * @param file: the filename of the debug file to reopen
160  * @param notation: reason for re-opening the file
161  */
162 void    debug_reopen(char *file, char *notation);
163
164 /* Rename the debugging logfile into a configuration-specific subdirectory
165  * of SUBDIR.  Any existing content of the file will be preserved.
166  *
167  * @param config: configuration name
168  * @param subdir: subdirectory in which to create the debug file.
169  */
170 void    debug_rename(char *config, char *subdir);
171
172 /* Flush and close the debugging logfile.  Call this function at application
173  * shutdown.
174  */
175 void    debug_close(void);
176
177 /* Add a message to the debugging logfile.  A newline is not automatically 
178  * added.
179  *
180  * This function is deprecated in favor of glib's g_debug().
181  */
182 void    debug_printf(const char *format, ...) G_GNUC_PRINTF(1,2);
183
184 /* Get the file descriptor for the debug file
185  *
186  * @returns: the file descriptor
187  */
188 int     debug_fd(void);
189
190 /* Get the stdio file handle for the debug file.
191  *
192  * @returns: the file handle
193  */
194 FILE *  debug_fp(void);
195
196 /* Get the pathname of the debug file.
197  *
198  * The result should not be freed by the caller.
199  *
200  * @returns: the pathname
201  */
202 char *  debug_fn(void);
203
204 /* Use 'dup2' to send stderr output to the debug file.  This is useful
205  * when launching other applications, where the stderr of those applications
206  * may be necessary for debugging.  It should be called in the child, after
207  * the fork().
208  */
209 void debug_dup_stderr_to_debug(void);
210
211 /* error() and critical() will print a C stack trace if possible.  Set this to
212  * TRUE to avoid this stack trace.  This is used by perl wrappers, for example */
213 void suppress_error_traceback(void);
214
215 /*
216  * PROCESS NAME
217  */
218
219 /*
220  * ASSERTIONS
221  */
222
223 #ifndef SWIG
224 #ifdef ASSERTIONS
225
226 /* Like the standard assert(), but call g_error() to log the result properly */
227 #define assert(exp)     do {                                            \
228     if (!(exp)) {                                                       \
229         g_error(_("assert: %s is false: file %s, line %d"),             \
230            stringize(exp), __FILE__, __LINE__);                         \
231         g_assert_not_reached();                                         \
232     }                                                                   \
233 } while (0)
234
235 #else   /* ASSERTIONS */
236
237 #define assert(exp) ((void)0)
238
239 #endif  /* ASSERTIONS */
240 #endif  /* SWIG */
241
242 #endif /* AMANDA_DEBUG_H */