00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 }