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.

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