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.

456 lines
14KB

  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. AVBPrint part[4];
  258. format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
  259. snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
  260. av_bprint_finalize(part+3, NULL);
  261. }
  262. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  263. {
  264. static int print_prefix = 1;
  265. static int count;
  266. static char prev[LINE_SZ];
  267. AVBPrint part[4];
  268. char line[LINE_SZ];
  269. static int is_atty;
  270. int type[2];
  271. unsigned tint = 0;
  272. if (level >= 0) {
  273. tint = level & 0xff00;
  274. level &= 0xff;
  275. }
  276. if (level > av_log_level)
  277. return;
  278. #if HAVE_PTHREADS
  279. pthread_mutex_lock(&mutex);
  280. #endif
  281. format_line(ptr, level, fmt, vl, part, &print_prefix, type);
  282. snprintf(line, sizeof(line), "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
  283. #if HAVE_ISATTY
  284. if (!is_atty)
  285. is_atty = isatty(2) ? 1 : -1;
  286. #endif
  287. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
  288. *line && line[strlen(line) - 1] != '\r'){
  289. count++;
  290. if (is_atty == 1)
  291. fprintf(stderr, " Last message repeated %d times\r", count);
  292. goto end;
  293. }
  294. if (count > 0) {
  295. fprintf(stderr, " Last message repeated %d times\n", count);
  296. count = 0;
  297. }
  298. strcpy(prev, line);
  299. sanitize(part[0].str);
  300. colored_fputs(type[0], 0, part[0].str);
  301. sanitize(part[1].str);
  302. colored_fputs(type[1], 0, part[1].str);
  303. sanitize(part[2].str);
  304. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[2].str);
  305. sanitize(part[3].str);
  306. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, part[3].str);
  307. #if CONFIG_VALGRIND_BACKTRACE
  308. if (level <= BACKTRACE_LOGLEVEL)
  309. VALGRIND_PRINTF_BACKTRACE("%s", "");
  310. #endif
  311. end:
  312. av_bprint_finalize(part+3, NULL);
  313. #if HAVE_PTHREADS
  314. pthread_mutex_unlock(&mutex);
  315. #endif
  316. }
  317. static void (*av_log_callback)(void*, int, const char*, va_list) =
  318. av_log_default_callback;
  319. void av_log(void* avcl, int level, const char *fmt, ...)
  320. {
  321. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  322. va_list vl;
  323. va_start(vl, fmt);
  324. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  325. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  326. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  327. av_vlog(avcl, level, fmt, vl);
  328. va_end(vl);
  329. }
  330. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  331. {
  332. void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
  333. if (log_callback)
  334. log_callback(avcl, level, fmt, vl);
  335. }
  336. int av_log_get_level(void)
  337. {
  338. return av_log_level;
  339. }
  340. void av_log_set_level(int level)
  341. {
  342. av_log_level = level;
  343. }
  344. void av_log_set_flags(int arg)
  345. {
  346. flags = arg;
  347. }
  348. int av_log_get_flags(void)
  349. {
  350. return flags;
  351. }
  352. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  353. {
  354. av_log_callback = callback;
  355. }
  356. static void missing_feature_sample(int sample, void *avc, const char *msg,
  357. va_list argument_list)
  358. {
  359. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  360. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
  361. "version to the newest one from Git. If the problem still "
  362. "occurs, it means that your file has a feature which has not "
  363. "been implemented.\n");
  364. if (sample)
  365. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  366. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  367. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  368. }
  369. void avpriv_request_sample(void *avc, const char *msg, ...)
  370. {
  371. va_list argument_list;
  372. va_start(argument_list, msg);
  373. missing_feature_sample(1, avc, msg, argument_list);
  374. va_end(argument_list);
  375. }
  376. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  377. {
  378. va_list argument_list;
  379. va_start(argument_list, msg);
  380. missing_feature_sample(0, avc, msg, argument_list);
  381. va_end(argument_list);
  382. }
  383. #ifdef TEST
  384. // LCOV_EXCL_START
  385. #include <string.h>
  386. int main(int argc, char **argv)
  387. {
  388. int i;
  389. av_log_set_level(AV_LOG_DEBUG);
  390. for (use_color=0; use_color<=256; use_color = 255*use_color+1) {
  391. av_log(NULL, AV_LOG_FATAL, "use_color: %d\n", use_color);
  392. for (i = AV_LOG_DEBUG; i>=AV_LOG_QUIET; i-=8) {
  393. av_log(NULL, i, " %d", i);
  394. av_log(NULL, AV_LOG_INFO, "e ");
  395. av_log(NULL, i + 256*123, "C%d", i);
  396. av_log(NULL, AV_LOG_INFO, "e");
  397. }
  398. av_log(NULL, AV_LOG_PANIC, "\n");
  399. }
  400. return 0;
  401. }
  402. // LCOV_EXCL_STOP
  403. #endif