13894be7676b961d14fb8900a5aa432e4e3b71da
[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  * g_error and g_critical will respect erroutput_type, or if that has not
57  * been set explicitly, the current process context (get_pcontext). This can
58  * mean sending the error to the Amanda logfile for this run (see logfile.c).
59  */
60
61 /* g_debug was introduced in glib 2.6, so define it here for systems where
62  * it is lacking.  g_info doesn't exist even in glib 2.13, but maybe it will
63  * be invented soon..
64  */
65
66 #ifndef g_debug
67 #define g_debug(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, __VA_ARGS__)
68 #endif
69
70 #ifndef g_info
71 #define g_info(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__)
72 #endif
73
74 /* Initialize the debugging interface.  This is the "high-level"
75  * initialization function; older and lower-level applications can call
76  * dbopen() and friends directly.
77  *
78  * This function sets up debug logging and error-handling according to
79  * the current process name, type, and context, as defined in util.
80  */
81 void debug_init(void);
82
83 /*
84  * FATAL ERROR HANDLING
85  */
86
87 /* for compatibility; these should eventually be substituted throughout
88  * the codebase.  Extra calls to exit() and abort() should be optimized
89  * away, and are there only for stupid compilers. */
90 #define errordump(...) do { g_error(__VA_ARGS__); abort(); } while (0)
91 #define error(...) do { g_critical(__VA_ARGS__); exit(error_exit_status); } while (0)
92
93 /* Additional handling for error and critical messages.  Leave this
94  * set to its default if you're using debug_init(). */
95 typedef enum {
96     /* send message to stderr (for interactive programs) */
97     ERR_INTERACTIVE     = 1 << 0, /* (default) */
98
99     /* log to syslog */
100     ERR_SYSLOG          = 1 << 1,
101
102     /* add an L_FATAL entry in the Amanda logfile for the 
103      * current run */
104     ERR_AMANDALOG       = 1 << 2,
105
106     /* the default situation -- do whatever's appropriate for
107      * the current context.  If this is set, the other flags
108      * are ignored. */
109     ERR_FROM_CONTEXT    = 1 << 3,
110 } erroutput_type_t;
111 extern erroutput_type_t erroutput_type;
112
113 /* The process exit status that will be given when error()
114  * or errordump() is called.
115  */
116 extern int error_exit_status;
117
118 /* Supply a pointer to the logfile module's logerror(), if
119  * ERR_AMANDALOG is set.
120  *
121  * This function is required because libamanda, which contains
122  * debug.c, is not always linked with the logerror module
123  * (which only appears in server applications).
124  *
125  * @param logerror_fn: function pointer
126  */
127 void set_logerror(void (*logerror_fn)(char *));
128
129 /*
130  * DEBUG LOGGING
131  */
132
133 /* short names */
134 #define dbopen(a)       debug_open(a)
135 #define dbreopen(a,b)   debug_reopen(a,b)
136 #define dbrename(a,b)   debug_rename(a,b)
137 #define dbclose()       debug_close()
138 #define dbprintf        debug_printf
139 #define dbfd()          debug_fd()
140 #define dbfp()          debug_fp()
141 #define dbfn()          debug_fn()
142
143 /* constants for db(re)open */
144 #define DBG_SUBDIR_SERVER  "server"
145 #define DBG_SUBDIR_CLIENT  "client"
146 #define DBG_SUBDIR_AMANDAD "amandad"
147
148 /* Open the debugging log in the given subdirectory.  Once 
149  * this function is called, debug logging is available.
150  *
151  * The debugging file is created in the given subdirectory of the
152  * amanda debugging directory, with a filename based on the current
153  * process name (from get_pname).
154  *
155  * @param subdir: subdirectory in which to create the debug file.
156  * This is usually one of the DBG_SUBDIR_* constants.  
157  */
158 void    debug_open(char *subdir);
159
160 /* Re-open a previously debug_close()d debug file, given by 
161  * filename, optionally adding a notation as to why it was
162  * reopened.
163  *
164  * @param file: the filename of the debug file to reopen
165  * @param notation: reason for re-opening the file
166  */
167 void    debug_reopen(char *file, char *notation);
168
169 /* Rename the debugging logfile into a configuration-specific subdirectory
170  * of SUBDIR.  Any existing content of the file will be preserved.
171  *
172  * @param config: configuration name
173  * @param subdir: subdirectory in which to create the debug file.
174  */
175 void    debug_rename(char *config, char *subdir);
176
177 /* Flush and close the debugging logfile.  Call this function at application
178  * shutdown.
179  */
180 void    debug_close(void);
181
182 /* Add a message to the debugging logfile.  A newline is not automatically 
183  * added.
184  *
185  * This function is deprecated in favor of glib's g_debug().
186  */
187 void    debug_printf(const char *format, ...) G_GNUC_PRINTF(1,2);
188
189 /* Get the file descriptor for the debug file
190  *
191  * @returns: the file descriptor
192  */
193 int     debug_fd(void);
194
195 /* Get the stdio file handle for the debug file.
196  *
197  * @returns: the file handle
198  */
199 FILE *  debug_fp(void);
200
201 /* Get the pathname of the debug file.
202  *
203  * The result should not be freed by the caller.
204  *
205  * @returns: the pathname
206  */
207 char *  debug_fn(void);
208
209 /* Use 'dup2' to send stderr output to the debug file.  This is useful
210  * when launching other applications, where the stderr of those applications
211  * may be necessary for debugging.  It should be called in the child, after
212  * the fork().
213  */
214 void debug_dup_stderr_to_debug(void);
215
216 /*
217  * PROCESS NAME
218  */
219
220 /*
221  * ASSERTIONS
222  */
223
224 #ifndef SWIG
225 #ifdef ASSERTIONS
226
227 /* Like the standard assert(), but call g_error() to log the result properly */
228 #define assert(exp)     do {                                            \
229     if (!(exp)) {                                                       \
230         g_error(_("assert: %s is false: file %s, line %d"),             \
231            stringize(exp), __FILE__, __LINE__);                         \
232         g_assert_not_reached();                                         \
233     }                                                                   \
234 } while (0)
235
236 #else   /* ASSERTIONS */
237
238 #define assert(exp) ((void)0)
239
240 #endif  /* ASSERTIONS */
241 #endif  /* SWIG */
242
243 #endif /* AMANDA_DEBUG_H */