log.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * log functions
  3. * Copyright (c) 2003 Michel Bardiaux
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * logging functions
  24. */
  25. #include "config.h"
  26. #if HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #include <stdlib.h>
  30. #include "avutil.h"
  31. #include "common.h"
  32. #include "log.h"
  33. #define LINE_SZ 1024
  34. static int av_log_level = AV_LOG_INFO;
  35. static int flags;
  36. #if defined(_WIN32) && !defined(__MINGW32CE__)
  37. #include <windows.h>
  38. #include <io.h>
  39. static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
  40. [AV_LOG_PANIC /8] = 12,
  41. [AV_LOG_FATAL /8] = 12,
  42. [AV_LOG_ERROR /8] = 12,
  43. [AV_LOG_WARNING/8] = 14,
  44. [AV_LOG_INFO /8] = 7,
  45. [AV_LOG_VERBOSE/8] = 10,
  46. [AV_LOG_DEBUG /8] = 10,
  47. [16+AV_CLASS_CATEGORY_NA ] = 7,
  48. [16+AV_CLASS_CATEGORY_INPUT ] = 13,
  49. [16+AV_CLASS_CATEGORY_OUTPUT ] = 5,
  50. [16+AV_CLASS_CATEGORY_MUXER ] = 13,
  51. [16+AV_CLASS_CATEGORY_DEMUXER ] = 5,
  52. [16+AV_CLASS_CATEGORY_ENCODER ] = 11,
  53. [16+AV_CLASS_CATEGORY_DECODER ] = 3,
  54. [16+AV_CLASS_CATEGORY_FILTER ] = 10,
  55. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 9,
  56. [16+AV_CLASS_CATEGORY_SWSCALER ] = 7,
  57. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 7,
  58. };
  59. static int16_t background, attr_orig;
  60. static HANDLE con;
  61. #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
  62. #define set_256color set_color
  63. #define reset_color() SetConsoleTextAttribute(con, attr_orig)
  64. #else
  65. static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
  66. [AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
  67. [AV_LOG_FATAL /8] = 208 << 8 | 0x41,
  68. [AV_LOG_ERROR /8] = 196 << 8 | 0x11,
  69. [AV_LOG_WARNING/8] = 226 << 8 | 0x03,
  70. [AV_LOG_INFO /8] = 253 << 8 | 0x09,
  71. [AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
  72. [AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
  73. [16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
  74. [16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
  75. [16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
  76. [16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
  77. [16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
  78. [16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
  79. [16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
  80. [16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
  81. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
  82. [16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
  83. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14,
  84. };
  85. #define set_color(x) fprintf(stderr, "\033[%d;3%dm", (color[x] >> 4) & 15, color[x] & 15)
  86. #define set_256color(x) fprintf(stderr, "\033[48;5;%dm\033[38;5;%dm", (color[x] >> 16) & 0xff, (color[x] >> 8) & 0xff)
  87. #define reset_color() fprintf(stderr, "\033[0m")
  88. #endif
  89. static int use_color = -1;
  90. #undef fprintf
  91. static void colored_fputs(int level, const char *str)
  92. {
  93. if (!*str)
  94. return;
  95. if (use_color < 0) {
  96. #if defined(_WIN32) && !defined(__MINGW32CE__)
  97. CONSOLE_SCREEN_BUFFER_INFO con_info;
  98. con = GetStdHandle(STD_ERROR_HANDLE);
  99. use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
  100. !getenv("AV_LOG_FORCE_NOCOLOR");
  101. if (use_color) {
  102. GetConsoleScreenBufferInfo(con, &con_info);
  103. attr_orig = con_info.wAttributes;
  104. background = attr_orig & 0xF0;
  105. }
  106. #elif HAVE_ISATTY
  107. use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
  108. (getenv("TERM") && isatty(2) ||
  109. getenv("AV_LOG_FORCE_COLOR"));
  110. if (getenv("AV_LOG_FORCE_256COLOR"))
  111. use_color *= 256;
  112. #else
  113. use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
  114. !getenv("AV_LOG_FORCE_NOCOLOR");
  115. #endif
  116. }
  117. if (use_color == 1) {
  118. set_color(level);
  119. } else if (use_color == 256)
  120. set_256color(level);
  121. fputs(str, stderr);
  122. if (use_color) {
  123. reset_color();
  124. }
  125. }
  126. const char *av_default_item_name(void *ptr)
  127. {
  128. return (*(AVClass **) ptr)->class_name;
  129. }
  130. AVClassCategory av_default_get_category(void *ptr)
  131. {
  132. return (*(AVClass **) ptr)->category;
  133. }
  134. static void sanitize(uint8_t *line){
  135. while(*line){
  136. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  137. *line='?';
  138. line++;
  139. }
  140. }
  141. static int get_category(void *ptr){
  142. AVClass *avc = *(AVClass **) ptr;
  143. if( !avc
  144. || (avc->version&0xFF)<100
  145. || avc->version < (51 << 16 | 59 << 8)
  146. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  147. if(avc->get_category)
  148. return avc->get_category(ptr) + 16;
  149. return avc->category + 16;
  150. }
  151. static void format_line(void *ptr, int level, const char *fmt, va_list vl,
  152. char part[3][LINE_SZ], int part_size, int *print_prefix, int type[2])
  153. {
  154. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  155. part[0][0] = part[1][0] = part[2][0] = 0;
  156. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  157. if (*print_prefix && avc) {
  158. if (avc->parent_log_context_offset) {
  159. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  160. avc->parent_log_context_offset);
  161. if (parent && *parent) {
  162. snprintf(part[0], part_size, "[%s @ %p] ",
  163. (*parent)->item_name(parent), parent);
  164. if(type) type[0] = get_category(parent);
  165. }
  166. }
  167. snprintf(part[1], part_size, "[%s @ %p] ",
  168. avc->item_name(ptr), ptr);
  169. if(type) type[1] = get_category(ptr);
  170. }
  171. vsnprintf(part[2], part_size, fmt, vl);
  172. *print_prefix = strlen(part[2]) && part[2][strlen(part[2]) - 1] == '\n';
  173. }
  174. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  175. char *line, int line_size, int *print_prefix)
  176. {
  177. char part[3][LINE_SZ];
  178. format_line(ptr, level, fmt, vl, part, sizeof(part[0]), print_prefix, NULL);
  179. snprintf(line, line_size, "%s%s%s", part[0], part[1], part[2]);
  180. }
  181. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  182. {
  183. static int print_prefix = 1;
  184. static int count;
  185. static char prev[LINE_SZ];
  186. char part[3][LINE_SZ];
  187. char line[LINE_SZ];
  188. static int is_atty;
  189. int type[2];
  190. if (level > av_log_level)
  191. return;
  192. format_line(ptr, level, fmt, vl, part, sizeof(part[0]), &print_prefix, type);
  193. snprintf(line, sizeof(line), "%s%s%s", part[0], part[1], part[2]);
  194. #if HAVE_ISATTY
  195. if (!is_atty)
  196. is_atty = isatty(2) ? 1 : -1;
  197. #endif
  198. #undef fprintf
  199. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
  200. count++;
  201. if (is_atty == 1)
  202. fprintf(stderr, " Last message repeated %d times\r", count);
  203. return;
  204. }
  205. if (count > 0) {
  206. fprintf(stderr, " Last message repeated %d times\n", count);
  207. count = 0;
  208. }
  209. strcpy(prev, line);
  210. sanitize(part[0]);
  211. colored_fputs(type[0], part[0]);
  212. sanitize(part[1]);
  213. colored_fputs(type[1], part[1]);
  214. sanitize(part[2]);
  215. colored_fputs(av_clip(level >> 3, 0, 6), part[2]);
  216. }
  217. static void (*av_log_callback)(void*, int, const char*, va_list) =
  218. av_log_default_callback;
  219. void av_log(void* avcl, int level, const char *fmt, ...)
  220. {
  221. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  222. va_list vl;
  223. va_start(vl, fmt);
  224. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  225. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  226. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  227. av_vlog(avcl, level, fmt, vl);
  228. va_end(vl);
  229. }
  230. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  231. {
  232. if(av_log_callback)
  233. av_log_callback(avcl, level, fmt, vl);
  234. }
  235. int av_log_get_level(void)
  236. {
  237. return av_log_level;
  238. }
  239. void av_log_set_level(int level)
  240. {
  241. av_log_level = level;
  242. }
  243. void av_log_set_flags(int arg)
  244. {
  245. flags = arg;
  246. }
  247. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  248. {
  249. av_log_callback = callback;
  250. }