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 "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 {
00053 if (use_color < 0) {
00054 #if defined(_WIN32) && !defined(__MINGW32CE__)
00055 CONSOLE_SCREEN_BUFFER_INFO con_info;
00056 con = GetStdHandle(STD_ERROR_HANDLE);
00057 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
00058 !getenv("AV_LOG_FORCE_NOCOLOR");
00059 if (use_color) {
00060 GetConsoleScreenBufferInfo(con, &con_info);
00061 attr_orig = con_info.wAttributes;
00062 background = attr_orig & 0xF0;
00063 }
00064 #elif HAVE_ISATTY
00065 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
00066 (getenv("TERM") && isatty(2) ||
00067 getenv("AV_LOG_FORCE_COLOR"));
00068 #else
00069 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
00070 !getenv("AV_LOG_FORCE_NOCOLOR");
00071 #endif
00072 }
00073
00074 if (use_color) {
00075 set_color(level);
00076 }
00077 fputs(str, stderr);
00078 if (use_color) {
00079 reset_color();
00080 }
00081 }
00082
00083 const char *av_default_item_name(void *ptr)
00084 {
00085 return (*(AVClass **) ptr)->class_name;
00086 }
00087
00088 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
00089 {
00090 static int print_prefix = 1;
00091 static int count;
00092 static char prev[1024];
00093 char line[1024];
00094 static int is_atty;
00095 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
00096 if (level > av_log_level)
00097 return;
00098 line[0] = 0;
00099 #undef fprintf
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, sizeof(line), "[%s @ %p] ",
00106 (*parent)->item_name(parent), parent);
00107 }
00108 }
00109 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
00110 avc->item_name(ptr), ptr);
00111 }
00112
00113 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
00114
00115 print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
00116
00117 #if HAVE_ISATTY
00118 if (!is_atty)
00119 is_atty = isatty(2) ? 1 : -1;
00120 #endif
00121
00122 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
00123 !strncmp(line, prev, sizeof line)) {
00124 count++;
00125 if (is_atty == 1)
00126 fprintf(stderr, " Last message repeated %d times\r", count);
00127 return;
00128 }
00129 if (count > 0) {
00130 fprintf(stderr, " Last message repeated %d times\n", count);
00131 count = 0;
00132 }
00133 colored_fputs(av_clip(level >> 3, 0, 6), line);
00134 av_strlcpy(prev, line, sizeof line);
00135 }
00136
00137 static void (*av_log_callback)(void*, int, const char*, va_list) =
00138 av_log_default_callback;
00139
00140 void av_log(void* avcl, int level, const char *fmt, ...)
00141 {
00142 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
00143 va_list vl;
00144 va_start(vl, fmt);
00145 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
00146 avc->log_level_offset_offset && level >= AV_LOG_FATAL)
00147 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
00148 av_vlog(avcl, level, fmt, vl);
00149 va_end(vl);
00150 }
00151
00152 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
00153 {
00154 av_log_callback(avcl, level, fmt, vl);
00155 }
00156
00157 int av_log_get_level(void)
00158 {
00159 return av_log_level;
00160 }
00161
00162 void av_log_set_level(int level)
00163 {
00164 av_log_level = level;
00165 }
00166
00167 void av_log_set_flags(int arg)
00168 {
00169 flags = arg;
00170 }
00171
00172 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
00173 {
00174 av_log_callback = callback;
00175 }