]> git.sesse.net Git - ffmpeg/blob - libavutil/log.c
blowfish: Add more tests
[ffmpeg] / libavutil / log.c
1 /*
2  * log functions
3  * Copyright (c) 2003 Michel Bardiaux
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * logging functions
25  */
26
27 #include "config.h"
28
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <stdlib.h>
33 #include "avstring.h"
34 #include "avutil.h"
35 #include "common.h"
36 #include "log.h"
37
38 static int av_log_level = AV_LOG_INFO;
39 static int flags;
40
41 #if defined(_WIN32) && !defined(__MINGW32CE__)
42 #include <windows.h>
43 #include <io.h>
44 static const uint8_t color[] = { 12, 12, 12, 14, 7, 10, 11 };
45 static int16_t background, attr_orig;
46 static HANDLE con;
47 #define set_color(x)  SetConsoleTextAttribute(con, background | color[x])
48 #define reset_color() SetConsoleTextAttribute(con, attr_orig)
49 #else
50 static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
51 #define set_color(x)  fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
52 #define reset_color() fprintf(stderr, "\033[0m")
53 #endif
54 static int use_color = -1;
55
56 #undef fprintf
57 static void colored_fputs(int level, const char *str)
58 {
59     if (use_color < 0) {
60 #if defined(_WIN32) && !defined(__MINGW32CE__)
61         CONSOLE_SCREEN_BUFFER_INFO con_info;
62         con = GetStdHandle(STD_ERROR_HANDLE);
63         use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
64                     !getenv("AV_LOG_FORCE_NOCOLOR");
65         if (use_color) {
66             GetConsoleScreenBufferInfo(con, &con_info);
67             attr_orig  = con_info.wAttributes;
68             background = attr_orig & 0xF0;
69         }
70 #elif HAVE_ISATTY
71         use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
72                     (getenv("TERM") && isatty(2) ||
73                      getenv("AV_LOG_FORCE_COLOR"));
74 #else
75         use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
76                    !getenv("AV_LOG_FORCE_NOCOLOR");
77 #endif
78     }
79
80     if (use_color) {
81         set_color(level);
82     }
83     fputs(str, stderr);
84     if (use_color) {
85         reset_color();
86     }
87 }
88
89 const char *av_default_item_name(void *ptr)
90 {
91     return (*(AVClass **) ptr)->class_name;
92 }
93
94 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
95 {
96     static int print_prefix = 1;
97     static int count;
98     static char prev[1024];
99     char line[1024];
100     static int is_atty;
101     AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
102     if (level > av_log_level)
103         return;
104     line[0] = 0;
105 #undef fprintf
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);
113             }
114         }
115         snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
116                  avc->item_name(ptr), ptr);
117     }
118
119     vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
120
121     print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
122
123 #if HAVE_ISATTY
124     if (!is_atty)
125         is_atty = isatty(2) ? 1 : -1;
126 #endif
127
128     if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
129         !strncmp(line, prev, sizeof line)) {
130         count++;
131         if (is_atty == 1)
132             fprintf(stderr, "    Last message repeated %d times\r", count);
133         return;
134     }
135     if (count > 0) {
136         fprintf(stderr, "    Last message repeated %d times\n", count);
137         count = 0;
138     }
139     colored_fputs(av_clip(level >> 3, 0, 6), line);
140     av_strlcpy(prev, line, sizeof line);
141 }
142
143 static void (*av_log_callback)(void*, int, const char*, va_list) =
144     av_log_default_callback;
145
146 void av_log(void* avcl, int level, const char *fmt, ...)
147 {
148     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
149     va_list vl;
150     va_start(vl, fmt);
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);
155     va_end(vl);
156 }
157
158 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
159 {
160     av_log_callback(avcl, level, fmt, vl);
161 }
162
163 int av_log_get_level(void)
164 {
165     return av_log_level;
166 }
167
168 void av_log_set_level(int level)
169 {
170     av_log_level = level;
171 }
172
173 void av_log_set_flags(int arg)
174 {
175     flags = arg;
176 }
177
178 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
179 {
180     av_log_callback = callback;
181 }