codeslogging.c 10.7 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * (C) 2001 Clemson University and The University of Chicago
 *
 * See COPYING in top-level directory.
 */

/** \file
 *  \ingroup codeslogging
 *
 *  Implementation of codeslogging interface.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif

31
#include "codes/codeslogging.h"
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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
#include <pthread.h>

/** controls whether debugging is on or off */
int codeslogging_debug_on = 0;

/** controls the mask level for debugging messages */
uint64_t codeslogging_debug_mask = 0;

enum
{
    CODESLOGGING_STDERR = 1,
    CODESLOGGING_FILE = 2,
};

/** determines which logging facility to use.  Default to stderr to begin
 *  with.
 */
int codeslogging_facility = CODESLOGGING_STDERR;

/* file handle used for file logging */
static FILE *internal_log_file = NULL;

/* what type of timestamp to put on logs */
static enum codeslogging_logstamp internal_logstamp = CODESLOGGING_LOGSTAMP_DEFAULT;

/*****************************************************************
 * prototypes
 */
static int codeslogging_disable_stderr(
    void);
static int codeslogging_disable_file(
    void);

static int codeslogging_debug_fp_va(
    FILE * fp,
    char prefix,
    const char *format,
    va_list ap,
    enum codeslogging_logstamp ts);

/*****************************************************************
 * visible functions
 */

/** Turns on logging to stderr.
 *
 *  \return 0 on success, -errno on failure.
 */
int codeslogging_enable_stderr(
    void)
{

    /* keep up with the existing logging settings */
    int tmp_debug_on = codeslogging_debug_on;
    uint64_t tmp_debug_mask = codeslogging_debug_mask;

    /* turn off any running facility */
    codeslogging_disable();

    codeslogging_facility = CODESLOGGING_STDERR;

    /* restore the logging settings */
    codeslogging_debug_on = tmp_debug_on;
    codeslogging_debug_mask = tmp_debug_mask;

    return 0;
}

/** Turns on logging to a file.  The filename argument indicates which
 *  file to use for logging messages, and the mode indicates whether the
 *  file should be truncated or appended (see fopen() man page).
 *
 *  \return 0 on success, -errno on failure.
 */
int codeslogging_enable_file(
    const char *filename,
    const char *mode)
{

    /* keep up with the existing logging settings */
    int tmp_debug_on = codeslogging_debug_on;
    uint64_t tmp_debug_mask = codeslogging_debug_mask;

    /* turn off any running facility */
    codeslogging_disable();

    internal_log_file = fopen(filename, mode);
    if(!internal_log_file)
    {
        return -errno;
    }

    codeslogging_facility = CODESLOGGING_FILE;

    /* restore the logging settings */
    codeslogging_debug_on = tmp_debug_on;
    codeslogging_debug_mask = tmp_debug_mask;

    return 0;
}

/** Turns off any active logging facility and disables debugging.
 *
 *  \return 0 on success, -errno on failure.
 */
int codeslogging_disable(
    void)
{
    int ret = -EINVAL;

    switch (codeslogging_facility)
    {
        case CODESLOGGING_STDERR:
            ret = codeslogging_disable_stderr();
            break;
        case CODESLOGGING_FILE:
            ret = codeslogging_disable_file();
            break;
        default:
            break;
    }

    codeslogging_debug_on = 0;
    codeslogging_debug_mask = 0;

    return ret;
}

/** Fills in args indicating whether debugging is on or off, and what the 
 *  mask level is.
 *
 *  \return 0 on success, -errno on failure.
 */
int codeslogging_get_debug_mask(
    int *debug_on,
    uint64_t * mask)
{
    *debug_on = codeslogging_debug_on;
    *mask = codeslogging_debug_mask;
    return 0;
}

/** Determines whether debugging messages are turned on or off.  Also
 *  specifies the mask that determines which debugging messages are
 *  printed.
 *
 *  \return 0 on success, -errno on failure.
 */
int codeslogging_set_debug_mask(
    int debug_on,
    uint64_t mask)
{
    if((debug_on != 0) && (debug_on != 1))
    {
        return -EINVAL;
    }

    codeslogging_debug_on = debug_on;
    codeslogging_debug_mask = mask;
    return 0;
}

/* codeslogging_set_logstamp()
 *
 * sets timestamp style for codeslogging messages
 *
 * returns 0 on success, -errno on failure
 */
int codeslogging_set_logstamp(
    enum codeslogging_logstamp ts)
{
    internal_logstamp = ts;
    return (0);
}

#ifndef __GNUC__
/* __codeslogging_debug_stub()
 * 
 * stub for codeslogging_debug that doesn't do anything; used when debugging
 * is "compiled out" on non-gcc builds
 *
 * returns 0
 */
int __codeslogging_debug_stub(
    uint64_t mask,
    char prefix,
    const char *format,
    ...)
{
    return 0;
}
#endif

/* __codeslogging_debug()
 * 
 * Logs a standard debugging message.  It will not be printed unless the
 * mask value matches (logical "and" operation) with the mask specified in
 * codeslogging_set_debug_mask() and debugging is turned on.
 *
 * returns 0 on success, -errno on failure
 */
int __codeslogging_debug(
    uint64_t mask,
    char prefix,
    const char *format,
    ...)
{
    int ret = -EINVAL;
    va_list ap;

    /* rip out the variable arguments */
    va_start(ap, format);
    ret = __codeslogging_debug_va(mask, prefix, format, ap);
    va_end(ap);

    return ret;
}

int __codeslogging_debug_va(
    uint64_t mask,
    char prefix,
    const char *format,
    va_list ap)
{
    int ret = -EINVAL;

    /* NOTE: this check happens in the macro (before making a function call)
     * if we use gcc 
     */
#ifndef __GNUC__
    /* exit quietly if we aren't meant to print */
    if((!codeslogging_debug_on) || !(codeslogging_debug_mask & mask) || (!codeslogging_facility))
    {
        return 0;
    }
#endif

    if(prefix == '?')
    {
        /* automatic prefix assignment */
        prefix = 'D';
    }

    switch (codeslogging_facility)
    {
        case CODESLOGGING_STDERR:
            ret = codeslogging_debug_fp_va(stderr, prefix, format, ap, internal_logstamp);
            break;
        case CODESLOGGING_FILE:
            ret =
                codeslogging_debug_fp_va(internal_log_file, prefix, format, ap, internal_logstamp);
            break;
        default:
            break;
    }

    return ret;
}

/** Logs a critical error message.  This will print regardless of the
 *  mask value and whether debugging is turned on or off, as long as some
 *  logging facility has been enabled.
 *
 *  \return 0 on success, -errno on failure.
 */
int codeslogging_err(
    const char *format,
    ...)
{
    va_list ap;
    int ret = -EINVAL;

    if(!codeslogging_facility)
    {
        return 0;
    }

    /* rip out the variable arguments */
    va_start(ap, format);

    switch (codeslogging_facility)
    {
        case CODESLOGGING_STDERR:
            ret = codeslogging_debug_fp_va(stderr, 'E', format, ap, internal_logstamp);
            break;
        case CODESLOGGING_FILE:
            ret = codeslogging_debug_fp_va(internal_log_file, 'E', format, ap, internal_logstamp);
            break;
        default:
            break;
    }

    va_end(ap);

    return ret;
}

#ifdef CODESLOGGING_ENABLE_BACKTRACE
#ifndef CODESLOGGING_BACKTRACE_DEPTH
#define CODESLOGGING_BACKTRACE_DEPTH 12
#endif
/** Prints out a dump of the current stack (excluding this function)
 *  using codeslogging_err.
 */
void codeslogging_backtrace(
    void)
{
    void *trace[CODESLOGGING_BACKTRACE_DEPTH];
    char **messages = NULL;
    int i, trace_size;

    trace_size = backtrace(trace, CODESLOGGING_BACKTRACE_DEPTH);
    messages = backtrace_symbols(trace, trace_size);
    for(i = 1; i < trace_size; i++)
    {
        codeslogging_err("\t[bt] %s\n", messages[i]);
    }
    free(messages);
}
#else
void codeslogging_backtrace(
    void)
{
}
#endif

/****************************************************************
 * Internal functions
 */

int codeslogging_debug_fp(
    FILE * fp,
    char prefix,
    enum codeslogging_logstamp ts,
    const char *format,
    ...)
{
    int ret;
    va_list ap;

    /* rip out the variable arguments */
    va_start(ap, format);
    ret = codeslogging_debug_fp_va(fp, prefix, format, ap, ts);
    va_end(ap);
    return ret;
}

/* codeslogging_debug_fp_va()
 * 
 * This is the standard debugging message function for the file logging
 * facility or to stderr.
 *
 * returns 0 on success, -errno on failure
 */
static int codeslogging_debug_fp_va(
    FILE * fp,
    char prefix,
    const char *format,
    va_list ap,
    enum codeslogging_logstamp ts)
{
    char buffer[CODESLOGGING_BUF_SIZE], *bptr = buffer;
    int bsize = sizeof(buffer);
    int ret = -EINVAL;
    struct timeval tv;
    time_t tp;

    sprintf(bptr, "[%c ", prefix);
    bptr += 3;
    bsize -= 3;

    switch (ts)
    {
        case CODESLOGGING_LOGSTAMP_USEC:
            gettimeofday(&tv, 0);
            tp = tv.tv_sec;
            strftime(bptr, 9, "%H:%M:%S", localtime(&tp));
            sprintf(bptr + 8, ".%06ld] ", (long) tv.tv_usec);
            bptr += 17;
            bsize -= 17;
            break;
        case CODESLOGGING_LOGSTAMP_DATETIME:
            gettimeofday(&tv, 0);
            tp = tv.tv_sec;
            strftime(bptr, 22, "%m/%d/%Y %H:%M:%S] ", localtime(&tp));
            bptr += 21;
            bsize -= 21;
            break;
        case CODESLOGGING_LOGSTAMP_THREAD:
            gettimeofday(&tv, 0);
            tp = tv.tv_sec;
            strftime(bptr, 9, "%H:%M:%S", localtime(&tp));
            sprintf(bptr + 8, ".%06ld (%ld)] ", (long) tv.tv_usec, pthread_self());
            bptr += 30;
            bsize -= 30;
            break;

        case CODESLOGGING_LOGSTAMP_NONE:
            bptr--;
            sprintf(bptr, "] ");
            bptr += 2;
            bsize++;
            break;
        default:
            break;
    }

    ret = vsnprintf(bptr, bsize, format, ap);
    if(ret < 0)
    {
        return -errno;
    }

    ret = fprintf(fp, "%s", buffer);
    if(ret < 0)
    {
        return -errno;
    }
    fflush(fp);

    return 0;
}

/* codeslogging_disable_stderr()
 * 
 * The shutdown function for the stderr logging facility.
 *
 * returns 0 on success, -errno on failure
 */
static int codeslogging_disable_stderr(
    void)
{
    /* this function doesn't need to do anything... */
    return 0;
}

/* codeslogging_disable_file()
 * 
 * The shutdown function for the file logging facility.
 *
 * returns 0 on success, -errno on failure
 */
static int codeslogging_disable_file(
    void)
{
    if(internal_log_file)
    {
        fclose(internal_log_file);
        internal_log_file = NULL;
    }
    return 0;
}

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */