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.

237 lines
6.6KB

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