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.

327 lines
10KB

  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. #if HAVE_IO_H
  30. #include <io.h>
  31. #endif
  32. #include <stdarg.h>
  33. #include <stdlib.h>
  34. #include "avutil.h"
  35. #include "bprint.h"
  36. #include "common.h"
  37. #include "internal.h"
  38. #include "log.h"
  39. #define LINE_SZ 1024
  40. static int av_log_level = AV_LOG_INFO;
  41. static int flags;
  42. #if HAVE_SETCONSOLETEXTATTRIBUTE
  43. #include <windows.h>
  44. static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
  45. [AV_LOG_PANIC /8] = 12,
  46. [AV_LOG_FATAL /8] = 12,
  47. [AV_LOG_ERROR /8] = 12,
  48. [AV_LOG_WARNING/8] = 14,
  49. [AV_LOG_INFO /8] = 7,
  50. [AV_LOG_VERBOSE/8] = 10,
  51. [AV_LOG_DEBUG /8] = 10,
  52. [16+AV_CLASS_CATEGORY_NA ] = 7,
  53. [16+AV_CLASS_CATEGORY_INPUT ] = 13,
  54. [16+AV_CLASS_CATEGORY_OUTPUT ] = 5,
  55. [16+AV_CLASS_CATEGORY_MUXER ] = 13,
  56. [16+AV_CLASS_CATEGORY_DEMUXER ] = 5,
  57. [16+AV_CLASS_CATEGORY_ENCODER ] = 11,
  58. [16+AV_CLASS_CATEGORY_DECODER ] = 3,
  59. [16+AV_CLASS_CATEGORY_FILTER ] = 10,
  60. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 9,
  61. [16+AV_CLASS_CATEGORY_SWSCALER ] = 7,
  62. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 7,
  63. };
  64. static int16_t background, attr_orig;
  65. static HANDLE con;
  66. #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
  67. #define set_256color set_color
  68. #define reset_color() SetConsoleTextAttribute(con, attr_orig)
  69. #else
  70. static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
  71. [AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
  72. [AV_LOG_FATAL /8] = 208 << 8 | 0x41,
  73. [AV_LOG_ERROR /8] = 196 << 8 | 0x11,
  74. [AV_LOG_WARNING/8] = 226 << 8 | 0x03,
  75. [AV_LOG_INFO /8] = 253 << 8 | 0x09,
  76. [AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
  77. [AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
  78. [16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
  79. [16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
  80. [16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
  81. [16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
  82. [16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
  83. [16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
  84. [16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
  85. [16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
  86. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
  87. [16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
  88. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14,
  89. };
  90. #define set_color(x) fprintf(stderr, "\033[%d;3%dm", (color[x] >> 4) & 15, color[x] & 15)
  91. #define set_256color(x) fprintf(stderr, "\033[48;5;%dm\033[38;5;%dm", (color[x] >> 16) & 0xff, (color[x] >> 8) & 0xff)
  92. #define reset_color() fprintf(stderr, "\033[0m")
  93. #endif
  94. static int use_color = -1;
  95. static void colored_fputs(int level, const char *str)
  96. {
  97. if (use_color < 0) {
  98. #if HAVE_SETCONSOLETEXTATTRIBUTE
  99. CONSOLE_SCREEN_BUFFER_INFO con_info;
  100. con = GetStdHandle(STD_ERROR_HANDLE);
  101. use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
  102. !getenv("AV_LOG_FORCE_NOCOLOR");
  103. if (use_color) {
  104. GetConsoleScreenBufferInfo(con, &con_info);
  105. attr_orig = con_info.wAttributes;
  106. background = attr_orig & 0xF0;
  107. }
  108. #elif HAVE_ISATTY
  109. use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
  110. (getenv("TERM") && isatty(2) ||
  111. getenv("AV_LOG_FORCE_COLOR"));
  112. if (getenv("AV_LOG_FORCE_256COLOR"))
  113. use_color *= 256;
  114. #else
  115. use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
  116. !getenv("AV_LOG_FORCE_NOCOLOR");
  117. #endif
  118. }
  119. if (use_color == 1) {
  120. set_color(level);
  121. } else if (use_color == 256)
  122. set_256color(level);
  123. fputs(str, stderr);
  124. if (use_color) {
  125. reset_color();
  126. }
  127. }
  128. const char *av_default_item_name(void *ptr)
  129. {
  130. return (*(AVClass **) ptr)->class_name;
  131. }
  132. AVClassCategory av_default_get_category(void *ptr)
  133. {
  134. return (*(AVClass **) ptr)->category;
  135. }
  136. static void sanitize(uint8_t *line){
  137. while(*line){
  138. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  139. *line='?';
  140. line++;
  141. }
  142. }
  143. static int get_category(void *ptr){
  144. AVClass *avc = *(AVClass **) ptr;
  145. if( !avc
  146. || (avc->version&0xFF)<100
  147. || avc->version < (51 << 16 | 59 << 8)
  148. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  149. if(avc->get_category)
  150. return avc->get_category(ptr) + 16;
  151. return avc->category + 16;
  152. }
  153. static void format_line(void *ptr, int level, const char *fmt, va_list vl,
  154. AVBPrint part[3], int *print_prefix, int type[2])
  155. {
  156. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  157. av_bprint_init(part+0, 0, 1);
  158. av_bprint_init(part+1, 0, 1);
  159. av_bprint_init(part+2, 0, 65536);
  160. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  161. if (*print_prefix && avc) {
  162. if (avc->parent_log_context_offset) {
  163. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  164. avc->parent_log_context_offset);
  165. if (parent && *parent) {
  166. av_bprintf(part+0, "[%s @ %p] ",
  167. (*parent)->item_name(parent), parent);
  168. if(type) type[0] = get_category(parent);
  169. }
  170. }
  171. av_bprintf(part+1, "[%s @ %p] ",
  172. avc->item_name(ptr), ptr);
  173. if(type) type[1] = get_category(ptr);
  174. }
  175. av_vbprintf(part+2, fmt, vl);
  176. if(*part[0].str || *part[1].str || *part[2].str) {
  177. char lastc = part[2].len ? part[2].str[part[2].len - 1] : 0;
  178. *print_prefix = lastc == '\n' || lastc == '\r';
  179. }
  180. }
  181. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  182. char *line, int line_size, int *print_prefix)
  183. {
  184. AVBPrint part[3];
  185. format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
  186. snprintf(line, line_size, "%s%s%s", part[0].str, part[1].str, part[2].str);
  187. av_bprint_finalize(part+2, NULL);
  188. }
  189. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  190. {
  191. static int print_prefix = 1;
  192. static int count;
  193. static char prev[LINE_SZ];
  194. AVBPrint part[3];
  195. char line[LINE_SZ];
  196. static int is_atty;
  197. int type[2];
  198. if (level > av_log_level)
  199. return;
  200. format_line(ptr, level, fmt, vl, part, &print_prefix, type);
  201. snprintf(line, sizeof(line), "%s%s%s", part[0].str, part[1].str, part[2].str);
  202. #if HAVE_ISATTY
  203. if (!is_atty)
  204. is_atty = isatty(2) ? 1 : -1;
  205. #endif
  206. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
  207. *line && line[strlen(line) - 1] != '\r'){
  208. count++;
  209. if (is_atty == 1)
  210. fprintf(stderr, " Last message repeated %d times\r", count);
  211. av_bprint_finalize(part+2, NULL);
  212. return;
  213. }
  214. if (count > 0) {
  215. fprintf(stderr, " Last message repeated %d times\n", count);
  216. count = 0;
  217. }
  218. strcpy(prev, line);
  219. sanitize(part[0].str);
  220. colored_fputs(type[0], part[0].str);
  221. sanitize(part[1].str);
  222. colored_fputs(type[1], part[1].str);
  223. sanitize(part[2].str);
  224. colored_fputs(av_clip(level >> 3, 0, 6), part[2].str);
  225. av_bprint_finalize(part+2, NULL);
  226. }
  227. static void (*av_log_callback)(void*, int, const char*, va_list) =
  228. av_log_default_callback;
  229. void av_log(void* avcl, int level, const char *fmt, ...)
  230. {
  231. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  232. va_list vl;
  233. va_start(vl, fmt);
  234. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  235. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  236. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  237. av_vlog(avcl, level, fmt, vl);
  238. va_end(vl);
  239. }
  240. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  241. {
  242. void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
  243. if (log_callback)
  244. log_callback(avcl, level, fmt, vl);
  245. }
  246. int av_log_get_level(void)
  247. {
  248. return av_log_level;
  249. }
  250. void av_log_set_level(int level)
  251. {
  252. av_log_level = level;
  253. }
  254. void av_log_set_flags(int arg)
  255. {
  256. flags = arg;
  257. }
  258. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  259. {
  260. av_log_callback = callback;
  261. }
  262. static void missing_feature_sample(int sample, void *avc, const char *msg,
  263. va_list argument_list)
  264. {
  265. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  266. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
  267. "version to the newest one from Git. If the problem still "
  268. "occurs, it means that your file has a feature which has not "
  269. "been implemented.\n");
  270. if (sample)
  271. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  272. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  273. "and contact the ffmpeg-devel mailing list.\n");
  274. }
  275. void avpriv_request_sample(void *avc, const char *msg, ...)
  276. {
  277. va_list argument_list;
  278. va_start(argument_list, msg);
  279. missing_feature_sample(1, avc, msg, argument_list);
  280. va_end(argument_list);
  281. }
  282. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  283. {
  284. va_list argument_list;
  285. va_start(argument_list, msg);
  286. missing_feature_sample(0, avc, msg, argument_list);
  287. va_end(argument_list);
  288. }