MythTV  0.26-pre
logging.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of libbluray
00003  * Copyright (C) 2009-2010  Obliter0n
00004  * Copyright (C) 2009-2010  John Stebbins
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library. If not, see
00018  * <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #include "logging.h"
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <stdarg.h>
00026 #include <string.h>
00027 
00028 static debug_mask_t debug_mask = DBG_CRIT;
00029 static BD_LOG_FUNC  log_func   = NULL;
00030 
00031 void bd_set_debug_handler(BD_LOG_FUNC f)
00032 {
00033     log_func = f;
00034 }
00035 
00036 void bd_set_debug_mask(uint32_t mask)
00037 {
00038     debug_mask = mask;
00039 }
00040 
00041 uint32_t bd_get_debug_mask(void)
00042 {
00043     return debug_mask;
00044 }
00045 
00046 char *print_hex(char *out, const uint8_t *buf, int count)
00047 {
00048     int zz;
00049     for(zz = 0; zz < count; zz++) {
00050         sprintf(out + (zz * 2), "%02X", buf[zz]);
00051     }
00052 
00053     return out;
00054 }
00055 
00056 void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...)
00057 {
00058     static int   debug_init = 0;
00059     static FILE *logfile    = NULL;
00060 
00061     // Only call getenv() once.
00062     if (!debug_init) {
00063         debug_init = 1;
00064         logfile = stderr;
00065 
00066         char *env = NULL;
00067         if ((env = getenv("BD_DEBUG_MASK")))
00068             debug_mask = strtol(env, NULL, 0);
00069 
00070         // Send DEBUG to file?
00071         if ((env = getenv("BD_DEBUG_FILE"))) {
00072             FILE *fp = fopen(env, "wb");
00073             if (fp) {
00074                 logfile = fp;
00075                 setvbuf(logfile, NULL, _IOLBF, 0);
00076             } else {
00077                 fprintf(logfile, "%s:%d: Error opening log file %s\n", __FILE__, __LINE__, env);
00078             }
00079         }
00080     }
00081 
00082     if (mask & debug_mask) {
00083         char buffer[512], *pt = buffer;
00084         va_list args;
00085 
00086         pt += sprintf(buffer, "%s:%d: ", file, line);
00087 
00088         va_start(args, format);
00089         vsprintf(pt, format, args);
00090         va_end(args);
00091 
00092         if (log_func) {
00093             log_func(buffer);
00094         } else {
00095             fprintf(logfile, "%s", buffer);
00096         }
00097     }
00098 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends