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.

339 lines
10KB

  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. };
  68. static int16_t background, attr_orig;
  69. static HANDLE con;
  70. #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
  71. #define set_256color set_color
  72. #define reset_color() SetConsoleTextAttribute(con, attr_orig)
  73. #else
  74. static const uint32_t color[16 + AV_CLASS_CATEGORY_NB] = {
  75. [AV_LOG_PANIC /8] = 52 << 16 | 196 << 8 | 0x41,
  76. [AV_LOG_FATAL /8] = 208 << 8 | 0x41,
  77. [AV_LOG_ERROR /8] = 196 << 8 | 0x11,
  78. [AV_LOG_WARNING/8] = 226 << 8 | 0x03,
  79. [AV_LOG_INFO /8] = 253 << 8 | 0x09,
  80. [AV_LOG_VERBOSE/8] = 40 << 8 | 0x02,
  81. [AV_LOG_DEBUG /8] = 34 << 8 | 0x02,
  82. [16+AV_CLASS_CATEGORY_NA ] = 250 << 8 | 0x09,
  83. [16+AV_CLASS_CATEGORY_INPUT ] = 219 << 8 | 0x15,
  84. [16+AV_CLASS_CATEGORY_OUTPUT ] = 201 << 8 | 0x05,
  85. [16+AV_CLASS_CATEGORY_MUXER ] = 213 << 8 | 0x15,
  86. [16+AV_CLASS_CATEGORY_DEMUXER ] = 207 << 8 | 0x05,
  87. [16+AV_CLASS_CATEGORY_ENCODER ] = 51 << 8 | 0x16,
  88. [16+AV_CLASS_CATEGORY_DECODER ] = 39 << 8 | 0x06,
  89. [16+AV_CLASS_CATEGORY_FILTER ] = 155 << 8 | 0x12,
  90. [16+AV_CLASS_CATEGORY_BITSTREAM_FILTER] = 192 << 8 | 0x14,
  91. [16+AV_CLASS_CATEGORY_SWSCALER ] = 153 << 8 | 0x14,
  92. [16+AV_CLASS_CATEGORY_SWRESAMPLER ] = 147 << 8 | 0x14,
  93. };
  94. #define set_color(x) fprintf(stderr, "\033[%d;3%dm", (color[x] >> 4) & 15, color[x] & 15)
  95. #define set_256color(x) fprintf(stderr, "\033[48;5;%dm\033[38;5;%dm", (color[x] >> 16) & 0xff, (color[x] >> 8) & 0xff)
  96. #define reset_color() fprintf(stderr, "\033[0m")
  97. #endif
  98. static int use_color = -1;
  99. static void colored_fputs(int level, const char *str)
  100. {
  101. if (use_color < 0) {
  102. #if HAVE_SETCONSOLETEXTATTRIBUTE
  103. CONSOLE_SCREEN_BUFFER_INFO con_info;
  104. con = GetStdHandle(STD_ERROR_HANDLE);
  105. use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
  106. !getenv("AV_LOG_FORCE_NOCOLOR");
  107. if (use_color) {
  108. GetConsoleScreenBufferInfo(con, &con_info);
  109. attr_orig = con_info.wAttributes;
  110. background = attr_orig & 0xF0;
  111. }
  112. #elif HAVE_ISATTY
  113. use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
  114. (getenv("TERM") && isatty(2) ||
  115. getenv("AV_LOG_FORCE_COLOR"));
  116. if (getenv("AV_LOG_FORCE_256COLOR"))
  117. use_color *= 256;
  118. #else
  119. use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
  120. !getenv("AV_LOG_FORCE_NOCOLOR");
  121. #endif
  122. }
  123. if (use_color == 1) {
  124. set_color(level);
  125. } else if (use_color == 256)
  126. set_256color(level);
  127. fputs(str, stderr);
  128. if (use_color) {
  129. reset_color();
  130. }
  131. }
  132. const char *av_default_item_name(void *ptr)
  133. {
  134. return (*(AVClass **) ptr)->class_name;
  135. }
  136. AVClassCategory av_default_get_category(void *ptr)
  137. {
  138. return (*(AVClass **) ptr)->category;
  139. }
  140. static void sanitize(uint8_t *line){
  141. while(*line){
  142. if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
  143. *line='?';
  144. line++;
  145. }
  146. }
  147. static int get_category(void *ptr){
  148. AVClass *avc = *(AVClass **) ptr;
  149. if( !avc
  150. || (avc->version&0xFF)<100
  151. || avc->version < (51 << 16 | 59 << 8)
  152. || avc->category >= AV_CLASS_CATEGORY_NB) return AV_CLASS_CATEGORY_NA + 16;
  153. if(avc->get_category)
  154. return avc->get_category(ptr) + 16;
  155. return avc->category + 16;
  156. }
  157. static void format_line(void *ptr, int level, const char *fmt, va_list vl,
  158. AVBPrint part[3], int *print_prefix, int type[2])
  159. {
  160. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  161. av_bprint_init(part+0, 0, 1);
  162. av_bprint_init(part+1, 0, 1);
  163. av_bprint_init(part+2, 0, 65536);
  164. if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
  165. if (*print_prefix && avc) {
  166. if (avc->parent_log_context_offset) {
  167. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  168. avc->parent_log_context_offset);
  169. if (parent && *parent) {
  170. av_bprintf(part+0, "[%s @ %p] ",
  171. (*parent)->item_name(parent), parent);
  172. if(type) type[0] = get_category(parent);
  173. }
  174. }
  175. av_bprintf(part+1, "[%s @ %p] ",
  176. avc->item_name(ptr), ptr);
  177. if(type) type[1] = get_category(ptr);
  178. }
  179. av_vbprintf(part+2, fmt, vl);
  180. if(*part[0].str || *part[1].str || *part[2].str) {
  181. char lastc = part[2].len ? part[2].str[part[2].len - 1] : 0;
  182. *print_prefix = lastc == '\n' || lastc == '\r';
  183. }
  184. }
  185. void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
  186. char *line, int line_size, int *print_prefix)
  187. {
  188. AVBPrint part[3];
  189. format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
  190. snprintf(line, line_size, "%s%s%s", part[0].str, part[1].str, part[2].str);
  191. av_bprint_finalize(part+2, NULL);
  192. }
  193. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  194. {
  195. static int print_prefix = 1;
  196. static int count;
  197. static char prev[LINE_SZ];
  198. AVBPrint part[3];
  199. char line[LINE_SZ];
  200. static int is_atty;
  201. int type[2];
  202. if (level > av_log_level)
  203. return;
  204. #if HAVE_PTHREADS
  205. pthread_mutex_lock(&mutex);
  206. #endif
  207. format_line(ptr, level, fmt, vl, part, &print_prefix, type);
  208. snprintf(line, sizeof(line), "%s%s%s", part[0].str, part[1].str, part[2].str);
  209. #if HAVE_ISATTY
  210. if (!is_atty)
  211. is_atty = isatty(2) ? 1 : -1;
  212. #endif
  213. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev) &&
  214. *line && line[strlen(line) - 1] != '\r'){
  215. count++;
  216. if (is_atty == 1)
  217. fprintf(stderr, " Last message repeated %d times\r", count);
  218. goto end;
  219. }
  220. if (count > 0) {
  221. fprintf(stderr, " Last message repeated %d times\n", count);
  222. count = 0;
  223. }
  224. strcpy(prev, line);
  225. sanitize(part[0].str);
  226. colored_fputs(type[0], part[0].str);
  227. sanitize(part[1].str);
  228. colored_fputs(type[1], part[1].str);
  229. sanitize(part[2].str);
  230. colored_fputs(av_clip(level >> 3, 0, 6), part[2].str);
  231. end:
  232. av_bprint_finalize(part+2, NULL);
  233. #if HAVE_PTHREADS
  234. pthread_mutex_unlock(&mutex);
  235. #endif
  236. }
  237. static void (*av_log_callback)(void*, int, const char*, va_list) =
  238. av_log_default_callback;
  239. void av_log(void* avcl, int level, const char *fmt, ...)
  240. {
  241. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  242. va_list vl;
  243. va_start(vl, fmt);
  244. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  245. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  246. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  247. av_vlog(avcl, level, fmt, vl);
  248. va_end(vl);
  249. }
  250. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  251. {
  252. void (*log_callback)(void*, int, const char*, va_list) = av_log_callback;
  253. if (log_callback)
  254. log_callback(avcl, level, fmt, vl);
  255. }
  256. int av_log_get_level(void)
  257. {
  258. return av_log_level;
  259. }
  260. void av_log_set_level(int level)
  261. {
  262. av_log_level = level;
  263. }
  264. void av_log_set_flags(int arg)
  265. {
  266. flags = arg;
  267. }
  268. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  269. {
  270. av_log_callback = callback;
  271. }
  272. static void missing_feature_sample(int sample, void *avc, const char *msg,
  273. va_list argument_list)
  274. {
  275. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  276. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
  277. "version to the newest one from Git. If the problem still "
  278. "occurs, it means that your file has a feature which has not "
  279. "been implemented.\n");
  280. if (sample)
  281. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  282. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  283. "and contact the ffmpeg-devel mailing list.\n");
  284. }
  285. void avpriv_request_sample(void *avc, const char *msg, ...)
  286. {
  287. va_list argument_list;
  288. va_start(argument_list, msg);
  289. missing_feature_sample(1, avc, msg, argument_list);
  290. va_end(argument_list);
  291. }
  292. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  293. {
  294. va_list argument_list;
  295. va_start(argument_list, msg);
  296. missing_feature_sample(0, avc, msg, argument_list);
  297. va_end(argument_list);
  298. }