log.c 11 KB

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