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 #if HAVE_IO_H
00033 #include <io.h>
00034 #endif
00035 #include <stdlib.h>
00036 #include "avutil.h"
00037 #include "common.h"
00038 #include "log.h"
00039
00040 #define LINE_SZ 1024
00041
00042 static int av_log_level = AV_LOG_INFO;
00043 static int flags;
00044
00045 #if HAVE_SETCONSOLETEXTATTRIBUTE
00046 #include <windows.h>
00047 static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
00048 [AV_LOG_PANIC /8] = 12,
00049 [AV_LOG_FATAL /8] = 12,
00050 [AV_LOG_ERROR /8] = 12,
00051 [AV_LOG_WARNING/8] = 14,
00052 [AV_LOG_INFO /8] = 7,
00053 [AV_LOG_VERBOSE/8] = 10,
00054 [AV_LOG_DEBUG /8] = 10,
00055 [16+AV_CLASS_CATEGORY_NA ] = 7,
00056 [16+AV_CLASS_CATEGORY_INPUT ] = 13,
00057 [16+AV_CLASS_CATEGORY_OUTPUT ] = 5,
00058 [16+AV_CLASS_CATEGORY_MUXER ] = 13,
00059 [16+AV_CLASS_CATEGORY_DEMUXER ] = 5,
00060 [16+AV_CLASS_CATEGORY_ENCODER ] = 11,
00061 [16+AV_CLASS_CATEGORY_DECODER ] = 3,
00062 [16+AV_CLASS_CATEGORY_FILTER ] = 10,
00063 [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 9,
00064 [16+AV_CLASS_CATEGORY_SWSCALER ] = 7,
00065 [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 7,
00066 };
00067
00068 static int16_t background, attr_orig;
00069 static HANDLE con;
00070 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
00071 #define set_256color set_color
00072 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
00073 #else
00074
00075 static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
00076 [AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
00077 [AV_LOG_FATAL /8] = 208 << 8 | 0x41,
00078 [AV_LOG_ERROR /8] = 196 << 8 | 0x11,
00079 [AV_LOG_WARNING/8] = 226 << 8 | 0x03,
00080 [AV_LOG_INFO /8] = 253 << 8 | 0x09,
00081 [AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
00082 [AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
00083 [16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
00084 [16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
00085 [16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
00086 [16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
00087 [16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
00088 [16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
00089 [16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
00090 [16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
00091 [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
00092 [16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
00093 [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14,
00094 };
00095
00096 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", (color[x] >> 4) & 15, color[x] & 15)
00097 #define set_256color(x) fprintf(stderr, "\033[48;5;%dm\033[38;5;%dm", (color[x] >> 16) & 0xff, (color[x] >> 8) & 0xff)
00098 #define reset_color() fprintf(stderr, "\033[0m")
00099 #endif
00100 static int use_color = -1;
00101
00102 static void colored_fputs(int level, const char *str)
00103 {
00104 if (use_color < 0) {
00105 #if HAVE_SETCONSOLETEXTATTRIBUTE
00106 CONSOLE_SCREEN_BUFFER_INFO con_info;
00107 con = GetStdHandle(STD_ERROR_HANDLE);
00108 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
00109 !getenv("AV_LOG_FORCE_NOCOLOR");
00110 if (use_color) {
00111 GetConsoleScreenBufferInfo(con, &con_info);
00112 attr_orig = con_info.wAttributes;
00113 background = attr_orig & 0xF0;
00114 }
00115 #elif HAVE_ISATTY
00116 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
00117 (getenv("TERM") && isatty(2) ||
00118 getenv("AV_LOG_FORCE_COLOR"));
00119 if (getenv("AV_LOG_FORCE_256COLOR"))
00120 use_color *= 256;
00121 #else
00122 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
00123 !getenv("AV_LOG_FORCE_NOCOLOR");
00124 #endif
00125 }
00126
00127 if (use_color == 1) {
00128 set_color(level);
00129 } else if (use_color == 256)
00130 set_256color(level);
00131 fputs(str, stderr);
00132 if (use_color) {
00133 reset_color();
00134 }
00135 }
00136
00137 const char *av_default_item_name(void *ptr)
00138 {
00139 return (*(AVClass **) ptr)->class_name;
00140 }
00141
00142 AVClassCategory av_default_get_category(void *ptr)
00143 {
00144 return (*(AVClass **) ptr)->category;
00145 }
00146
00147 static void sanitize(uint8_t *line){
00148 while(*line){
00149 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
00150 *line='?';
00151 line++;
00152 }
00153 }
00154
00155 static int get_category(void *ptr){
00156 AVClass *avc = *(AVClass **) ptr;
00157 if( !avc
00158 || (avc->version&0xFF)<100
00159 || avc->version < (51 << 16 | 59 << 8)
00160 || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
00161
00162 if(avc->get_category)
00163 return avc->get_category(ptr) + 16;
00164
00165 return avc->category + 16;
00166 }
00167
00168 static void format_line(void *ptr, int level, const char *fmt, va_list vl,
00169 char part[3][LINE_SZ], int part_size, int *print_prefix, int type[2])
00170 {
00171 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
00172 part[0][0] = part[1][0] = part[2][0] = 0;
00173 if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
00174 if (*print_prefix && avc) {
00175 if (avc->parent_log_context_offset) {
00176 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
00177 avc->parent_log_context_offset);
00178 if (parent && *parent) {
00179 snprintf(part[0], part_size, "[%s @ %p] ",
00180 (*parent)->item_name(parent), parent);
00181 if(type) type[0] = get_category(((uint8_t *) ptr) + avc->parent_log_context_offset);
00182 }
00183 }
00184 snprintf(part[1], part_size, "[%s @ %p] ",
00185 avc->item_name(ptr), ptr);
00186 if(type) type[1] = get_category(ptr);
00187 }
00188
00189 vsnprintf(part[2], part_size, fmt, vl);
00190
00191 *print_prefix = strlen(part[2]) && part[2][strlen(part[2]) - 1] == '\n';
00192 }
00193
00194 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
00195 char *line, int line_size, int *print_prefix)
00196 {
00197 char part[3][LINE_SZ];
00198 format_line(ptr, level, fmt, vl, part, sizeof(part[0]), print_prefix, NULL);
00199 snprintf(line, line_size, "%s%s%s", part[0], part[1], part[2]);
00200 }
00201
00202 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
00203 {
00204 static int print_prefix = 1;
00205 static int count;
00206 static char prev[LINE_SZ];
00207 char part[3][LINE_SZ];
00208 char line[LINE_SZ];
00209 static int is_atty;
00210 int type[2];
00211
00212 if (level > av_log_level)
00213 return;
00214 format_line(ptr, level, fmt, vl, part, sizeof(part[0]), &print_prefix, type);
00215 snprintf(line, sizeof(line), "%s%s%s", part[0], part[1], part[2]);
00216
00217 #if HAVE_ISATTY
00218 if (!is_atty)
00219 is_atty = isatty(2) ? 1 : -1;
00220 #endif
00221
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 }