Blame src/util/logging.c

Packit 5e46da
/*
Packit 5e46da
 * This file is part of libbluray
Packit 5e46da
 * Copyright (C) 2009-2010  Obliter0n
Packit 5e46da
 * Copyright (C) 2009-2010  John Stebbins
Packit 5e46da
 *
Packit 5e46da
 * This library is free software; you can redistribute it and/or
Packit 5e46da
 * modify it under the terms of the GNU Lesser General Public
Packit 5e46da
 * License as published by the Free Software Foundation; either
Packit 5e46da
 * version 2.1 of the License, or (at your option) any later version.
Packit 5e46da
 *
Packit 5e46da
 * This library is distributed in the hope that it will be useful,
Packit 5e46da
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5e46da
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 5e46da
 * Lesser General Public License for more details.
Packit 5e46da
 *
Packit 5e46da
 * You should have received a copy of the GNU Lesser General Public
Packit 5e46da
 * License along with this library. If not, see
Packit 5e46da
 * <http://www.gnu.org/licenses/>.
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
#if HAVE_CONFIG_H
Packit 5e46da
#include "config.h"
Packit 5e46da
#endif
Packit 5e46da
Packit 5e46da
#include "logging.h"
Packit 5e46da
Packit 5e46da
#include "file/file.h"
Packit 5e46da
Packit 5e46da
#include <stdlib.h>
Packit 5e46da
#include <stdio.h>
Packit 5e46da
#include <stdarg.h>
Packit 5e46da
#include <string.h>
Packit 5e46da
Packit 5e46da
uint32_t            debug_mask = (uint32_t)-1; /* set all bits to make sure bd_debug() is called for initialization */
Packit 5e46da
static BD_LOG_FUNC  log_func   = NULL;
Packit 5e46da
Packit 5e46da
void bd_set_debug_handler(BD_LOG_FUNC f)
Packit 5e46da
{
Packit 5e46da
    log_func = f;
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
void bd_set_debug_mask(uint32_t mask)
Packit 5e46da
{
Packit 5e46da
    debug_mask = mask;
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
uint32_t bd_get_debug_mask(void)
Packit 5e46da
{
Packit 5e46da
    return debug_mask;
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...)
Packit 5e46da
{
Packit 5e46da
    static int   debug_init = 0, debug_file = 0;
Packit 5e46da
    static FILE *logfile    = NULL;
Packit 5e46da
Packit 5e46da
    // Only call getenv() once.
Packit 5e46da
    if (!debug_init) {
Packit 5e46da
        debug_init = 1;
Packit 5e46da
        logfile = stderr;
Packit 5e46da
Packit 5e46da
        char *env = NULL;
Packit 5e46da
        if (debug_mask == (uint32_t)-1) {
Packit 5e46da
            /* might be set by application with bd_set_debug_mask() */
Packit 5e46da
            debug_mask = DBG_CRIT;
Packit 5e46da
        }
Packit 5e46da
        if ((env = getenv("BD_DEBUG_MASK")))
Packit 5e46da
            debug_mask = strtol(env, NULL, 0);
Packit 5e46da
Packit 5e46da
        // Send DEBUG to file?
Packit 5e46da
        if ((env = getenv("BD_DEBUG_FILE"))) {
Packit 5e46da
            FILE *fp = fopen(env, "wb");
Packit 5e46da
            if (fp) {
Packit 5e46da
                logfile = fp;
Packit 5e46da
                setvbuf(logfile, NULL, _IONBF, 0);
Packit 5e46da
                debug_file = 1;
Packit 5e46da
            } else {
Packit 5e46da
                fprintf(logfile, "%s:%d: Error opening log file %s\n", __FILE__, __LINE__, env);
Packit 5e46da
            }
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    if (mask & debug_mask) {
Packit 5e46da
        const char *f = strrchr(file, DIR_SEP_CHAR);
Packit 5e46da
        char buffer[4096];
Packit 5e46da
        va_list args;
Packit 5e46da
        int len, len2;
Packit 5e46da
        BD_LOG_FUNC lf;
Packit 5e46da
Packit 5e46da
        len = sprintf(buffer, "%s:%d: ", f ? f + 1 : file, line);
Packit 5e46da
        if (len < 0) {
Packit 5e46da
            return;
Packit 5e46da
        }
Packit 5e46da
Packit 5e46da
        va_start(args, format);
Packit 5e46da
        len2 = vsnprintf(buffer + len, sizeof(buffer) - len - 1, format, args);
Packit 5e46da
        va_end(args);
Packit 5e46da
Packit 5e46da
        if (len2 < 0) {
Packit 5e46da
            return;
Packit 5e46da
        }
Packit 5e46da
        lf = log_func;
Packit 5e46da
        if (lf) {
Packit 5e46da
            buffer[sizeof(buffer)-1] = 0;
Packit 5e46da
            lf(buffer);
Packit 5e46da
        }
Packit 5e46da
Packit 5e46da
        if (!lf || debug_file) {
Packit 5e46da
            len += len2;
Packit 5e46da
            if ((size_t)len >= sizeof(buffer)) {
Packit 5e46da
                len = sizeof(buffer);
Packit 5e46da
            }
Packit 5e46da
Packit 5e46da
            fwrite(buffer, len, 1, logfile);
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
}