Libav

libavutil/log.c

Go to the documentation of this file.
00001 /*
00002  * log functions
00003  * Copyright (c) 2003 Michel Bardiaux
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include <unistd.h>
00028 #include <stdlib.h>
00029 #include "avutil.h"
00030 #include "log.h"
00031 
00032 #if LIBAVUTIL_VERSION_MAJOR > 50
00033 static
00034 #endif
00035 int av_log_level = AV_LOG_INFO;
00036 
00037 static int use_ansi_color=-1;
00038 
00039 #undef fprintf
00040 static void colored_fputs(int color, const char *str){
00041     if(use_ansi_color<0){
00042 #if HAVE_ISATTY && !defined(_WIN32)
00043         use_ansi_color= getenv("TERM") && !getenv("NO_COLOR") && isatty(2);
00044 #else
00045         use_ansi_color= 0;
00046 #endif
00047     }
00048 
00049     if(use_ansi_color){
00050         fprintf(stderr, "\033[%d;3%dm", color>>4, color&15);
00051     }
00052     fputs(str, stderr);
00053     if(use_ansi_color){
00054         fprintf(stderr, "\033[0m");
00055     }
00056 }
00057 
00058 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
00059 {
00060     static int print_prefix=1;
00061     static int count;
00062     static char prev[1024];
00063     char line[1024];
00064     static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
00065     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
00066     if(level>av_log_level)
00067         return;
00068 #undef fprintf
00069     if(print_prefix && avc) {
00070         snprintf(line, sizeof(line), "[%s @ %p]", avc->item_name(ptr), ptr);
00071     }else
00072         line[0]=0;
00073 
00074     vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
00075 
00076     print_prefix= line[strlen(line)-1] == '\n';
00077     if(print_prefix && !strncmp(line, prev, sizeof line)){
00078         count++;
00079         return;
00080     }
00081     if(count>0){
00082         fprintf(stderr, "    Last message repeated %d times\n", count);
00083         count=0;
00084     }
00085     colored_fputs(color[av_clip(level>>3, 0, 6)], line);
00086     strncpy(prev, line, sizeof line);
00087 }
00088 
00089 static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
00090 
00091 void av_log(void* avcl, int level, const char *fmt, ...)
00092 {
00093     va_list vl;
00094     va_start(vl, fmt);
00095     av_vlog(avcl, level, fmt, vl);
00096     va_end(vl);
00097 }
00098 
00099 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
00100 {
00101     av_log_callback(avcl, level, fmt, vl);
00102 }
00103 
00104 int av_log_get_level(void)
00105 {
00106     return av_log_level;
00107 }
00108 
00109 void av_log_set_level(int level)
00110 {
00111     av_log_level = level;
00112 }
00113 
00114 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
00115 {
00116     av_log_callback = callback;
00117 }