2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
3
Copyright (C) 2005- The University of Notre Dame
4
This software is distributed under the GNU General Public License.
5
See the file COPYING for details.
9
#include "domain_name_cache.h"
11
#include "stringtools.h"
32
static int debug_fd = 2;
33
static char *debug_file = 0;
34
static int debug_file_size = 10485760;
35
static const char *program_name = "";
36
static INT64_T debug_flags = D_NOTICE;
37
static pid_t (*debug_getpid)() = getpid;
44
static struct flag_info table[] = {
45
{ "syscall", D_SYSCALL },
46
{ "notice", D_NOTICE },
47
{ "channel", D_CHANNEL },
48
{ "process", D_PROCESS },
49
{ "resolve", D_RESOLVE },
50
{ "libcall", D_LIBCALL },
59
{ "landlord", D_LANDLORD },
67
{ "pstree", D_PSTREE },
68
{ "alloc" , D_ALLOC },
72
{ "bxgrid", D_BXGRID },
78
{ "xrootd", D_XROOTD},
79
{ "remote", D_REMOTE },
81
{ "time", 0 }, /* backwards compatibility */
82
{ "pid", 0 }, /* backwards compatibility */
86
struct fatal_callback {
88
struct fatal_callback *next;
91
struct fatal_callback * fatal_callback_list = 0;
94
int debug_flags_set( const char *flagname )
98
for(i=table;i->name;i++) {
99
if(!strcmp(flagname,i->name)) {
100
debug_flags |= i->flag;
108
void debug_flags_print( FILE *stream )
112
for(i=0;table[i].name;i++) {
113
fprintf(stream,"%s ",table[i].name);
117
void debug_set_flag_name( INT64_T flag, const char *name )
121
for(i=table;i->name;i++) {
129
static const char * flag_to_name( INT64_T flag )
133
for(i=table;i->name;i++) {
134
if(i->flag&flag) return i->name;
140
static void do_debug( int is_fatal, INT64_T flags, const char *fmt, va_list args )
150
tm = localtime(&tv.tv_sec);
152
sprintf(newfmt,"%04d/%02d/%02d %02d:%02d:%02d.%02ld [%d] %s: %s: %s",
153
tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,(long)tv.tv_usec/10000,(int)debug_getpid(),program_name,is_fatal ? "fatal " : flag_to_name(flags),fmt);
155
vsprintf(buffer,newfmt,args);
156
string_chomp(buffer);
158
length = strlen(buffer);
163
fstat(debug_fd,&info);
164
if(info.st_size >= debug_file_size && debug_file_size!=0) {
167
if(stat(debug_file,&info)==0) {
168
if(info.st_size >= debug_file_size) {
169
char *newname = alloca(strlen(debug_file)+5);
170
sprintf(newname,"%s.old",debug_file);
171
rename(debug_file,newname);
175
debug_fd = open(debug_file,O_CREAT|O_TRUNC|O_WRONLY,0777);
176
if(debug_fd<0) fatal("couldn't open %s: %s",debug_file,strerror(errno));
180
full_write(debug_fd,buffer,length);
183
void debug( INT64_T flags, const char *fmt, ... )
188
if(flags&debug_flags) {
189
int save_errno = errno;
190
do_debug(0,flags,fmt,args);
197
void fatal( const char *fmt, ... )
199
struct fatal_callback *f;
203
do_debug(1,0,fmt,args);
205
for(f=fatal_callback_list;f;f=f->next) {
210
kill(getpid(),SIGTERM);
211
kill(getpid(),SIGKILL);
217
void debug_config_fatal( void (*callback) () )
219
struct fatal_callback *f;
220
f = xxmalloc(sizeof(*f));
221
f->callback = callback;
222
f->next = fatal_callback_list;
223
fatal_callback_list = f;
226
void debug_config( const char *name )
228
const char *n = strdup(name);
230
program_name = strrchr(n,'/');
238
void debug_config_file( const char *f )
247
debug_file = strdup(f);
250
if (getcwd(path, sizeof(path)) == NULL) assert(0);
251
assert(strlen(path) + strlen(f) + 1 < 8192);
254
debug_file = strdup(path);
256
debug_fd = open(f,O_CREAT|O_APPEND|O_WRONLY,0777);
257
if(debug_fd<0) fatal("couldn't open %s: %s",f,strerror(errno));
263
void debug_config_file_size( int size )
265
debug_file_size = size;
268
void debug_config_getpid( pid_t (*getpidfunc)() )
270
debug_getpid = getpidfunc;
273
int debug_flags_clear()
275
INT64_T result = debug_flags;
280
void debug_flags_restore( INT64_T fl )