log.c 16 KB

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