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