4b45de0e |
/*
* log functions
* Copyright (c) 2003 Michel Bardiaux
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
4b45de0e |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
4b45de0e |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
4b45de0e |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
4b45de0e |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** |
ba87f080 |
* @file |
7d685b48 |
* logging functions |
4b45de0e |
*/
|
539df611 |
#include "config.h"
|
145a8096 |
#if HAVE_UNISTD_H |
51e026d1 |
#include <unistd.h> |
539df611 |
#endif |
b70abd5b |
#if HAVE_IO_H
#include <io.h>
#endif |
f099d3d1 |
#include <stdarg.h> |
62044024 |
#include <stdlib.h> |
4b45de0e |
#include "avutil.h" |
148310ca |
#include "bprint.h" |
1d9c2dc8 |
#include "common.h" |
f099d3d1 |
#include "internal.h" |
77652a6a |
#include "log.h" |
4b45de0e |
|
423ae313 |
#if HAVE_PTHREADS
#include <pthread.h>
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
|
258dfff8 |
#define LINE_SZ 1024
|
490a022d |
static int av_log_level = AV_LOG_INFO; |
1c1c80f0 |
static int flags; |
4b45de0e |
|
7e6a11bc |
#if HAVE_SETCONSOLETEXTATTRIBUTE |
4855f867 |
#include <windows.h> |
2191aef9 |
static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
[AV_LOG_PANIC /8] = 12,
[AV_LOG_FATAL /8] = 12,
[AV_LOG_ERROR /8] = 12,
[AV_LOG_WARNING/8] = 14,
[AV_LOG_INFO /8] = 7,
[AV_LOG_VERBOSE/8] = 10, |
eb7efc12 |
[AV_LOG_DEBUG /8] = 10,
[16+AV_CLASS_CATEGORY_NA ] = 7, |
bc210bfc |
[16+AV_CLASS_CATEGORY_INPUT ] = 13,
[16+AV_CLASS_CATEGORY_OUTPUT ] = 5,
[16+AV_CLASS_CATEGORY_MUXER ] = 13,
[16+AV_CLASS_CATEGORY_DEMUXER ] = 5,
[16+AV_CLASS_CATEGORY_ENCODER ] = 11,
[16+AV_CLASS_CATEGORY_DECODER ] = 3, |
ee7214c5 |
[16+AV_CLASS_CATEGORY_FILTER ] = 10, |
eb7efc12 |
[16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 9, |
1842b275 |
[16+AV_CLASS_CATEGORY_SWSCALER ] = 7,
[16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 7, |
2191aef9 |
};
|
4855f867 |
static int16_t background, attr_orig;
static HANDLE con;
#else |
2191aef9 |
|
1842b275 |
static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
[AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
[AV_LOG_FATAL /8] = 208 << 8 | 0x41,
[AV_LOG_ERROR /8] = 196 << 8 | 0x11,
[AV_LOG_WARNING/8] = 226 << 8 | 0x03,
[AV_LOG_INFO /8] = 253 << 8 | 0x09,
[AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
[AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
[16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
[16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
[16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
[16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
[16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
[16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
[16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
[16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
[16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
[16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
[16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14, |
2191aef9 |
};
|
4855f867 |
#endif |
425b45d4 |
static int use_color = -1; |
51e026d1 |
|
425b45d4 |
static void colored_fputs(int level, const char *str)
{ |
a044a183 |
if (!*str)
return;
|
425b45d4 |
if (use_color < 0) { |
7e6a11bc |
#if HAVE_SETCONSOLETEXTATTRIBUTE |
4855f867 |
CONSOLE_SCREEN_BUFFER_INFO con_info;
con = GetStdHandle(STD_ERROR_HANDLE); |
425b45d4 |
use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
!getenv("AV_LOG_FORCE_NOCOLOR"); |
4855f867 |
if (use_color) {
GetConsoleScreenBufferInfo(con, &con_info);
attr_orig = con_info.wAttributes;
background = attr_orig & 0xF0;
}
#elif HAVE_ISATTY |
425b45d4 |
use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
(getenv("TERM") && isatty(2) ||
getenv("AV_LOG_FORCE_COLOR")); |
1842b275 |
if (getenv("AV_LOG_FORCE_256COLOR"))
use_color *= 256; |
62044024 |
#else |
425b45d4 |
use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
!getenv("AV_LOG_FORCE_NOCOLOR"); |
62044024 |
#endif
}
|
2c21e2ee |
#if HAVE_SETCONSOLETEXTATTRIBUTE |
d6838309 |
if (use_color && level != AV_LOG_INFO/8) |
2c21e2ee |
SetConsoleTextAttribute(con, background | color[level]); |
51e026d1 |
fputs(str, stderr); |
d6838309 |
if (use_color && level != AV_LOG_INFO/8) |
2c21e2ee |
SetConsoleTextAttribute(con, attr_orig);
#else |
d6838309 |
if (use_color == 1 && level != AV_LOG_INFO/8) { |
2c21e2ee |
fprintf(stderr,
"\033[%d;3%dm%s\033[0m",
(color[level] >> 4) & 15,
color[level] & 15,
str); |
d6838309 |
} else if (use_color == 256 && level != AV_LOG_INFO/8) { |
2c21e2ee |
fprintf(stderr,
"\033[48;5;%dm\033[38;5;%dm%s\033[0m",
(color[level] >> 16) & 0xff,
(color[level] >> 8) & 0xff,
str);
} else
fputs(str, stderr);
#endif
|
51e026d1 |
}
|
425b45d4 |
const char *av_default_item_name(void *ptr)
{
return (*(AVClass **) ptr)->class_name; |
8d2a5139 |
}
|
e69263cd |
AVClassCategory av_default_get_category(void *ptr)
{
return (*(AVClass **) ptr)->category;
}
|
0a51362b |
static void sanitize(uint8_t *line){
while(*line){
if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
*line='?';
line++;
}
}
|
e69263cd |
static int get_category(void *ptr){
AVClass *avc = *(AVClass **) ptr; |
eb7efc12 |
if( !avc
|| (avc->version&0xFF)<100 |
e69263cd |
|| avc->version < (51 << 16 | 59 << 8) |
eb7efc12 |
|| avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
|
e69263cd |
if(avc->get_category)
return avc->get_category(ptr) + 16;
|
eb7efc12 |
return avc->category + 16;
}
|
74744790 |
static void format_line(void *avcl, int level, const char *fmt, va_list vl, |
148310ca |
AVBPrint part[3], int *print_prefix, int type[2]) |
4b45de0e |
{ |
25a1ba81 |
AVClass* avc = avcl ? *(AVClass **) avcl : NULL; |
148310ca |
av_bprint_init(part+0, 0, 1);
av_bprint_init(part+1, 0, 1);
av_bprint_init(part+2, 0, 65536);
|
eb7efc12 |
if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16; |
26c6fec9 |
if (*print_prefix && avc) { |
63de9e7d |
if (avc->parent_log_context_offset) { |
25a1ba81 |
AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) + |
425b45d4 |
avc->parent_log_context_offset);
if (parent && *parent) { |
148310ca |
av_bprintf(part+0, "[%s @ %p] ", |
425b45d4 |
(*parent)->item_name(parent), parent); |
7edb984d |
if(type) type[0] = get_category(parent); |
4880cfd9 |
}
} |
148310ca |
av_bprintf(part+1, "[%s @ %p] ", |
25a1ba81 |
avc->item_name(avcl), avcl); |
74744790 |
if(type) type[1] = get_category(avcl); |
4880cfd9 |
} |
4b45de0e |
|
148310ca |
av_vbprintf(part+2, fmt, vl); |
26c6fec9 |
|
148310ca |
if(*part[0].str || *part[1].str || *part[2].str) { |
808c10e7 |
char lastc = part[2].len && part[2].len <= part[2].size ? part[2].str[part[2].len - 1] : 0; |
c25d1ba5 |
*print_prefix = lastc == '\n' || lastc == '\r';
} |
eb7efc12 |
}
void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
char *line, int line_size, int *print_prefix)
{ |
148310ca |
AVBPrint part[3];
format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
snprintf(line, line_size, "%s%s%s", part[0].str, part[1].str, part[2].str);
av_bprint_finalize(part+2, NULL); |
26c6fec9 |
}
void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
{
static int print_prefix = 1;
static int count; |
258dfff8 |
static char prev[LINE_SZ]; |
148310ca |
AVBPrint part[3]; |
258dfff8 |
char line[LINE_SZ]; |
26c6fec9 |
static int is_atty; |
eb7efc12 |
int type[2]; |
4b45de0e |
|
26c6fec9 |
if (level > av_log_level)
return; |
423ae313 |
#if HAVE_PTHREADS
pthread_mutex_lock(&mutex);
#endif
|
148310ca |
format_line(ptr, level, fmt, vl, part, &print_prefix, type);
snprintf(line, sizeof(line), "%s%s%s", part[0].str, part[1].str, part[2].str); |
c157fe63 |
#if HAVE_ISATTY |
425b45d4 |
if (!is_atty)
is_atty = isatty(2) ? 1 : -1; |
c157fe63 |
#endif
|
39bb26f9 |
if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
*line && line[strlen(line) - 1] != '\r'){ |
b9c353ff |
count++; |
425b45d4 |
if (is_atty == 1) |
d7cd001f |
fprintf(stderr, " Last message repeated %d times\r", count); |
423ae313 |
goto end; |
b9c353ff |
} |
425b45d4 |
if (count > 0) { |
b9c353ff |
fprintf(stderr, " Last message repeated %d times\n", count); |
425b45d4 |
count = 0; |
b9c353ff |
}
strcpy(prev, line); |
148310ca |
sanitize(part[0].str);
colored_fputs(type[0], part[0].str);
sanitize(part[1].str);
colored_fputs(type[1], part[1].str);
sanitize(part[2].str);
colored_fputs(av_clip(level >> 3, 0, 6), part[2].str); |
423ae313 |
end: |
148310ca |
av_bprint_finalize(part+2, NULL); |
423ae313 |
#if HAVE_PTHREADS
pthread_mutex_unlock(&mutex);
#endif |
4b45de0e |
}
|
425b45d4 |
static void (*av_log_callback)(void*, int, const char*, va_list) =
av_log_default_callback; |
4b45de0e |
void av_log(void* avcl, int level, const char *fmt, ...)
{ |
425b45d4 |
AVClass* avc = avcl ? *(AVClass **) avcl : NULL; |
4b45de0e |
va_list vl;
va_start(vl, fmt); |
425b45d4 |
if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
avc->log_level_offset_offset && level >= AV_LOG_FATAL)
level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset); |
4b45de0e |
av_vlog(avcl, level, fmt, vl);
va_end(vl);
}
void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
{ |
3ed65d98 |
void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
if (log_callback)
log_callback(avcl, level, fmt, vl); |
4b45de0e |
}
int av_log_get_level(void)
{
return av_log_level;
}
void av_log_set_level(int level)
{
av_log_level = level;
}
|
1c1c80f0 |
void av_log_set_flags(int arg)
{ |
425b45d4 |
flags = arg; |
1c1c80f0 |
}
|
4b45de0e |
void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
{
av_log_callback = callback;
} |
f099d3d1 |
|
0420c810 |
static void missing_feature_sample(int sample, void *avc, const char *msg,
va_list argument_list) |
f099d3d1 |
{
av_vlog(avc, AV_LOG_WARNING, msg, argument_list); |
905e7eb4 |
av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg " |
f099d3d1 |
"version to the newest one from Git. If the problem still "
"occurs, it means that your file has a feature which has not "
"been implemented.\n");
if (sample)
av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample " |
905e7eb4 |
"of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
"and contact the ffmpeg-devel mailing list.\n"); |
f099d3d1 |
}
void avpriv_request_sample(void *avc, const char *msg, ...)
{
va_list argument_list;
va_start(argument_list, msg);
missing_feature_sample(1, avc, msg, argument_list);
va_end(argument_list);
}
void avpriv_report_missing_feature(void *avc, const char *msg, ...)
{
va_list argument_list;
va_start(argument_list, msg);
missing_feature_sample(0, avc, msg, argument_list);
va_end(argument_list);
} |