codeslogging.h 5.17 KB
Newer Older
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
/*
 * (C) 2001 Clemson University and The University of Chicago
 *
 * See COPYING in top-level directory.
 */

/** \defgroup codeslogging codeslogging logging interface
 *
 * This is a basic application logging facility.  It uses printf style
 * formatting and provides several mechanisms for output.
 *
 * @{
 */

/* This code was derived from the PVFS2 codeslogging circa vs. 2.8.1 */

/** \file
 *
 *  Declarations for the codeslogging logging interface.
 */

#ifndef SRC_COMMON_LOGGING_CODESLOGGING_H
#define SRC_COMMON_LOGGING_CODESLOGGING_H

#include <stdint.h>
#include <stdarg.h>

/********************************************************************
 * Visible interface
 */

#define CODESLOGGING_BUF_SIZE 1024

/* what type of timestamp to place in msgs */
enum codeslogging_logstamp
{
    CODESLOGGING_LOGSTAMP_NONE = 0,
    CODESLOGGING_LOGSTAMP_USEC = 1,
    CODESLOGGING_LOGSTAMP_DATETIME = 2,
    CODESLOGGING_LOGSTAMP_THREAD = 3
};
#define CODESLOGGING_LOGSTAMP_DEFAULT CODESLOGGING_LOGSTAMP_USEC

/* stdio is needed by codeslogging_debug_fp declaration for FILE* */
#include <stdio.h>

int codeslogging_enable_stderr(
    void);
int codeslogging_enable_file(
    const char *filename,
    const char *mode);
int codeslogging_disable(
    void);
int codeslogging_set_debug_mask(
    int debug_on,
    uint64_t mask);
int codeslogging_get_debug_mask(
    int *debug_on,
    uint64_t * mask);
int codeslogging_set_logstamp(
    enum codeslogging_logstamp ts);

void codeslogging_backtrace(
    void);

#ifdef __GNUC__

/* do printf style type checking if built with gcc */
int __codeslogging_debug(
    uint64_t mask,
    char prefix,
    const char *format,
    ...) __attribute__ ((format(printf, 3, 4)));
int codeslogging_err(
    const char *format,
    ...) __attribute__ ((format(printf, 1, 2)));
int __codeslogging_debug_va(
    uint64_t mask,
    char prefix,
    const char *format,
    va_list ap);
int codeslogging_debug_fp(
    FILE * fp,
    char prefix,
    enum codeslogging_logstamp ts,
    const char *format,
    ...) __attribute__ ((format(printf, 4, 5)));

#ifdef CODESLOGGING_DISABLE_DEBUG
#define codeslogging_debug(mask, format, f...) do {} while(0)
#define codeslogging_perf_log(format, f...) do {} while(0)
#define codeslogging_debug_enabled(__m) 0
#else
extern int codeslogging_debug_on;
extern int codeslogging_facility;
extern uint64_t codeslogging_debug_mask;

#define codeslogging_debug_enabled(__m) \
    (codeslogging_debug_on && (codeslogging_debug_mask & __m))

/* try to avoid function call overhead by checking masks in macro */
#define codeslogging_debug(mask, format, f...)                  \
do {                                                      \
    if ((codeslogging_debug_on) && (codeslogging_debug_mask & mask) &&\
        (codeslogging_facility))                                \
    {                                                     \
        __codeslogging_debug(mask, '?', format, ##f);           \
    }                                                     \
} while(0)
#define codeslogging_perf_log(format, f...)                     \
do {                                                      \
    if ((codeslogging_debug_on) &&                              \
        (codeslogging_debug_mask & CODESLOGGING_PERFCOUNTER_DEBUG) && \
        (codeslogging_facility))                                \
    {                                                     \
        __codeslogging_debug(CODESLOGGING_PERFCOUNTER_DEBUG, 'P',     \
            format, ##f);                                 \
    }                                                     \
} while(0)

#endif /* CODESLOGGING_DISABLE_DEBUG */

/* do file and line number printouts w/ the GNU preprocessor */
#define codeslogging_ldebug(mask, format, f...)                  \
do {                                                       \
    codeslogging_debug(mask, "%s: " format, __func__ , ##f); \
} while(0)

#define codeslogging_lerr(format, f...)                  \
do {                                               \
    codeslogging_err("%s line %d: " format, __FILE__ , __LINE__ , ##f); \
    codeslogging_backtrace();                            \
} while(0)
#else /* ! __GNUC__ */

int __codeslogging_debug(
    uint64_t mask,
    char prefix,
    const char *format,
    ...);
int __codeslogging_debug_stub(
    uint64_t mask,
    char prefix,
    const char *format,
    ...);
int codeslogging_err(
    const char *format,
    ...);

#ifdef CODESLOGGING_DISABLE_DEBUG
#define codeslogging_debug(__m, __f, f...) __codeslogging_debug_stub(__m, '?', __f, ##f);
#define codeslogging_ldebug(__m, __f, f...) __codeslogging_debug_stub(__m, '?', __f, ##f);
#define codeslogging_debug_enabled(__m) 0
#else
#define codeslogging_debug(__m, __f, f...) __codeslogging_debug(__m, '?', __f, ##f);
#define codeslogging_ldebug(__m, __f, f...) __codeslogging_debug(__m, '?', __f, ##f);
#define codeslogging_debug_enabled(__m) \
            ((codeslogging_debug_on != 0) && (__m & codeslogging_debug_mask))

#endif /* CODESLOGGING_DISABLE_DEBUG */

#define codeslogging_lerr codeslogging_err

#endif /* __GNUC__ */

#endif /* __CODESLOGGING_H */

/* @} */

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