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.

491 lines
15KB

  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. #include "thread.h"
  40. static AVMutex mutex = AV_MUTEX_INITIALIZER;
  41. #define LINE_SZ 1024
  42. #if HAVE_VALGRIND_VALGRIND_H
  43. #include <valgrind/valgrind.h>
  44. /* this is the log level at which valgrind will output a full backtrace */
  45. #define BACKTRACE_LOGLEVEL AV_LOG_ERROR
  46. #endif
  47. static int av_log_level = AV_LOG_INFO;
  48. static int flags;
  49. #define NB_LEVELS 8
  50. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
  51. #include <windows.h>
  52. static const uint8_t color[16 + AV_CLASS_CATEGORY_NB] = {
  53. [AV_LOG_PANIC /8] = 12,
  54. [AV_LOG_FATAL /8] = 12,
  55. [AV_LOG_ERROR /8] = 12,
  56. [AV_LOG_WARNING/8] = 14,
  57. [AV_LOG_INFO /8] = 7,
  58. [AV_LOG_VERBOSE/8] = 10,
  59. [AV_LOG_DEBUG /8] = 10,
  60. [AV_LOG_TRACE /8] = 8,
  61. [16+AV_CLASS_CATEGORY_NA ] = 7,
  62. [16+AV_CLASS_CATEGORY_INPUT ] = 13,
  63. [16+AV_CLASS_CATEGORY_OUTPUT ] = 5,
  64. [16+AV_CLASS_CATEGORY_MUXER ] = 13,
  65. [16+AV_CLASS_CATEGORY_DEMUXER ] = 5,
  66. [16+AV_CLASS_CATEGORY_ENCODER ] = 11,
  67. [16+AV_CLASS_CATEGORY_DECODER ] = 3,
  68. [16+AV_CLASS_CATEGORY_FILTER ] = 10,
  69. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 9,
  70. [16+AV_CLASS_CATEGORY_SWSCALER ] = 7,
  71. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 7,
  72. [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 13,
  73. [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT ] = 5,
  74. [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 13,
  75. [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT ] = 5,
  76. [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT ] = 13,
  77. [16+AV_CLASS_CATEGORY_DEVICE_INPUT ] = 5,
  78. };
  79. static int16_t background, attr_orig;
  80. static HANDLE con;
  81. #else
  82. static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
  83. [AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
  84. [AV_LOG_FATAL /8] = 208 << 8 | 0x41,
  85. [AV_LOG_ERROR /8] = 196 << 8 | 0x11,
  86. [AV_LOG_WARNING/8] = 226 << 8 | 0x03,
  87. [AV_LOG_INFO /8] = 253 << 8 | 0x09,
  88. [AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
  89. [AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
  90. [AV_LOG_TRACE /8] = 34 << 8 | 0x07,
  91. [16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
  92. [16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
  93. [16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
  94. [16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
  95. [16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
  96. [16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
  97. [16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
  98. [16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
  99. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
  100. [16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
  101. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14,
  102. [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT ] = 213 << 8 | 0x15,
  103. [16+AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT ] = 207 << 8 | 0x05,
  104. [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT ] = 213 << 8 | 0x15,
  105. [16+AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT ] = 207 << 8 | 0x05,
  106. [16+AV_CLASS_CATEGORY_DEVICE_OUTPUT ] = 213 << 8 | 0x15,
  107. [16+AV_CLASS_CATEGORY_DEVICE_INPUT ] = 207 << 8 | 0x05,
  108. };
  109. #endif
  110. static int use_color = -1;
  111. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
  112. static void win_console_puts(const char *str)
  113. {
  114. const uint8_t *q = str;
  115. uint16_t line[LINE_SZ];
  116. while (*q) {
  117. uint16_t *buf = line;
  118. DWORD nb_chars = 0;
  119. DWORD written;
  120. while (*q && nb_chars < LINE_SZ - 1) {
  121. uint32_t ch;
  122. uint16_t tmp;
  123. GET_UTF8(ch, *q ? *q++ : 0, ch = 0xfffd; goto continue_on_invalid;)
  124. continue_on_invalid:
  125. PUT_UTF16(ch, tmp, *buf++ = tmp; nb_chars++;)
  126. }
  127. WriteConsoleW(con, line, nb_chars, &written, NULL);
  128. }
  129. }
  130. #endif
  131. static void check_color_terminal(void)
  132. {
  133. char *term = getenv("TERM");
  134. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
  135. CONSOLE_SCREEN_BUFFER_INFO con_info;
  136. DWORD dummy;
  137. con = GetStdHandle(STD_ERROR_HANDLE);
  138. if (con != INVALID_HANDLE_VALUE && !GetConsoleMode(con, &dummy))
  139. con = INVALID_HANDLE_VALUE;
  140. if (con != INVALID_HANDLE_VALUE) {
  141. GetConsoleScreenBufferInfo(con, &con_info);
  142. attr_orig = con_info.wAttributes;
  143. background = attr_orig & 0xF0;
  144. }
  145. #endif
  146. if (getenv("AV_LOG_FORCE_NOCOLOR")) {
  147. use_color = 0;
  148. } else if (getenv("AV_LOG_FORCE_COLOR")) {
  149. use_color = 1;
  150. } else {
  151. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
  152. use_color = (con != INVALID_HANDLE_VALUE);
  153. #elif HAVE_ISATTY
  154. use_color = (term && isatty(2));
  155. #else
  156. use_color = 0;
  157. #endif
  158. }
  159. if (getenv("AV_LOG_FORCE_256COLOR") || term && strstr(term, "256color"))
  160. use_color *= 256;
  161. }
  162. static void ansi_fputs(int level, int tint, const char *str, int local_use_color)
  163. {
  164. if (local_use_color == 1) {
  165. fprintf(stderr,
  166. "\033[%"PRIu32";3%"PRIu32"m%s\033[0m",
  167. (color[level] >> 4) & 15,
  168. color[level] & 15,
  169. str);
  170. } else if (tint && use_color == 256) {
  171. fprintf(stderr,
  172. "\033[48;5;%"PRIu32"m\033[38;5;%dm%s\033[0m",
  173. (color[level] >> 16) & 0xff,
  174. tint,
  175. str);
  176. } else if (local_use_color == 256) {
  177. fprintf(stderr,
  178. "\033[48;5;%"PRIu32"m\033[38;5;%"PRIu32"m%s\033[0m",
  179. (color[level] >> 16) & 0xff,
  180. (color[level] >> 8) & 0xff,
  181. str);
  182. } else
  183. fputs(str, stderr);
  184. }
  185. static void colored_fputs(int level, int tint, const char *str)
  186. {
  187. int local_use_color;
  188. if (!*str)
  189. return;
  190. if (use_color < 0)
  191. check_color_terminal();
  192. if (level == AV_LOG_INFO/8) local_use_color = 0;
  193. else local_use_color = use_color;
  194. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE && HAVE_GETSTDHANDLE
  195. if (con != INVALID_HANDLE_VALUE) {
  196. if (local_use_color)
  197. SetConsoleTextAttribute(con, background | color[level]);
  198. win_console_puts(str);
  199. if (local_use_color)
  200. SetConsoleTextAttribute(con, attr_orig);
  201. } else {
  202. ansi_fputs(level, tint, str, local_use_color);
  203. }
  204. #else
  205. ansi_fputs(level, tint, str, local_use_color);
  206. #endif
  207. }
  208. const char *av_default_item_name(void *ptr)
  209. {
  210. return (*(AVClass **) ptr)->class_name;
  211. }
  212. AVClassCategory av_default_get_category(void *ptr)
  213. {
  214. return (*(AVClass **) ptr)->category;
  215. }
  216. static void sanitize(uint8_t *line){
  217. while(*line){
  218. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  219. *line='?';
  220. line++;
  221. }
  222. }
  223. static int get_category(void *ptr){
  224. AVClass *avc = *(AVClass **) ptr;
  225. if( !avc
  226. || (avc->version&0xFF)<100
  227. || avc->version < (51 << 16 | 59 << 8)
  228. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  229. if(avc->get_category)
  230. return avc->get_category(ptr) + 16;
  231. return avc->category + 16;
  232. }
  233. static const char *get_level_str(int level)
  234. {
  235. switch (level) {
  236. case AV_LOG_QUIET:
  237. return "quiet";
  238. case AV_LOG_DEBUG:
  239. return "debug";
  240. case AV_LOG_TRACE:
  241. return "trace";
  242. case AV_LOG_VERBOSE:
  243. return "verbose";
  244. case AV_LOG_INFO:
  245. return "info";
  246. case AV_LOG_WARNING:
  247. return "warning";
  248. case AV_LOG_ERROR:
  249. return "error";
  250. case AV_LOG_FATAL:
  251. return "fatal";
  252. case AV_LOG_PANIC:
  253. return "panic";
  254. default:
  255. return "";
  256. }
  257. }
  258. static void format_line(void *avcl, int level, const char *fmt, va_list vl,
  259. AVBPrint part[4], int *print_prefix, int type[2])
  260. {
  261. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  262. av_bprint_init(part+0, 0, AV_BPRINT_SIZE_AUTOMATIC);
  263. av_bprint_init(part+1, 0, AV_BPRINT_SIZE_AUTOMATIC);
  264. av_bprint_init(part+2, 0, AV_BPRINT_SIZE_AUTOMATIC);
  265. av_bprint_init(part+3, 0, 65536);
  266. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  267. if (*print_prefix && avc) {
  268. if (avc->parent_log_context_offset) {
  269. AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
  270. avc->parent_log_context_offset);
  271. if (parent && *parent) {
  272. av_bprintf(part+0, "[%s @ %p] ",
  273. (*parent)->item_name(parent), parent);
  274. if(type) type[0] = get_category(parent);
  275. }
  276. }
  277. av_bprintf(part+1, "[%s @ %p] ",
  278. avc->item_name(avcl), avcl);
  279. if(type) type[1] = get_category(avcl);
  280. }
  281. if (*print_prefix && (level > AV_LOG_QUIET) && (flags & AV_LOG_PRINT_LEVEL))
  282. av_bprintf(part+2, "[%s] ", get_level_str(level));
  283. av_vbprintf(part+3, fmt, vl);
  284. if(*part[0].str || *part[1].str || *part[2].str || *part[3].str) {
  285. char lastc = part[3].len && part[3].len <= part[3].size ? part[3].str[part[3].len - 1] : 0;
  286. *print_prefix = lastc == '\n' || lastc == '\r';
  287. }
  288. }
  289. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  290. char *line, int line_size, int *print_prefix)
  291. {
  292. av_log_format_line2(ptr, level, fmt, vl, line, line_size, print_prefix);
  293. }
  294. int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
  295. char *line, int line_size, int *print_prefix)
  296. {
  297. AVBPrint part[4];
  298. int ret;
  299. format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
  300. ret = snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
  301. av_bprint_finalize(part+3, NULL);
  302. return ret;
  303. }
  304. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  305. {
  306. static int print_prefix = 1;
  307. static int count;
  308. static char prev[LINE_SZ];
  309. AVBPrint part[4];
  310. char line[LINE_SZ];
  311. static int is_atty;
  312. int type[2];
  313. unsigned tint = 0;
  314. if (level >= 0) {
  315. tint = level & 0xff00;
  316. level &= 0xff;
  317. }
  318. if (level > av_log_level)
  319. return;
  320. ff_mutex_lock(&mutex);
  321. format_line(ptr, level, fmt, vl, part, &print_prefix, type);
  322. snprintf(line, sizeof(line), "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
  323. #if HAVE_ISATTY
  324. if (!is_atty)
  325. is_atty = isatty(2) ? 1 : -1;
  326. #endif
  327. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
  328. *line && line[strlen(line) - 1] != '\r'){
  329. count++;
  330. if (is_atty == 1)
  331. fprintf(stderr, " Last message repeated %d times\r", count);
  332. goto end;
  333. }
  334. if (count > 0) {
  335. fprintf(stderr, " Last message repeated %d times\n", count);
  336. count = 0;
  337. }
  338. strcpy(prev, line);
  339. sanitize(part[0].str);
  340. colored_fputs(type[0], 0, part[0].str);
  341. sanitize(part[1].str);
  342. colored_fputs(type[1], 0, part[1].str);
  343. sanitize(part[2].str);
  344. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[2].str);
  345. sanitize(part[3].str);
  346. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[3].str);
  347. #if CONFIG_VALGRIND_BACKTRACE
  348. if (level <= BACKTRACE_LOGLEVEL)
  349. VALGRIND_PRINTF_BACKTRACE("%s", "");
  350. #endif
  351. end:
  352. av_bprint_finalize(part+3, NULL);
  353. ff_mutex_unlock(&mutex);
  354. }
  355. static void (*av_log_callback)(void*, int, const char*, va_list) =
  356. av_log_default_callback;
  357. void av_log(void* avcl, int level, const char *fmt, ...)
  358. {
  359. va_list vl;
  360. va_start(vl, fmt);
  361. av_vlog(avcl, level, fmt, vl);
  362. va_end(vl);
  363. }
  364. void av_log_once(void* avcl, int initial_level, int subsequent_level, int *state, const char *fmt, ...)
  365. {
  366. va_list vl;
  367. va_start(vl, fmt);
  368. av_vlog(avcl, *state ? subsequent_level : initial_level, fmt, vl);
  369. va_end(vl);
  370. *state = 1;
  371. }
  372. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  373. {
  374. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  375. void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
  376. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  377. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  378. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  379. if (log_callback)
  380. log_callback(avcl, level, fmt, vl);
  381. }
  382. int av_log_get_level(void)
  383. {
  384. return av_log_level;
  385. }
  386. void av_log_set_level(int level)
  387. {
  388. av_log_level = level;
  389. }
  390. void av_log_set_flags(int arg)
  391. {
  392. flags = arg;
  393. }
  394. int av_log_get_flags(void)
  395. {
  396. return flags;
  397. }
  398. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  399. {
  400. av_log_callback = callback;
  401. }
  402. static void missing_feature_sample(int sample, void *avc, const char *msg,
  403. va_list argument_list)
  404. {
  405. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  406. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
  407. "version to the newest one from Git. If the problem still "
  408. "occurs, it means that your file has a feature which has not "
  409. "been implemented.\n");
  410. if (sample)
  411. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  412. "of this file to https://streams.videolan.org/upload/ "
  413. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  414. }
  415. void avpriv_request_sample(void *avc, const char *msg, ...)
  416. {
  417. va_list argument_list;
  418. va_start(argument_list, msg);
  419. missing_feature_sample(1, avc, msg, argument_list);
  420. va_end(argument_list);
  421. }
  422. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  423. {
  424. va_list argument_list;
  425. va_start(argument_list, msg);
  426. missing_feature_sample(0, avc, msg, argument_list);
  427. va_end(argument_list);
  428. }