Libav 0.7.1
|
00001 /* 00002 * log functions 00003 * Copyright (c) 2003 Michel Bardiaux 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; 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 "avstring.h" 00030 #include "avutil.h" 00031 #include "log.h" 00032 00033 static int av_log_level = AV_LOG_INFO; 00034 static int flags; 00035 00036 #if defined(_WIN32) && !defined(__MINGW32CE__) 00037 #include <windows.h> 00038 static const uint8_t color[] = {12,12,12,14,7,7,7}; 00039 static int16_t background, attr_orig; 00040 static HANDLE con; 00041 #define set_color(x) SetConsoleTextAttribute(con, background | color[x]) 00042 #define reset_color() SetConsoleTextAttribute(con, attr_orig) 00043 #else 00044 static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9}; 00045 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x]>>4, color[x]&15) 00046 #define reset_color() fprintf(stderr, "\033[0m") 00047 #endif 00048 static int use_color=-1; 00049 00050 #undef fprintf 00051 static void colored_fputs(int level, const char *str){ 00052 if(use_color<0){ 00053 #if defined(_WIN32) && !defined(__MINGW32CE__) 00054 CONSOLE_SCREEN_BUFFER_INFO con_info; 00055 con = GetStdHandle(STD_ERROR_HANDLE); 00056 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR"); 00057 if (use_color) { 00058 GetConsoleScreenBufferInfo(con, &con_info); 00059 attr_orig = con_info.wAttributes; 00060 background = attr_orig & 0xF0; 00061 } 00062 #elif HAVE_ISATTY 00063 use_color= !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR") && 00064 (getenv("TERM") && isatty(2) || getenv("FFMPEG_FORCE_COLOR")); 00065 #else 00066 use_color= getenv("FFMPEG_FORCE_COLOR") && !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR"); 00067 #endif 00068 } 00069 00070 if(use_color){ 00071 set_color(level); 00072 } 00073 fputs(str, stderr); 00074 if(use_color){ 00075 reset_color(); 00076 } 00077 } 00078 00079 const char* av_default_item_name(void* ptr){ 00080 return (*(AVClass**)ptr)->class_name; 00081 } 00082 00083 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl) 00084 { 00085 static int print_prefix=1; 00086 static int count; 00087 static char prev[1024]; 00088 char line[1024]; 00089 static int is_atty; 00090 AVClass* avc= ptr ? *(AVClass**)ptr : NULL; 00091 if(level>av_log_level) 00092 return; 00093 line[0]=0; 00094 #undef fprintf 00095 if(print_prefix && avc) { 00096 if (avc->parent_log_context_offset) { 00097 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset); 00098 if(parent && *parent){ 00099 snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent); 00100 } 00101 } 00102 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr); 00103 } 00104 00105 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl); 00106 00107 print_prefix = strlen(line) && line[strlen(line)-1] == '\n'; 00108 00109 #if HAVE_ISATTY 00110 if(!is_atty) is_atty= isatty(2) ? 1 : -1; 00111 #endif 00112 00113 if(print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strncmp(line, prev, sizeof line)){ 00114 count++; 00115 if(is_atty==1) 00116 fprintf(stderr, " Last message repeated %d times\r", count); 00117 return; 00118 } 00119 if(count>0){ 00120 fprintf(stderr, " Last message repeated %d times\n", count); 00121 count=0; 00122 } 00123 colored_fputs(av_clip(level>>3, 0, 6), line); 00124 av_strlcpy(prev, line, sizeof line); 00125 } 00126 00127 static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback; 00128 00129 void av_log(void* avcl, int level, const char *fmt, ...) 00130 { 00131 AVClass* avc= avcl ? *(AVClass**)avcl : NULL; 00132 va_list vl; 00133 va_start(vl, fmt); 00134 if(avc && avc->version >= (50<<16 | 15<<8 | 2) && avc->log_level_offset_offset && level>=AV_LOG_FATAL) 00135 level += *(int*)(((uint8_t*)avcl) + avc->log_level_offset_offset); 00136 av_vlog(avcl, level, fmt, vl); 00137 va_end(vl); 00138 } 00139 00140 void av_vlog(void* avcl, int level, const char *fmt, va_list vl) 00141 { 00142 av_log_callback(avcl, level, fmt, vl); 00143 } 00144 00145 int av_log_get_level(void) 00146 { 00147 return av_log_level; 00148 } 00149 00150 void av_log_set_level(int level) 00151 { 00152 av_log_level = level; 00153 } 00154 00155 void av_log_set_flags(int arg) 00156 { 00157 flags= arg; 00158 } 00159 00160 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list)) 00161 { 00162 av_log_callback = callback; 00163 }