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 static int av_log_level = AV_LOG_INFO; 00033 static int flags; 00034 00035 #if defined(_WIN32) && !defined(__MINGW32CE__) 00036 #include <windows.h> 00037 static const uint8_t color[] = { 12, 12, 12, 14, 7, 7, 7 }; 00038 static int16_t background, attr_orig; 00039 static HANDLE con; 00040 #define set_color(x) SetConsoleTextAttribute(con, background | color[x]) 00041 #define reset_color() SetConsoleTextAttribute(con, attr_orig) 00042 #else 00043 static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 9, 9 }; 00044 #define set_color(x) fprintf(stderr, "033円[%d;3%dm", color[x] >> 4, color[x]&15) 00045 #define reset_color() fprintf(stderr, "033円[0m") 00046 #endif 00047 static int use_color = -1; 00048 00049 #undef fprintf 00050 static void colored_fputs(int level, const char *str) 00051 { 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") && 00057 !getenv("AV_LOG_FORCE_NOCOLOR"); 00058 if (use_color) { 00059 GetConsoleScreenBufferInfo(con, &con_info); 00060 attr_orig = con_info.wAttributes; 00061 background = attr_orig & 0xF0; 00062 } 00063 #elif HAVE_ISATTY 00064 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") && 00065 (getenv("TERM") && isatty(2) || 00066 getenv("AV_LOG_FORCE_COLOR")); 00067 #else 00068 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") && 00069 !getenv("AV_LOG_FORCE_NOCOLOR"); 00070 #endif 00071 } 00072 00073 if (use_color) { 00074 set_color(level); 00075 } 00076 fputs(str, stderr); 00077 if (use_color) { 00078 reset_color(); 00079 } 00080 } 00081 00082 const char *av_default_item_name(void *ptr) 00083 { 00084 return (*(AVClass **) ptr)->class_name; 00085 } 00086 00087 static void sanitize(uint8_t *line){ 00088 while(*line){ 00089 if(*line < 0x08 || (*line > 0x0D && *line < 0x20)) 00090 *line='?'; 00091 line++; 00092 } 00093 } 00094 00095 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl, 00096 char *line, int line_size, int *print_prefix) 00097 { 00098 AVClass* avc = ptr ? *(AVClass **) ptr : NULL; 00099 line[0] = 0; 00100 if (*print_prefix && avc) { 00101 if (avc->parent_log_context_offset) { 00102 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) + 00103 avc->parent_log_context_offset); 00104 if (parent && *parent) { 00105 snprintf(line, line_size, "[%s @ %p] ", 00106 (*parent)->item_name(parent), parent); 00107 } 00108 } 00109 snprintf(line + strlen(line), line_size - strlen(line), "[%s @ %p] ", 00110 avc->item_name(ptr), ptr); 00111 } 00112 00113 vsnprintf(line + strlen(line), line_size - strlen(line), fmt, vl); 00114 00115 *print_prefix = strlen(line) && line[strlen(line) - 1] == '\n'; 00116 } 00117 00118 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl) 00119 { 00120 static int print_prefix = 1; 00121 static int count; 00122 static char prev[1024]; 00123 char line[1024]; 00124 static int is_atty; 00125 00126 if (level > av_log_level) 00127 return; 00128 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix); 00129 00130 #if HAVE_ISATTY 00131 if (!is_atty) 00132 is_atty = isatty(2) ? 1 : -1; 00133 #endif 00134 00135 #undef fprintf 00136 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){ 00137 count++; 00138 if (is_atty == 1) 00139 fprintf(stderr, " Last message repeated %d times\r", count); 00140 return; 00141 } 00142 if (count > 0) { 00143 fprintf(stderr, " Last message repeated %d times\n", count); 00144 count = 0; 00145 } 00146 strcpy(prev, line); 00147 sanitize(line); 00148 colored_fputs(av_clip(level >> 3, 0, 6), line); 00149 } 00150 00151 static void (*av_log_callback)(void*, int, const char*, va_list) = 00152 av_log_default_callback; 00153 00154 void av_log(void* avcl, int level, const char *fmt, ...) 00155 { 00156 AVClass* avc = avcl ? *(AVClass **) avcl : NULL; 00157 va_list vl; 00158 va_start(vl, fmt); 00159 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) && 00160 avc->log_level_offset_offset && level >= AV_LOG_FATAL) 00161 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset); 00162 av_vlog(avcl, level, fmt, vl); 00163 va_end(vl); 00164 } 00165 00166 void av_vlog(void* avcl, int level, const char *fmt, va_list vl) 00167 { 00168 av_log_callback(avcl, level, fmt, vl); 00169 } 00170 00171 int av_log_get_level(void) 00172 { 00173 return av_log_level; 00174 } 00175 00176 void av_log_set_level(int level) 00177 { 00178 av_log_level = level; 00179 } 00180 00181 void av_log_set_flags(int arg) 00182 { 00183 flags = arg; 00184 } 00185 00186 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list)) 00187 { 00188 av_log_callback = callback; 00189 }