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.

378 lines
12KB

  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 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 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. || 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. if (!*str)
  132. return;
  133. if (use_color < 0)
  134. check_color_terminal();
  135. #if HAVE_SETCONSOLETEXTATTRIBUTE
  136. if (use_color && level != AV_LOG_INFO/8)
  137. SetConsoleTextAttribute(con, background | color[level]);
  138. fputs(str, stderr);
  139. if (use_color && level != AV_LOG_INFO/8)
  140. SetConsoleTextAttribute(con, attr_orig);
  141. #else
  142. if (use_color == 1 && level != AV_LOG_INFO/8) {
  143. fprintf(stderr,
  144. "\033[%d;3%dm%s\033[0m",
  145. (color[level] >> 4) & 15,
  146. color[level] & 15,
  147. str);
  148. } else if (tint && use_color == 256) {
  149. fprintf(stderr,
  150. "\033[48;5;%dm\033[38;5;%dm%s\033[0m",
  151. (color[level] >> 16) & 0xff,
  152. tint,
  153. str);
  154. } else if (use_color == 256 && level != AV_LOG_INFO/8) {
  155. fprintf(stderr,
  156. "\033[48;5;%dm\033[38;5;%dm%s\033[0m",
  157. (color[level] >> 16) & 0xff,
  158. (color[level] >> 8) & 0xff,
  159. str);
  160. } else
  161. fputs(str, stderr);
  162. #endif
  163. }
  164. const char *av_default_item_name(void *ptr)
  165. {
  166. return (*(AVClass **) ptr)->class_name;
  167. }
  168. AVClassCategory av_default_get_category(void *ptr)
  169. {
  170. return (*(AVClass **) ptr)->category;
  171. }
  172. static void sanitize(uint8_t *line){
  173. while(*line){
  174. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  175. *line='?';
  176. line++;
  177. }
  178. }
  179. static int get_category(void *ptr){
  180. AVClass *avc = *(AVClass **) ptr;
  181. if( !avc
  182. || (avc->version&0xFF)<100
  183. || avc->version < (51 << 16 | 59 << 8)
  184. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  185. if(avc->get_category)
  186. return avc->get_category(ptr) + 16;
  187. return avc->category + 16;
  188. }
  189. static void format_line(void *avcl, int level, const char *fmt, va_list vl,
  190. AVBPrint part[3], int *print_prefix, int type[2])
  191. {
  192. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  193. av_bprint_init(part+0, 0, 1);
  194. av_bprint_init(part+1, 0, 1);
  195. av_bprint_init(part+2, 0, 65536);
  196. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  197. if (*print_prefix && avc) {
  198. if (avc->parent_log_context_offset) {
  199. AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
  200. avc->parent_log_context_offset);
  201. if (parent && *parent) {
  202. av_bprintf(part+0, "[%s @ %p] ",
  203. (*parent)->item_name(parent), parent);
  204. if(type) type[0] = get_category(parent);
  205. }
  206. }
  207. av_bprintf(part+1, "[%s @ %p] ",
  208. avc->item_name(avcl), avcl);
  209. if(type) type[1] = get_category(avcl);
  210. }
  211. av_vbprintf(part+2, fmt, vl);
  212. if(*part[0].str || *part[1].str || *part[2].str) {
  213. char lastc = part[2].len && part[2].len <= part[2].size ? part[2].str[part[2].len - 1] : 0;
  214. *print_prefix = lastc == '\n' || lastc == '\r';
  215. }
  216. }
  217. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  218. char *line, int line_size, int *print_prefix)
  219. {
  220. AVBPrint part[3];
  221. format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
  222. snprintf(line, line_size, "%s%s%s", part[0].str, part[1].str, part[2].str);
  223. av_bprint_finalize(part+2, NULL);
  224. }
  225. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  226. {
  227. static int print_prefix = 1;
  228. static int count;
  229. static char prev[LINE_SZ];
  230. AVBPrint part[3];
  231. char line[LINE_SZ];
  232. static int is_atty;
  233. int type[2];
  234. int tint = av_clip(level >> 8, 0, 256);
  235. level &= 0xff;
  236. if (level > av_log_level)
  237. return;
  238. #if HAVE_PTHREADS
  239. pthread_mutex_lock(&mutex);
  240. #endif
  241. format_line(ptr, level, fmt, vl, part, &print_prefix, type);
  242. snprintf(line, sizeof(line), "%s%s%s", part[0].str, part[1].str, part[2].str);
  243. #if HAVE_ISATTY
  244. if (!is_atty)
  245. is_atty = isatty(2) ? 1 : -1;
  246. #endif
  247. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
  248. *line && line[strlen(line) - 1] != '\r'){
  249. count++;
  250. if (is_atty == 1)
  251. fprintf(stderr, " Last message repeated %d times\r", count);
  252. goto end;
  253. }
  254. if (count > 0) {
  255. fprintf(stderr, " Last message repeated %d times\n", count);
  256. count = 0;
  257. }
  258. strcpy(prev, line);
  259. sanitize(part[0].str);
  260. colored_fputs(type[0], 0, part[0].str);
  261. sanitize(part[1].str);
  262. colored_fputs(type[1], 0, part[1].str);
  263. sanitize(part[2].str);
  264. colored_fputs(av_clip(level >> 3, 0, 6), tint, part[2].str);
  265. end:
  266. av_bprint_finalize(part+2, NULL);
  267. #if HAVE_PTHREADS
  268. pthread_mutex_unlock(&mutex);
  269. #endif
  270. }
  271. static void (*av_log_callback)(void*, int, const char*, va_list) =
  272. av_log_default_callback;
  273. void av_log(void* avcl, int level, const char *fmt, ...)
  274. {
  275. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  276. va_list vl;
  277. va_start(vl, fmt);
  278. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  279. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  280. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  281. av_vlog(avcl, level, fmt, vl);
  282. va_end(vl);
  283. }
  284. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  285. {
  286. void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
  287. if (log_callback)
  288. log_callback(avcl, level, fmt, vl);
  289. }
  290. int av_log_get_level(void)
  291. {
  292. return av_log_level;
  293. }
  294. void av_log_set_level(int level)
  295. {
  296. av_log_level = level;
  297. }
  298. void av_log_set_flags(int arg)
  299. {
  300. flags = arg;
  301. }
  302. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  303. {
  304. av_log_callback = callback;
  305. }
  306. static void missing_feature_sample(int sample, void *avc, const char *msg,
  307. va_list argument_list)
  308. {
  309. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  310. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
  311. "version to the newest one from Git. If the problem still "
  312. "occurs, it means that your file has a feature which has not "
  313. "been implemented.\n");
  314. if (sample)
  315. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  316. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  317. "and contact the ffmpeg-devel mailing list.\n");
  318. }
  319. void avpriv_request_sample(void *avc, const char *msg, ...)
  320. {
  321. va_list argument_list;
  322. va_start(argument_list, msg);
  323. missing_feature_sample(1, avc, msg, argument_list);
  324. va_end(argument_list);
  325. }
  326. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  327. {
  328. va_list argument_list;
  329. va_start(argument_list, msg);
  330. missing_feature_sample(0, avc, msg, argument_list);
  331. va_end(argument_list);
  332. }