Remove unused variable.
[libav.git] / libavutil / log.c
CommitLineData
4b45de0e
LA
1/*
2 * log functions
3 * Copyright (c) 2003 Michel Bardiaux
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
4b45de0e
LA
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
4b45de0e 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
4b45de0e
LA
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
4b45de0e
LA
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
7d685b48 24 * logging functions
4b45de0e
LA
25 */
26
51e026d1 27#include <unistd.h>
62044024 28#include <stdlib.h>
4b45de0e 29#include "avutil.h"
77652a6a 30#include "log.h"
4b45de0e 31
e244f54e
RP
32#if LIBAVUTIL_VERSION_MAJOR > 50
33static
34#endif
918a4591 35int av_log_level = AV_LOG_INFO;
4b45de0e 36
3c467bac 37#if defined(_WIN32) && !defined(__MINGW32CE__)
4855f867
RP
38#include <windows.h>
39static const uint8_t color[] = {12,12,12,14,7,7,7};
40static int16_t background, attr_orig;
41static HANDLE con;
db16e3ca 42#define set_color(x) SetConsoleTextAttribute(con, background | color[x])
4855f867
RP
43#define reset_color() SetConsoleTextAttribute(con, attr_orig)
44#else
6e34a558 45static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
4855f867
RP
46#define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x]>>4, color[x]&15)
47#define reset_color() fprintf(stderr, "\033[0m")
48#endif
a1c027e9 49static int use_color=-1;
51e026d1
MN
50
51#undef fprintf
6e34a558 52static void colored_fputs(int level, const char *str){
a1c027e9 53 if(use_color<0){
3c467bac 54#if defined(_WIN32) && !defined(__MINGW32CE__)
4855f867
RP
55 CONSOLE_SCREEN_BUFFER_INFO con_info;
56 con = GetStdHandle(STD_ERROR_HANDLE);
57 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR");
58 if (use_color) {
59 GetConsoleScreenBufferInfo(con, &con_info);
60 attr_orig = con_info.wAttributes;
61 background = attr_orig & 0xF0;
62 }
63#elif HAVE_ISATTY
a1c027e9 64 use_color= getenv("TERM") && !getenv("NO_COLOR") && isatty(2);
62044024 65#else
a1c027e9 66 use_color= 0;
62044024
MN
67#endif
68 }
69
a1c027e9 70 if(use_color){
4855f867 71 set_color(level);
51e026d1
MN
72 }
73 fputs(str, stderr);
a1c027e9 74 if(use_color){
4855f867 75 reset_color();
51e026d1
MN
76 }
77}
78
8d2a5139
MN
79const char* av_default_item_name(void* ptr){
80 return (*(AVClass**)ptr)->class_name;
81}
82
411983c1 83void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
4b45de0e
LA
84{
85 static int print_prefix=1;
b9c353ff
MN
86 static int count;
87 static char line[1024], prev[1024];
c157fe63 88 static int detect_repeats;
4b45de0e
LA
89 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
90 if(level>av_log_level)
91 return;
4880cfd9 92 line[0]=0;
4b45de0e
LA
93#undef fprintf
94 if(print_prefix && avc) {
4880cfd9
MN
95 if(avc->version >= (50<<16 | 15<<8 | 3) && avc->parent_log_context_offset){
96 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
97 if(parent && *parent){
50061b62 98 snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent);
4880cfd9
MN
99 }
100 }
50061b62 101 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
4880cfd9 102 }
4b45de0e 103
b9c353ff 104 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
4b45de0e 105
b9c353ff 106 print_prefix= line[strlen(line)-1] == '\n';
c157fe63
MN
107
108#if HAVE_ISATTY
109 if(!detect_repeats) detect_repeats= isatty(2) ? 1 : -1;
110#endif
111
112 if(print_prefix && detect_repeats==1 && !strcmp(line, prev)){
b9c353ff 113 count++;
fd3064b6 114 fprintf(stderr, " Last message repeated %d times\r", count);
b9c353ff
MN
115 return;
116 }
117 if(count>0){
118 fprintf(stderr, " Last message repeated %d times\n", count);
119 count=0;
120 }
6e34a558 121 colored_fputs(av_clip(level>>3, 0, 6), line);
b9c353ff 122 strcpy(prev, line);
4b45de0e
LA
123}
124
125static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
126
127void av_log(void* avcl, int level, const char *fmt, ...)
128{
3fe1ec39 129 AVClass* avc= avcl ? *(AVClass**)avcl : NULL;
4b45de0e
LA
130 va_list vl;
131 va_start(vl, fmt);
3fe1ec39
MN
132 if(avc && avc->version >= (50<<16 | 15<<8 | 2) && avc->log_level_offset_offset && level>=AV_LOG_FATAL)
133 level += *(int*)(((uint8_t*)avcl) + avc->log_level_offset_offset);
4b45de0e
LA
134 av_vlog(avcl, level, fmt, vl);
135 va_end(vl);
136}
137
138void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
139{
140 av_log_callback(avcl, level, fmt, vl);
141}
142
143int av_log_get_level(void)
144{
145 return av_log_level;
146}
147
148void av_log_set_level(int level)
149{
150 av_log_level = level;
151}
152
153void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
154{
155 av_log_callback = callback;
156}