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.

240 lines
6.7KB

  1. /*
  2. * log functions
  3. * Copyright (c) 2003 Michel Bardiaux
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "avstring.h"
  35. #include "avutil.h"
  36. #include "common.h"
  37. #include "internal.h"
  38. #include "log.h"
  39. static int av_log_level = AV_LOG_INFO;
  40. static int flags;
  41. #define NB_LEVELS 8
  42. #if HAVE_SETCONSOLETEXTATTRIBUTE
  43. #include <windows.h>
  44. static const uint8_t color[NB_LEVELS] = { 12, 12, 12, 14, 7, 10, 11, 8};
  45. static int16_t background, attr_orig;
  46. static HANDLE con;
  47. #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
  48. #define reset_color() SetConsoleTextAttribute(con, attr_orig)
  49. #define print_256color(x)
  50. #else
  51. static const uint8_t color[NB_LEVELS] = {
  52. 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06, 0x07
  53. };
  54. #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
  55. #define print_256color(x) fprintf(stderr, "\033[38;5;%dm", x)
  56. #define reset_color() fprintf(stderr, "\033[0m")
  57. #endif
  58. static int use_color = -1;
  59. static void check_color_terminal(void)
  60. {
  61. #if HAVE_SETCONSOLETEXTATTRIBUTE
  62. CONSOLE_SCREEN_BUFFER_INFO con_info;
  63. con = GetStdHandle(STD_ERROR_HANDLE);
  64. use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
  65. !getenv("AV_LOG_FORCE_NOCOLOR");
  66. if (use_color) {
  67. GetConsoleScreenBufferInfo(con, &con_info);
  68. attr_orig = con_info.wAttributes;
  69. background = attr_orig & 0xF0;
  70. }
  71. #elif HAVE_ISATTY
  72. char *term = getenv("TERM");
  73. use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
  74. (getenv("TERM") && isatty(2) || getenv("AV_LOG_FORCE_COLOR"));
  75. if (use_color)
  76. use_color += term && strstr(term, "256color");
  77. #else
  78. use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
  79. !getenv("AV_LOG_FORCE_NOCOLOR");
  80. #endif
  81. }
  82. static void colored_fputs(int level, int tint, const char *str)
  83. {
  84. if (use_color < 0)
  85. check_color_terminal();
  86. switch (use_color) {
  87. case 1:
  88. set_color(level);
  89. break;
  90. case 2:
  91. set_color(level);
  92. if (tint)
  93. print_256color(tint);
  94. break;
  95. default:
  96. break;
  97. }
  98. fputs(str, stderr);
  99. if (use_color) {
  100. reset_color();
  101. }
  102. }
  103. const char *av_default_item_name(void *ptr)
  104. {
  105. return (*(AVClass **) ptr)->class_name;
  106. }
  107. void av_log_default_callback(void *avcl, int level, const char *fmt, va_list vl)
  108. {
  109. static int print_prefix = 1;
  110. static int count;
  111. static char prev[1024];
  112. char line[1024];
  113. static int is_atty;
  114. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  115. unsigned tint = level & 0xff00;
  116. level &= 0xff;
  117. if (level > av_log_level)
  118. return;
  119. line[0] = 0;
  120. if (print_prefix && avc) {
  121. if (avc->parent_log_context_offset) {
  122. AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
  123. avc->parent_log_context_offset);
  124. if (parent && *parent) {
  125. snprintf(line, sizeof(line), "[%s @ %p] ",
  126. (*parent)->item_name(parent), parent);
  127. }
  128. }
  129. snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
  130. avc->item_name(avcl), avcl);
  131. }
  132. vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
  133. print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
  134. #if HAVE_ISATTY
  135. if (!is_atty)
  136. is_atty = isatty(2) ? 1 : -1;
  137. #endif
  138. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
  139. !strncmp(line, prev, sizeof line)) {
  140. count++;
  141. if (is_atty == 1)
  142. fprintf(stderr, " Last message repeated %d times\r", count);
  143. return;
  144. }
  145. if (count > 0) {
  146. fprintf(stderr, " Last message repeated %d times\n", count);
  147. count = 0;
  148. }
  149. colored_fputs(av_clip(level >> 3, 0, NB_LEVELS - 1), tint >> 8, line);
  150. av_strlcpy(prev, line, sizeof line);
  151. }
  152. static void (*av_log_callback)(void*, int, const char*, va_list) =
  153. av_log_default_callback;
  154. void av_log(void* avcl, int level, const char *fmt, ...)
  155. {
  156. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  157. va_list vl;
  158. va_start(vl, fmt);
  159. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  160. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  161. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  162. av_vlog(avcl, level, fmt, vl);
  163. va_end(vl);
  164. }
  165. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  166. {
  167. av_log_callback(avcl, level, fmt, vl);
  168. }
  169. int av_log_get_level(void)
  170. {
  171. return av_log_level;
  172. }
  173. void av_log_set_level(int level)
  174. {
  175. av_log_level = level;
  176. }
  177. void av_log_set_flags(int arg)
  178. {
  179. flags = arg;
  180. }
  181. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  182. {
  183. av_log_callback = callback;
  184. }
  185. static void missing_feature_sample(int sample, void *avc, const char *msg,
  186. va_list argument_list)
  187. {
  188. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  189. av_log(avc, AV_LOG_WARNING, " is not implemented. Update your Libav "
  190. "version to the newest one from Git. If the problem still "
  191. "occurs, it means that your file has a feature which has not "
  192. "been implemented.\n");
  193. if (sample)
  194. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  195. "of this file to ftp://upload.libav.org/incoming/ "
  196. "and contact the libav-devel mailing list.\n");
  197. }
  198. void avpriv_request_sample(void *avc, const char *msg, ...)
  199. {
  200. va_list argument_list;
  201. va_start(argument_list, msg);
  202. missing_feature_sample(1, avc, msg, argument_list);
  203. va_end(argument_list);
  204. }
  205. void avpriv_report_missing_feature(void *avc, const char *msg, ...)
  206. {
  207. va_list argument_list;
  208. va_start(argument_list, msg);
  209. missing_feature_sample(0, avc, msg, argument_list);
  210. va_end(argument_list);
  211. }