3 * Copyright (c) 2003 Michel Bardiaux
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
41 static int av_log_level
= AV_LOG_INFO
;
44 #if HAVE_SETCONSOLETEXTATTRIBUTE
46 static const uint8_t color
[] = { 12, 12, 12, 14, 7, 10, 11 };
47 static int16_t background
, attr_orig
;
49 #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
50 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
52 static const uint8_t color
[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
53 #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
54 #define reset_color() fprintf(stderr, "\033[0m")
56 static int use_color
= -1;
58 static void colored_fputs(int level
, const char *str
)
61 #if HAVE_SETCONSOLETEXTATTRIBUTE
62 CONSOLE_SCREEN_BUFFER_INFO con_info
;
63 con
= GetStdHandle(STD_ERROR_HANDLE
);
64 use_color
= (con
!= INVALID_HANDLE_VALUE
) && !getenv("NO_COLOR") &&
65 !getenv("AV_LOG_FORCE_NOCOLOR");
67 GetConsoleScreenBufferInfo(con
, &con_info
);
68 attr_orig
= con_info
.wAttributes
;
69 background
= attr_orig
& 0xF0;
72 use_color
= !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
73 (getenv("TERM") && isatty(2) ||
74 getenv("AV_LOG_FORCE_COLOR"));
76 use_color
= getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
77 !getenv("AV_LOG_FORCE_NOCOLOR");
90 const char *av_default_item_name(void *ptr
)
92 return (*(AVClass
**) ptr
)->class_name
;
95 void av_log_default_callback(void* ptr
, int level
, const char* fmt
, va_list vl
)
97 static int print_prefix
= 1;
99 static char prev
[1024];
102 AVClass
* avc
= ptr
? *(AVClass
**) ptr
: NULL
;
103 if (level
> av_log_level
)
106 if (print_prefix
&& avc
) {
107 if (avc
->parent_log_context_offset
) {
108 AVClass
** parent
= *(AVClass
***) (((uint8_t *) ptr
) +
109 avc
->parent_log_context_offset
);
110 if (parent
&& *parent
) {
111 snprintf(line
, sizeof(line
), "[%s @ %p] ",
112 (*parent
)->item_name(parent
), parent
);
115 snprintf(line
+ strlen(line
), sizeof(line
) - strlen(line
), "[%s @ %p] ",
116 avc
->item_name(ptr
), ptr
);
119 vsnprintf(line
+ strlen(line
), sizeof(line
) - strlen(line
), fmt
, vl
);
121 print_prefix
= strlen(line
) && line
[strlen(line
) - 1] == '\n';
125 is_atty
= isatty(2) ? 1 : -1;
128 if (print_prefix
&& (flags
& AV_LOG_SKIP_REPEATED
) &&
129 !strncmp(line
, prev
, sizeof line
)) {
132 fprintf(stderr
, " Last message repeated %d times\r", count
);
136 fprintf(stderr
, " Last message repeated %d times\n", count
);
139 colored_fputs(av_clip(level
>> 3, 0, 6), line
);
140 av_strlcpy(prev
, line
, sizeof line
);
143 static void (*av_log_callback
)(void*, int, const char*, va_list) =
144 av_log_default_callback
;
146 void av_log(void* avcl
, int level
, const char *fmt
, ...)
148 AVClass
* avc
= avcl
? *(AVClass
**) avcl
: NULL
;
151 if (avc
&& avc
->version
>= (50 << 16 | 15 << 8 | 2) &&
152 avc
->log_level_offset_offset
&& level
>= AV_LOG_FATAL
)
153 level
+= *(int *) (((uint8_t *) avcl
) + avc
->log_level_offset_offset
);
154 av_vlog(avcl
, level
, fmt
, vl
);
158 void av_vlog(void* avcl
, int level
, const char *fmt
, va_list vl
)
160 av_log_callback(avcl
, level
, fmt
, vl
);
163 int av_log_get_level(void)
168 void av_log_set_level(int level
)
170 av_log_level
= level
;
173 void av_log_set_flags(int arg
)
178 void av_log_set_callback(void (*callback
)(void*, int, const char*, va_list))
180 av_log_callback
= callback
;