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