Code clean-up for crc.c, lfg.c, log.c, random_see.d, rational.c and tree.c.
[libav.git] / libavutil / log.c
CommitLineData
4b45de0e
LA
1/*
2 * log functions
3 * Copyright (c) 2003 Michel Bardiaux
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; 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>
1a5e4fd8 29#include "avstring.h"
4b45de0e 30#include "avutil.h"
77652a6a 31#include "log.h"
4b45de0e 32
490a022d 33static int av_log_level = AV_LOG_INFO;
1c1c80f0 34static int flags;
4b45de0e 35
3c467bac 36#if defined(_WIN32) && !defined(__MINGW32CE__)
4855f867 37#include <windows.h>
425b45d4 38static const uint8_t color[] = { 12, 12, 12, 14, 7, 7, 7 };
4855f867
RP
39static int16_t background, attr_orig;
40static HANDLE con;
db16e3ca 41#define set_color(x) SetConsoleTextAttribute(con, background | color[x])
4855f867
RP
42#define reset_color() SetConsoleTextAttribute(con, attr_orig)
43#else
425b45d4
YM
44static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 9, 9 };
45#define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
4855f867
RP
46#define reset_color() fprintf(stderr, "\033[0m")
47#endif
425b45d4 48static int use_color = -1;
51e026d1
MN
49
50#undef fprintf
425b45d4
YM
51static void colored_fputs(int level, const char *str)
52{
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);
425b45d4
YM
57 use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
58 !getenv("AV_LOG_FORCE_NOCOLOR");
4855f867
RP
59 if (use_color) {
60 GetConsoleScreenBufferInfo(con, &con_info);
61 attr_orig = con_info.wAttributes;
62 background = attr_orig & 0xF0;
63 }
64#elif HAVE_ISATTY
425b45d4
YM
65 use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
66 (getenv("TERM") && isatty(2) ||
67 getenv("AV_LOG_FORCE_COLOR"));
62044024 68#else
425b45d4
YM
69 use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
70 !getenv("AV_LOG_FORCE_NOCOLOR");
62044024
MN
71#endif
72 }
73
425b45d4 74 if (use_color) {
4855f867 75 set_color(level);
51e026d1
MN
76 }
77 fputs(str, stderr);
425b45d4 78 if (use_color) {
4855f867 79 reset_color();
51e026d1
MN
80 }
81}
82
425b45d4
YM
83const char *av_default_item_name(void *ptr)
84{
85 return (*(AVClass **) ptr)->class_name;
8d2a5139
MN
86}
87
411983c1 88void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
4b45de0e 89{
425b45d4 90 static int print_prefix = 1;
b9c353ff 91 static int count;
0247bdee
RT
92 static char prev[1024];
93 char line[1024];
8a190533 94 static int is_atty;
425b45d4
YM
95 AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
96 if (level > av_log_level)
4b45de0e 97 return;
425b45d4 98 line[0] = 0;
4b45de0e 99#undef fprintf
425b45d4 100 if (print_prefix && avc) {
63de9e7d 101 if (avc->parent_log_context_offset) {
425b45d4
YM
102 AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
103 avc->parent_log_context_offset);
104 if (parent && *parent) {
105 snprintf(line, sizeof(line), "[%s @ %p] ",
106 (*parent)->item_name(parent), parent);
4880cfd9
MN
107 }
108 }
425b45d4
YM
109 snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
110 avc->item_name(ptr), ptr);
4880cfd9 111 }
4b45de0e 112
b9c353ff 113 vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
4b45de0e 114
425b45d4 115 print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
c157fe63
MN
116
117#if HAVE_ISATTY
425b45d4
YM
118 if (!is_atty)
119 is_atty = isatty(2) ? 1 : -1;
c157fe63
MN
120#endif
121
425b45d4
YM
122 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
123 !strncmp(line, prev, sizeof line)) {
b9c353ff 124 count++;
425b45d4 125 if (is_atty == 1)
d7cd001f 126 fprintf(stderr, " Last message repeated %d times\r", count);
b9c353ff
MN
127 return;
128 }
425b45d4 129 if (count > 0) {
b9c353ff 130 fprintf(stderr, " Last message repeated %d times\n", count);
425b45d4 131 count = 0;
b9c353ff 132 }
425b45d4 133 colored_fputs(av_clip(level >> 3, 0, 6), line);
1a5e4fd8 134 av_strlcpy(prev, line, sizeof line);
4b45de0e
LA
135}
136
425b45d4
YM
137static void (*av_log_callback)(void*, int, const char*, va_list) =
138 av_log_default_callback;
4b45de0e
LA
139
140void av_log(void* avcl, int level, const char *fmt, ...)
141{
425b45d4 142 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
4b45de0e
LA
143 va_list vl;
144 va_start(vl, fmt);
425b45d4
YM
145 if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
146 avc->log_level_offset_offset && level >= AV_LOG_FATAL)
147 level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
4b45de0e
LA
148 av_vlog(avcl, level, fmt, vl);
149 va_end(vl);
150}
151
152void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
153{
154 av_log_callback(avcl, level, fmt, vl);
155}
156
157int av_log_get_level(void)
158{
159 return av_log_level;
160}
161
162void av_log_set_level(int level)
163{
164 av_log_level = level;
165}
166
1c1c80f0
MN
167void av_log_set_flags(int arg)
168{
425b45d4 169 flags = arg;
1c1c80f0
MN
170}
171
4b45de0e
LA
172void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
173{
174 av_log_callback = callback;
175}