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.

445 lines
13KB

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