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 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 && !strcmp(line, prev)){
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 strcpy(prev, 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 }