You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.9KB

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