1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
Copyright (C) 2005- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#include "debug.h"
#include "buffer.h"
#include "path.h"
#include "xxmalloc.h"
#include <sys/time.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
extern void debug_stderr_write (int64_t flags, const char *str);
extern void debug_stdout_write (int64_t flags, const char *str);
extern void debug_file_write (int64_t flags, const char *str);
extern void debug_file_size (off_t size);
extern int debug_file_path (const char *path);
extern void debug_file_rename (const char *suffix);
extern int debug_file_reopen (void);
static void (*debug_write) (int64_t flags, const char *str) = debug_stderr_write;
static pid_t (*debug_getpid) (void) = getpid;
static char debug_program_name[PATH_MAX];
static int64_t debug_flags = D_NOTICE|D_ERROR|D_FATAL;
struct flag_info {
const char *name;
int64_t flag;
};
static struct flag_info table[] = {
/* info, the default, is not shown here */
{"fatal", D_FATAL},
{"error", D_ERROR},
{"notice", D_NOTICE},
{"debug", D_DEBUG},
/* subsystems */
{"syscall", D_SYSCALL},
{"channel", D_CHANNEL},
{"process", D_PROCESS},
{"resolve", D_RESOLVE},
{"libcall", D_LIBCALL},
{"tcp", D_TCP},
{"dns", D_DNS},
{"auth", D_AUTH},
{"local", D_LOCAL},
{"http", D_HTTP},
{"ftp", D_FTP},
{"nest", D_NEST},
{"chirp", D_CHIRP},
{"cvmfs", D_CVMFS},
{"multi", D_MULTI},
{"dcap", D_DCAP},
{"rfio", D_RFIO},
{"glite", D_GLITE},
{"lfc", D_LFC},
{"gfal", D_GFAL},
{"grow", D_GROW},
{"pstree", D_PSTREE},
{"alloc", D_ALLOC},
{"cache", D_CACHE},
{"poll", D_POLL},
{"hdfs", D_HDFS},
{"bxgrid", D_BXGRID},
{"login", D_LOGIN},
{"irods", D_IRODS},
{"wq", D_WQ},
{"mpi", D_MPI},
{"user", D_USER},
{"xrootd", D_XROOTD},
{"remote", D_REMOTE},
{"batch", D_BATCH},
{"rmonitor", D_RMON},
{"makeflow", D_MAKEFLOW},
{"makeflow_run", D_MAKEFLOW_RUN},
{"makeflow_alloc", D_MAKEFLOW_ALLOC},
{"makeflow_lexer", D_MAKEFLOW_LEXER},
{"makeflow_parser", D_MAKEFLOW_PARSER},
{"makeflow_hook", D_MAKEFLOW_HOOK},
{"ext", D_EXT},
{"rmonitor", D_RMON},
{"confuga", D_CONFUGA},
{"dataswarm", D_DATASWARM},
{"tlq", D_TLQ},
{"jx", D_JX},
{"all", D_ALL},
{"time", 0}, /* backwards compatibility */
{"pid", 0}, /* backwards compatibility */
{0, 0}
};
struct fatal_callback {
void (*callback) ();
struct fatal_callback *next;
};
struct fatal_callback *fatal_callback_list = 0;
int debug_flags_set(const char *flagname)
{
struct flag_info *i;
if(!strcmp(flagname,"clear")) {
debug_flags_clear();
return 1;
}
for(i = table; i->name; i++) {
if(!strcmp(flagname, i->name)) {
debug_flags |= i->flag;
return 1;
}
}
return 0;
}
void debug_flags_print(FILE * stream)
{
int i;
fprintf(stream, "clear (unsets all flags)");
for(i = 0; table[i].name; i++) {
fprintf(stream, ", %s", table[i].name);
}
}
void debug_set_flag_name(int64_t flag, const char *name)
{
struct flag_info *i;
for(i = table; i->name; i++) {
if(i->flag & flag) {
i->name = name;
break;
}
}
}
static const char *debug_flags_to_name(int64_t flags)
{
struct flag_info *i;
for(i = table; i->name; i++) {
if(i->flag & flags)
return i->name;
}
return "debug";
}
static void do_debug(int64_t flags, const char *fmt, va_list args)
{
buffer_t B;
char ubuf[1<<16];
buffer_init(&B);
buffer_ubuf(&B, ubuf, sizeof(ubuf));
buffer_max(&B, sizeof(ubuf));
if (debug_write == debug_file_write || debug_write == debug_stderr_write || debug_write == debug_stdout_write) {
struct timeval tv;
struct tm *tm;
gettimeofday(&tv, 0);
tm = localtime(&tv.tv_sec);
buffer_putfstring(&B, "%04d/%02d/%02d %02d:%02d:%02d.%02ld ", 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);
buffer_putfstring(&B, "%s[%d] ", debug_program_name, getpid());
}
/* Parrot prints debug messages for children: */
if (getpid() != debug_getpid()) {
buffer_putfstring(&B, "<child:%d> ", (int)debug_getpid());
}
buffer_putfstring(&B, "%s: ", debug_flags_to_name(flags));
buffer_putvfstring(&B, fmt, args);
while(isspace(buffer_tostring(&B)[buffer_pos(&B)-1]))
buffer_rewind(&B, buffer_pos(&B)-1); /* chomp whitespace */
buffer_putliteral(&B, "\n");
debug_write(flags, buffer_tostring(&B));
if(debug_write != debug_stderr_write && (flags & (D_ERROR | D_NOTICE | D_FATAL))) {
debug_stderr_write(flags, buffer_tostring(&B));
}
buffer_free(&B);
}
void debug(int64_t flags, const char *fmt, ...)
{
if(flags & debug_flags) {
va_list args;
int save_errno = errno;
va_start(args, fmt);
do_debug(flags, fmt, args);
va_end(args);
errno = save_errno;
}
}
void vdebug(int64_t flags, const char *fmt, va_list args)
{
if(flags & debug_flags) {
int save_errno = errno;
do_debug(flags, fmt, args);
errno = save_errno;
}
}
void warn(int64_t flags, const char *fmt, ...)
{
va_list args;
int save_errno = errno;
va_start(args, fmt);
do_debug(flags|D_ERROR, fmt, args);
va_end(args);
errno = save_errno;
}
void notice(int64_t flags, const char *fmt, ...)
{
va_list args;
int save_errno = errno;
va_start(args, fmt);
do_debug(flags|D_NOTICE, fmt, args);
va_end(args);
errno = save_errno;
}
void fatal(const char *fmt, ...)
{
struct fatal_callback *f;
va_list args;
va_start(args, fmt);
do_debug(D_FATAL, fmt, args);
va_end(args);
for(f = fatal_callback_list; f; f = f->next) {
f->callback();
}
while(1) {
raise(SIGTERM);
raise(SIGKILL);
}
}
void debug_config_fatal(void (*callback) ())
{
struct fatal_callback *f;
f = xxmalloc(sizeof(*f));
f->callback = callback;
f->next = fatal_callback_list;
fatal_callback_list = f;
}
int debug_config_file_e (const char *path)
{
if(path == NULL || strcmp(path, ":stderr") == 0) {
debug_write = debug_stderr_write;
return 0;
} else if(strcmp(path, ":stdout") == 0) {
debug_write = debug_stdout_write;
return 0;
} else {
debug_write = debug_file_write;
return debug_file_path(path);
}
}
void debug_config_file (const char *path)
{
if (debug_config_file_e(path) == -1) {
fprintf(stderr, "could not set debug file '%s': %s", path, strerror(errno));
exit(EXIT_FAILURE);
}
}
void debug_config (const char *name)
{
strncpy(debug_program_name, path_basename(name), sizeof(debug_program_name)-1);
}
void debug_config_file_size (off_t size)
{
debug_file_size(size);
}
void debug_config_getpid (pid_t (*getpidf)(void))
{
debug_getpid = getpidf;
}
int64_t debug_flags_clear()
{
int64_t result = debug_flags;
debug_flags = 0;
return result;
}
void debug_flags_restore(int64_t fl)
{
debug_flags = fl;
}
void debug_rename(const char *suffix)
{
debug_file_rename(suffix);
}
void debug_reopen(void)
{
if (debug_file_reopen() == -1)
fatal("could not reopen debug log: %s", strerror(errno));
}
/* vim: set noexpandtab tabstop=4: */
|