• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavutil/log.c

Go to the documentation of this file.
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 #if LIBAVUTIL_VERSION_MAJOR > 50
00033 static
00034 #endif
00035 int av_log_level = AV_LOG_INFO;
00036 static int flags;
00037 
00038 #if defined(_WIN32) && !defined(__MINGW32CE__)
00039 #include <windows.h>
00040 static const uint8_t color[] = {12,12,12,14,7,7,7};
00041 static int16_t background, attr_orig;
00042 static HANDLE con;
00043 #define set_color(x)  SetConsoleTextAttribute(con, background | color[x])
00044 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
00045 #else
00046 static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
00047 #define set_color(x)  fprintf(stderr, "\033[%d;3%dm", color[x]>>4, color[x]&15)
00048 #define reset_color() fprintf(stderr, "\033[0m")
00049 #endif
00050 static int use_color=-1;
00051 
00052 #undef fprintf
00053 static void colored_fputs(int level, const char *str){
00054     if(use_color<0){
00055 #if defined(_WIN32) && !defined(__MINGW32CE__)
00056         CONSOLE_SCREEN_BUFFER_INFO con_info;
00057         con = GetStdHandle(STD_ERROR_HANDLE);
00058         use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") && !getenv("FFMPEG_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("FFMPEG_FORCE_NOCOLOR") &&
00066             (getenv("TERM") && isatty(2) || getenv("FFMPEG_FORCE_COLOR"));
00067 #else
00068         use_color= getenv("FFMPEG_FORCE_COLOR") && !getenv("NO_COLOR") && !getenv("FFMPEG_FORCE_NOCOLOR");
00069 #endif
00070     }
00071 
00072     if(use_color){
00073         set_color(level);
00074     }
00075     fputs(str, stderr);
00076     if(use_color){
00077         reset_color();
00078     }
00079 }
00080 
00081 const char* av_default_item_name(void* ptr){
00082     return (*(AVClass**)ptr)->class_name;
00083 }
00084 
00085 static void sanitize(uint8_t *line){
00086     while(*line){
00087         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
00088             *line='?';
00089         line++;
00090     }
00091 }
00092 
00093 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
00094 {
00095     static int print_prefix=1;
00096     static int count;
00097     static char prev[1024];
00098     char line[1024];
00099     static int is_atty;
00100     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
00101     if(level>av_log_level)
00102         return;
00103     line[0]=0;
00104 #undef fprintf
00105     if(print_prefix && avc) {
00106         if(avc->version >= (50<<16 | 15<<8 | 3) && avc->parent_log_context_offset){
00107             AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
00108             if(parent && *parent){
00109                 snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent);
00110             }
00111         }
00112         snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
00113     }
00114 
00115     vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
00116 
00117     print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
00118 
00119 #if HAVE_ISATTY
00120     if(!is_atty) is_atty= isatty(2) ? 1 : -1;
00121 #endif
00122 
00123     if(print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
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     strcpy(prev, line);
00134     sanitize(line);
00135     colored_fputs(av_clip(level>>3, 0, 6), line);
00136 }
00137 
00138 static void (*av_log_callback)(void*, int, const char*, va_list) = 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) && avc->log_level_offset_offset && level>=AV_LOG_FATAL)
00146         level += *(int*)(((uint8_t*)avcl) + avc->log_level_offset_offset);
00147     av_vlog(avcl, level, fmt, vl);
00148     va_end(vl);
00149 }
00150 
00151 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
00152 {
00153     av_log_callback(avcl, level, fmt, vl);
00154 }
00155 
00156 int av_log_get_level(void)
00157 {
00158     return av_log_level;
00159 }
00160 
00161 void av_log_set_level(int level)
00162 {
00163     av_log_level = level;
00164 }
00165 
00166 void av_log_set_flags(int arg)
00167 {
00168     flags= arg;
00169 }
00170 
00171 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
00172 {
00173     av_log_callback = callback;
00174 }

Generated on Wed Apr 11 2012 07:31:37 for FFmpeg by  doxygen 1.7.1