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.

255 lines
7.7KB

  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 ] = 3,
  43. [16+AV_CLASS_CATEGORY_OUTPUT ] = 11,
  44. [16+AV_CLASS_CATEGORY_MUXER ] = 3,
  45. [16+AV_CLASS_CATEGORY_DEMUXER ] = 11,
  46. [16+AV_CLASS_CATEGORY_ENCODER ] = 5,
  47. [16+AV_CLASS_CATEGORY_DECODER ] = 13,
  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 ] = 0x06,
  66. [16+AV_CLASS_CATEGORY_OUTPUT ] = 0x16,
  67. [16+AV_CLASS_CATEGORY_MUXER ] = 0x06,
  68. [16+AV_CLASS_CATEGORY_DEMUXER ] = 0x16,
  69. [16+AV_CLASS_CATEGORY_ENCODER ] = 0x05,
  70. [16+AV_CLASS_CATEGORY_DECODER ] = 0x15,
  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. static void sanitize(uint8_t *line){
  114. while(*line){
  115. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  116. *line='?';
  117. line++;
  118. }
  119. }
  120. static int get_category(AVClass *avc){
  121. if( !avc
  122. || (avc->version&0xFF)<100
  123. || avc->version < (51 << 16 | 56 << 8)
  124. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  125. return avc->category + 16;
  126. }
  127. static void format_line(void *ptr, int level, const char *fmt, va_list vl,
  128. char part[3][512], int part_size, int *print_prefix, int type[2])
  129. {
  130. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  131. part[0][0] = part[1][0] = part[2][0] = 0;
  132. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  133. if (*print_prefix && avc) {
  134. if (avc->parent_log_context_offset) {
  135. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  136. avc->parent_log_context_offset);
  137. if (parent && *parent) {
  138. snprintf(part[0], part_size, "[%s @ %p] ",
  139. (*parent)->item_name(parent), parent);
  140. if(type) type[0] = get_category(*parent);
  141. }
  142. }
  143. snprintf(part[1], part_size, "[%s @ %p] ",
  144. avc->item_name(ptr), ptr);
  145. if(type) type[1] = get_category(avc);
  146. }
  147. vsnprintf(part[2], part_size, fmt, vl);
  148. *print_prefix = strlen(part[2]) && part[2][strlen(part[2]) - 1] == '\n';
  149. }
  150. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  151. char *line, int line_size, int *print_prefix)
  152. {
  153. char part[3][512];
  154. format_line(ptr, level, fmt, vl, part, sizeof(part[0]), print_prefix, NULL);
  155. snprintf(line, line_size, "%s%s%s", part[0], part[1], part[2]);
  156. }
  157. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  158. {
  159. static int print_prefix = 1;
  160. static int count;
  161. static char prev[1024];
  162. char part[3][512];
  163. char line[1024];
  164. static int is_atty;
  165. int type[2];
  166. if (level > av_log_level)
  167. return;
  168. format_line(ptr, level, fmt, vl, part, sizeof(part[0]), &print_prefix, type);
  169. snprintf(line, sizeof(line), "%s%s%s", part[0], part[1], part[2]);
  170. #if HAVE_ISATTY
  171. if (!is_atty)
  172. is_atty = isatty(2) ? 1 : -1;
  173. #endif
  174. #undef fprintf
  175. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
  176. count++;
  177. if (is_atty == 1)
  178. fprintf(stderr, " Last message repeated %d times\r", count);
  179. return;
  180. }
  181. if (count > 0) {
  182. fprintf(stderr, " Last message repeated %d times\n", count);
  183. count = 0;
  184. }
  185. strcpy(prev, line);
  186. sanitize(part[0]);
  187. colored_fputs(type[0], part[0]);
  188. sanitize(part[1]);
  189. colored_fputs(type[1], part[1]);
  190. sanitize(part[2]);
  191. colored_fputs(av_clip(level >> 3, 0, 6), part[2]);
  192. }
  193. static void (*av_log_callback)(void*, int, const char*, va_list) =
  194. av_log_default_callback;
  195. void av_log(void* avcl, int level, const char *fmt, ...)
  196. {
  197. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  198. va_list vl;
  199. va_start(vl, fmt);
  200. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  201. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  202. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  203. av_vlog(avcl, level, fmt, vl);
  204. va_end(vl);
  205. }
  206. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  207. {
  208. if(av_log_callback)
  209. av_log_callback(avcl, level, fmt, vl);
  210. }
  211. int av_log_get_level(void)
  212. {
  213. return av_log_level;
  214. }
  215. void av_log_set_level(int level)
  216. {
  217. av_log_level = level;
  218. }
  219. void av_log_set_flags(int arg)
  220. {
  221. flags = arg;
  222. }
  223. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  224. {
  225. av_log_callback = callback;
  226. }