| 1 |
/* |
| 2 |
* This file is part of libbdplus |
| 3 |
* Copyright (C) 2008-2010 Accident |
| 4 |
* Copyright (C) 2009-2010 Obliter0n |
| 5 |
* Copyright (C) 2013 VideoLAN |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library. If not, see |
| 19 |
* <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#if HAVE_CONFIG_H |
| 23 |
#include "config.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "logging.h" |
| 27 |
|
| 28 |
#include "file/file.h" |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdarg.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
uint32_t debug_mask = (uint32_t)-1; /* set all bits to make sure bd_debug() is called for initialization */ |
| 36 |
|
| 37 |
void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...) |
| 38 |
{ |
| 39 |
static int debug_init = 0; |
| 40 |
static FILE *logfile = NULL; |
| 41 |
|
| 42 |
// Only call getenv() once. |
| 43 |
if (!debug_init) { |
| 44 |
debug_init = 1; |
| 45 |
logfile = stderr; |
| 46 |
|
| 47 |
char *env = NULL; |
| 48 |
if (debug_mask == (uint32_t)-1) { |
| 49 |
/* might be set by application with bd_set_debug_mask() */ |
| 50 |
debug_mask = DBG_CRIT; |
| 51 |
} |
| 52 |
if ((env = getenv("BD_DEBUG_MASK"))) |
| 53 |
debug_mask = strtol(env, NULL, 0); |
| 54 |
|
| 55 |
// Send DEBUG to file? |
| 56 |
if ((env = getenv("BDPLUS_DEBUG_FILE"))) { |
| 57 |
FILE *fp = fopen(env, "wb"); |
| 58 |
if (fp) { |
| 59 |
logfile = fp; |
| 60 |
setvbuf(logfile, NULL, _IONBF, 0); |
| 61 |
} else { |
| 62 |
fprintf(logfile, "%s:%d: Error opening log file %s\n", __FILE__, __LINE__, env); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (mask & debug_mask) { |
| 68 |
const char *f = strrchr(file, DIR_SEP_CHAR); |
| 69 |
char buffer[4096], *pt = buffer; |
| 70 |
va_list args; |
| 71 |
|
| 72 |
pt += sprintf(buffer, "%s:%d: ", f ? f + 1 : file, line); |
| 73 |
|
| 74 |
va_start(args, format); |
| 75 |
vsnprintf(pt, sizeof(buffer) - (size_t)(intptr_t)(pt - buffer) - 1, format, args); |
| 76 |
va_end(args); |
| 77 |
|
| 78 |
fprintf(logfile, "%s", buffer); |
| 79 |
} |
| 80 |
} |