Libav
|
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 line[1024], prev[1024]; 00063 static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9}; 00064 AVClass* avc= ptr ? *(AVClass**)ptr : NULL; 00065 if(level>av_log_level) 00066 return; 00067 #undef fprintf 00068 if(print_prefix && avc) { 00069 snprintf(line, sizeof(line), "[%s @ %p]", avc->item_name(ptr), ptr); 00070 }else 00071 line[0]=0; 00072 00073 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl); 00074 00075 print_prefix= line[strlen(line)-1] == '\n'; 00076 if(print_prefix && !strcmp(line, prev)){ 00077 count++; 00078 return; 00079 } 00080 if(count>0){ 00081 fprintf(stderr, " Last message repeated %d times\n", count); 00082 count=0; 00083 } 00084 colored_fputs(color[av_clip(level>>3, 0, 6)], line); 00085 strcpy(prev, line); 00086 } 00087 00088 static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback; 00089 00090 void av_log(void* avcl, int level, const char *fmt, ...) 00091 { 00092 va_list vl; 00093 va_start(vl, fmt); 00094 av_vlog(avcl, level, fmt, vl); 00095 va_end(vl); 00096 } 00097 00098 void av_vlog(void* avcl, int level, const char *fmt, va_list vl) 00099 { 00100 av_log_callback(avcl, level, fmt, vl); 00101 } 00102 00103 int av_log_get_level(void) 00104 { 00105 return av_log_level; 00106 } 00107 00108 void av_log_set_level(int level) 00109 { 00110 av_log_level = level; 00111 } 00112 00113 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list)) 00114 { 00115 av_log_callback = callback; 00116 }