log.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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
  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. static void check_color_terminal(void)
  112. {
  113. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE
  114. CONSOLE_SCREEN_BUFFER_INFO con_info;
  115. con = GetStdHandle(STD_ERROR_HANDLE);
  116. use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
  117. !getenv("AV_LOG_FORCE_NOCOLOR");
  118. if (use_color) {
  119. GetConsoleScreenBufferInfo(con, &con_info);
  120. attr_orig = con_info.wAttributes;
  121. background = attr_orig & 0xF0;
  122. }
  123. #elif HAVE_ISATTY
  124. char *term = getenv("TERM");
  125. use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
  126. (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
  127. if ( getenv("AV_LOG_FORCE_256COLOR")
  128. || (term && strstr(term, "256color")))
  129. use_color *= 256;
  130. #else
  131. use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
  132. !getenv("AV_LOG_FORCE_NOCOLOR");
  133. #endif
  134. }
  135. static void colored_fputs(int level, int tint, const char *str)
  136. {
  137. int local_use_color;
  138. if (!*str)
  139. return;
  140. if (use_color < 0)
  141. check_color_terminal();
  142. if (level == AV_LOG_INFO/8) local_use_color = 0;
  143. else local_use_color = use_color;
  144. #if defined(_WIN32) && HAVE_SETCONSOLETEXTATTRIBUTE
  145. if (local_use_color)
  146. SetConsoleTextAttribute(con, background | color[level]);
  147. fputs(str, stderr);
  148. if (local_use_color)
  149. SetConsoleTextAttribute(con, attr_orig);
  150. #else
  151. if (local_use_color == 1) {
  152. fprintf(stderr,
  153. "\033[%"PRIu32";3%"PRIu32"m%s\033[0m",
  154. (color[level] >> 4) & 15,
  155. color[level] & 15,
  156. str);
  157. } else if (tint && use_color == 256) {
  158. fprintf(stderr,
  159. "\033[48;5;%"PRIu32"m\033[38;5;%dm%s\033[0m",
  160. (color[level] >> 16) & 0xff,
  161. tint,
  162. str);
  163. } else if (local_use_color == 256) {
  164. fprintf(stderr,
  165. "\033[48;5;%"PRIu32"m\033[38;5;%"PRIu32"m%s\033[0m",
  166. (color[level] >> 16) & 0xff,
  167. (color[level] >> 8) & 0xff,
  168. str);
  169. } else
  170. fputs(str, stderr);
  171. #endif
  172. }
  173. const char *av_default_item_name(void *ptr)
  174. {
  175. return (*(AVClass **) ptr)->class_name;
  176. }
  177. AVClassCategory av_default_get_category(void *ptr)
  178. {
  179. return (*(AVClass **) ptr)->category;
  180. }
  181. static void sanitize(uint8_t *line){
  182. while(*line){
  183. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  184. *line='?';
  185. line++;
  186. }
  187. }
  188. static int get_category(void *ptr){
  189. AVClass *avc = *(AVClass **) ptr;
  190. if( !avc
  191. || (avc->version&0xFF)<100
  192. || avc->version < (51 << 16 | 59 << 8)
  193. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  194. if(avc->get_category)
  195. return avc->get_category(ptr) + 16;
  196. return avc->category + 16;
  197. }
  198. static const char *get_level_str(int level)
  199. {
  200. switch (level) {
  201. case AV_LOG_QUIET:
  202. return "quiet";
  203. case AV_LOG_DEBUG:
  204. return "debug";
  205. case AV_LOG_VERBOSE:
  206. return "verbose";
  207. case AV_LOG_INFO:
  208. return "info";
  209. case AV_LOG_WARNING:
  210. return "warning";
  211. case AV_LOG_ERROR:
  212. return "error";
  213. case AV_LOG_FATAL:
  214. return "fatal";
  215. case AV_LOG_PANIC:
  216. return "panic";
  217. default:
  218. return "";
  219. }
  220. }
  221. static void format_line(void *avcl, int level, const char *fmt, va_list vl,
  222. AVBPrint part[4], int *print_prefix, int type[2])
  223. {
  224. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  225. av_bprint_init(part+0, 0, 1);
  226. av_bprint_init(part+1, 0, 1);
  227. av_bprint_init(part+2, 0, 1);
  228. av_bprint_init(part+3, 0, 65536);
  229. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  230. if (*print_prefix && avc) {
  231. if (avc->parent_log_context_offset) {
  232. AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
  233. avc->parent_log_context_offset);
  234. if (parent && *parent) {
  235. av_bprintf(part+0, "[%s @ %p] ",
  236. (*parent)->item_name(parent), parent);
  237. if(type) type[0] = get_category(parent);
  238. }
  239. }
  240. av_bprintf(part+1, "[%s @ %p] ",
  241. avc->item_name(avcl), avcl);
  242. if(type) type[1] = get_category(avcl);
  243. }
  244. if (*print_prefix && (level > AV_LOG_QUIET) && (flags & AV_LOG_PRINT_LEVEL))
  245. av_bprintf(part+2, "[%s] ", get_level_str(level));
  246. av_vbprintf(part+3, fmt, vl);
  247. if(*part[0].str || *part[1].str || *part[2].str || *part[3].str) {
  248. char lastc = part[3].len && part[3].len <= part[3].size ? part[3].str[part[3].len - 1] : 0;
  249. *print_prefix = lastc == '\n' || lastc == '\r';
  250. }
  251. }
  252. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  253. char *line, int line_size, int *print_prefix)
  254. {
  255. av_log_format_line2(ptr, level, fmt, vl, line, line_size, print_prefix);
  256. }
  257. int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
  258. char *line, int line_size, int *print_prefix)
  259. {
  260. AVBPrint part[4];
  261. int ret;
  262. format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
  263. ret = snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
  264. av_bprint_finalize(part+3, NULL);
  265. return ret;
  266. }
  267. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  268. {
  269. static int print_prefix = 1;
  270. static int count;
  271. static char prev[LINE_SZ];
  272. AVBPrint part[4];
  273. char line[LINE_SZ];
  274. static int is_atty;
  275. int type[2];
  276. unsigned tint = 0;
  277. if (level >= 0) {
  278. tint = level & 0xff00;
  279. level &= 0xff;
  280. }
  281. if (level > av_log_level)
  282. return;
  283. ff_mutex_lock(&mutex);
  284. format_line(ptr, level, fmt, vl, part, &print_prefix, type);
  285. snprintf(line, sizeof(line), "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
  286. #if HAVE_ISATTY
  287. if (!is_atty)
  288. is_atty = isatty(2) ? 1 : -1;
  289. #endif
  290. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
  291. *line && line[strlen(line) - 1] != '\r'){
  292. count++;
  293. if (is_atty == 1)
  294. fprintf(stderr, " Last message repeated %d times\r", count);
  295. goto end;
  296. }
  297. if (count > 0) {
  298. fprintf(stderr, " Last message repeated %d times\n", count);
  299. count = 0;
  300. }
  301. strcpy(prev, line);
  302. sanitize(part[0].str);
  303. colored_fputs(type[0], 0, part[0].str);
  304. sanitize(part[1].str);
  305. colored_fputs(type[1], 0, part[1].str);
  306. sanitize(part[2].str);
  307. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[2].str);
  308. sanitize(part[3].str);
  309. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[3].str);
  310. #if CONFIG_VALGRIND_BACKTRACE
  311. if (level <= BACKTRACE_LOGLEVEL)
  312. VALGRIND_PRINTF_BACKTRACE("%s", "");
  313. #endif
  314. end:
  315. av_bprint_finalize(part+3, NULL);
  316. ff_mutex_unlock(&mutex);
  317. }
  318. static void (*av_log_callback)(void*, int, const char*, va_list) =
  319. av_log_default_callback;
  320. void av_log(void* avcl, int level, const char *fmt, ...)
  321. {
  322. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  323. va_list vl;
  324. va_start(vl, fmt);
  325. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  326. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  327. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  328. av_vlog(avcl, level, fmt, vl);
  329. va_end(vl);
  330. }
  331. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  332. {
  333. void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
  334. if (log_callback)
  335. log_callback(avcl, level, fmt, vl);
  336. }
  337. int av_log_get_level(void)
  338. {
  339. return av_log_level;
  340. }
  341. void av_log_set_level(int level)
  342. {
  343. av_log_level = level;
  344. }
  345. void av_log_set_flags(int arg)
  346. {
  347. flags = arg;
  348. }
  349. int av_log_get_flags(void)
  350. {
  351. return flags;
  352. }
  353. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  354. {
  355. av_log_callback = callback;
  356. }
  357. static void missing_feature_sample(int sample, void *avc, const char *msg,
  358. va_list argument_list)
  359. {
  360. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  361. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
  362. "version to the newest one from Git. If the problem still "
  363. "occurs, it means that your file has a feature which has not "
  364. "been implemented.\n");
  365. if (sample)
  366. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  367. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  368. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  369. }
  370. void avpriv_request_sample(void *avc, const char *msg, ...)
  371. {
  372. va_list argument_list;
  373. va_start(argument_list, msg);
  374. missing_feature_sample(1, avc, msg, argument_list);
  375. va_end(argument_list);
  376. }
  377. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  378. {
  379. va_list argument_list;
  380. va_start(argument_list, msg);
  381. missing_feature_sample(0, avc, msg, argument_list);
  382. va_end(argument_list);
  383. }