log.c 13 KB

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