00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "config.h"
00028
00029 #if HAVE_UNISTD_H
00030 #include <unistd.h>
00031 #endif
00032 #include <stdlib.h>
00033 #include "avutil.h"
00034 #include "common.h"
00035 #include "log.h"
00036
00037 #define LINE_SZ 1024
00038
00039 static int av_log_level = AV_LOG_INFO;
00040 static int flags;
00041
00042 #if defined(_WIN32) && !defined(__MINGW32CE__)
00043 #include <windows.h>
00044 #include <io.h>
00045 static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
00046 [AV_LOG_PANIC /8] = 12,
00047 [AV_LOG_FATAL /8] = 12,
00048 [AV_LOG_ERROR /8] = 12,
00049 [AV_LOG_WARNING/8] = 14,
00050 [AV_LOG_INFO /8] = 7,
00051 [AV_LOG_VERBOSE/8] = 10,
00052 [AV_LOG_DEBUG /8] = 10,
00053 [16+AV_CLASS_CATEGORY_NA ] = 7,
00054 [16+AV_CLASS_CATEGORY_INPUT ] = 13,
00055 [16+AV_CLASS_CATEGORY_OUTPUT ] = 5,
00056 [16+AV_CLASS_CATEGORY_MUXER ] = 13,
00057 [16+AV_CLASS_CATEGORY_DEMUXER ] = 5,
00058 [16+AV_CLASS_CATEGORY_ENCODER ] = 11,
00059 [16+AV_CLASS_CATEGORY_DECODER ] = 3,
00060 [16+AV_CLASS_CATEGORY_FILTER ] = 10,
00061 [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 9,
00062 [16+AV_CLASS_CATEGORY_SWSCALER ] = 7,
00063 [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 7,
00064 };
00065
00066 static int16_t background, attr_orig;
00067 static HANDLE con;
00068 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
00069 #define set_256color set_color
00070 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
00071 #else
00072
00073 static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
00074 [AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
00075 [AV_LOG_FATAL /8] = 208 << 8 | 0x41,
00076 [AV_LOG_ERROR /8] = 196 << 8 | 0x11,
00077 [AV_LOG_WARNING/8] = 226 << 8 | 0x03,
00078 [AV_LOG_INFO /8] = 253 << 8 | 0x09,
00079 [AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
00080 [AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
00081 [16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
00082 [16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
00083 [16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
00084 [16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
00085 [16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
00086 [16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
00087 [16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
00088 [16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
00089 [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
00090 [16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
00091 [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14,
00092 };
00093
00094 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", (color[x] >> 4) & 15, color[x] & 15)
00095 #define set_256color(x) fprintf(stderr, "\033[48;5;%dm\033[38;5;%dm", (color[x] >> 16) & 0xff, (color[x] >> 8) & 0xff)
00096 #define reset_color() fprintf(stderr, "\033[0m")
00097 #endif
00098 static int use_color = -1;
00099
00100 #undef fprintf
00101 static void colored_fputs(int level, const char *str)
00102 {
00103 if (use_color < 0) {
00104 #if defined(_WIN32) && !defined(__MINGW32CE__)
00105 CONSOLE_SCREEN_BUFFER_INFO con_info;
00106 con = GetStdHandle(STD_ERROR_HANDLE);
00107 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
00108 !getenv("AV_LOG_FORCE_NOCOLOR");
00109 if (use_color) {
00110 GetConsoleScreenBufferInfo(con, &con_info);
00111 attr_orig = con_info.wAttributes;
00112 background = attr_orig & 0xF0;
00113 }
00114 #elif HAVE_ISATTY
00115 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
00116 (getenv("TERM") && isatty(2) ||
00117 getenv("AV_LOG_FORCE_COLOR"));
00118 if (getenv("AV_LOG_FORCE_256COLOR"))
00119 use_color *= 256;
00120 #else
00121 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
00122 !getenv("AV_LOG_FORCE_NOCOLOR");
00123 #endif
00124 }
00125
00126 if (use_color == 1) {
00127 set_color(level);
00128 } else if (use_color == 256)
00129 set_256color(level);
00130 fputs(str, stderr);
00131 if (use_color) {
00132 reset_color();
00133 }
00134 }
00135
00136 const char *av_default_item_name(void *ptr)
00137 {
00138 return (*(AVClass **) ptr)->class_name;
00139 }
00140
00141 AVClassCategory av_default_get_category(void *ptr)
00142 {
00143 return (*(AVClass **) ptr)->category;
00144 }
00145
00146 static void sanitize(uint8_t *line){
00147 while(*line){
00148 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
00149 *line='?';
00150 line++;
00151 }
00152 }
00153
00154 static int get_category(void *ptr){
00155 AVClass *avc = *(AVClass **) ptr;
00156 if( !avc
00157 || (avc->version&0xFF)<100
00158 || avc->version < (51 << 16 | 59 << 8)
00159 || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
00160
00161 if(avc->get_category)
00162 return avc->get_category(ptr) + 16;
00163
00164 return avc->category + 16;
00165 }
00166
00167 static void format_line(void *ptr, int level, const char *fmt, va_list vl,
00168 char part[3][LINE_SZ], int part_size, int *print_prefix, int type[2])
00169 {
00170 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
00171 part[0][0] = part[1][0] = part[2][0] = 0;
00172 if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
00173 if (*print_prefix && avc) {
00174 if (avc->parent_log_context_offset) {
00175 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
00176 avc->parent_log_context_offset);
00177 if (parent && *parent) {
00178 snprintf(part[0], part_size, "[%s @ %p] ",
00179 (*parent)->item_name(parent), parent);
00180 if(type) type[0] = get_category(((uint8_t *) ptr) + avc->parent_log_context_offset);
00181 }
00182 }
00183 snprintf(part[1], part_size, "[%s @ %p] ",
00184 avc->item_name(ptr), ptr);
00185 if(type) type[1] = get_category(ptr);
00186 }
00187
00188 vsnprintf(part[2], part_size, fmt, vl);
00189
00190 *print_prefix = strlen(part[2]) && part[2][strlen(part[2]) - 1] == '\n';
00191 }
00192
00193 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
00194 char *line, int line_size, int *print_prefix)
00195 {
00196 char part[3][LINE_SZ];
00197 format_line(ptr, level, fmt, vl, part, sizeof(part[0]), print_prefix, NULL);
00198 snprintf(line, line_size, "%s%s%s", part[0], part[1], part[2]);
00199 }
00200
00201 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
00202 {
00203 static int print_prefix = 1;
00204 static int count;
00205 static char prev[LINE_SZ];
00206 char part[3][LINE_SZ];
00207 char line[LINE_SZ];
00208 static int is_atty;
00209 int type[2];
00210
00211 if (level > av_log_level)
00212 return;
00213 format_line(ptr, level, fmt, vl, part, sizeof(part[0]), &print_prefix, type);
00214 snprintf(line, sizeof(line), "%s%s%s", part[0], part[1], part[2]);
00215
00216 #if HAVE_ISATTY
00217 if (!is_atty)
00218 is_atty = isatty(2) ? 1 : -1;
00219 #endif
00220
00221 #undef fprintf
00222 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
00223 count++;
00224 if (is_atty == 1)
00225 fprintf(stderr, " Last message repeated %d times\r", count);
00226 return;
00227 }
00228 if (count > 0) {
00229 fprintf(stderr, " Last message repeated %d times\n", count);
00230 count = 0;
00231 }
00232 strcpy(prev, line);
00233 sanitize(part[0]);
00234 colored_fputs(type[0], part[0]);
00235 sanitize(part[1]);
00236 colored_fputs(type[1], part[1]);
00237 sanitize(part[2]);
00238 colored_fputs(av_clip(level >> 3, 0, 6), part[2]);
00239 }
00240
00241 static void (*av_log_callback)(void*, int, const char*, va_list) =
00242 av_log_default_callback;
00243
00244 void av_log(void* avcl, int level, const char *fmt, ...)
00245 {
00246 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
00247 va_list vl;
00248 va_start(vl, fmt);
00249 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
00250 avc->log_level_offset_offset && level >= AV_LOG_FATAL)
00251 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
00252 av_vlog(avcl, level, fmt, vl);
00253 va_end(vl);
00254 }
00255
00256 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
00257 {
00258 if(av_log_callback)
00259 av_log_callback(avcl, level, fmt, vl);
00260 }
00261
00262 int av_log_get_level(void)
00263 {
00264 return av_log_level;
00265 }
00266
00267 void av_log_set_level(int level)
00268 {
00269 av_log_level = level;
00270 }
00271
00272 void av_log_set_flags(int arg)
00273 {
00274 flags = arg;
00275 }
00276
00277 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
00278 {
00279 av_log_callback = callback;
00280 }