log.c 10 KB

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