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.

180 lines
5.1KB

  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. #include <stdlib.h>
  30. #include "avstring.h"
  31. #include "avutil.h"
  32. #include "log.h"
  33. static int av_log_level = AV_LOG_INFO;
  34. static int flags;
  35. #if defined(_WIN32) && !defined(__MINGW32CE__)
  36. #include <windows.h>
  37. static const uint8_t color[] = { 12, 12, 12, 14, 7, 10, 11 };
  38. static int16_t background, attr_orig;
  39. static HANDLE con;
  40. #define set_color(x) SetConsoleTextAttribute(con, background | color[x])
  41. #define reset_color() SetConsoleTextAttribute(con, attr_orig)
  42. #else
  43. static const uint8_t color[] = { 0x41, 0x41, 0x11, 0x03, 9, 0x02, 0x06 };
  44. #define set_color(x) fprintf(stderr, "\033[%d;3%dm", color[x] >> 4, color[x]&15)
  45. #define reset_color() fprintf(stderr, "\033[0m")
  46. #endif
  47. static int use_color = -1;
  48. #undef fprintf
  49. static void colored_fputs(int level, const char *str)
  50. {
  51. if (use_color < 0) {
  52. #if defined(_WIN32) && !defined(__MINGW32CE__)
  53. CONSOLE_SCREEN_BUFFER_INFO con_info;
  54. con = GetStdHandle(STD_ERROR_HANDLE);
  55. use_color = (con != INVALID_HANDLE_VALUE) && !getenv("NO_COLOR") &&
  56. !getenv("AV_LOG_FORCE_NOCOLOR");
  57. if (use_color) {
  58. GetConsoleScreenBufferInfo(con, &con_info);
  59. attr_orig = con_info.wAttributes;
  60. background = attr_orig & 0xF0;
  61. }
  62. #elif HAVE_ISATTY
  63. use_color = !getenv("NO_COLOR") && !getenv("AV_LOG_FORCE_NOCOLOR") &&
  64. (getenv("TERM") && isatty(2) ||
  65. getenv("AV_LOG_FORCE_COLOR"));
  66. #else
  67. use_color = getenv("AV_LOG_FORCE_COLOR") && !getenv("NO_COLOR") &&
  68. !getenv("AV_LOG_FORCE_NOCOLOR");
  69. #endif
  70. }
  71. if (use_color) {
  72. set_color(level);
  73. }
  74. fputs(str, stderr);
  75. if (use_color) {
  76. reset_color();
  77. }
  78. }
  79. const char *av_default_item_name(void *ptr)
  80. {
  81. return (*(AVClass **) ptr)->class_name;
  82. }
  83. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  84. {
  85. static int print_prefix = 1;
  86. static int count;
  87. static char prev[1024];
  88. char line[1024];
  89. static int is_atty;
  90. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  91. if (level > av_log_level)
  92. return;
  93. line[0] = 0;
  94. #undef fprintf
  95. if (print_prefix && avc) {
  96. if (avc->parent_log_context_offset) {
  97. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  98. avc->parent_log_context_offset);
  99. if (parent && *parent) {
  100. snprintf(line, sizeof(line), "[%s @ %p] ",
  101. (*parent)->item_name(parent), parent);
  102. }
  103. }
  104. snprintf(line + strlen(line), sizeof(line) - strlen(line), "[%s @ %p] ",
  105. avc->item_name(ptr), ptr);
  106. }
  107. vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
  108. print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
  109. #if HAVE_ISATTY
  110. if (!is_atty)
  111. is_atty = isatty(2) ? 1 : -1;
  112. #endif
  113. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) &&
  114. !strncmp(line, prev, sizeof line)) {
  115. count++;
  116. if (is_atty == 1)
  117. fprintf(stderr, " Last message repeated %d times\r", count);
  118. return;
  119. }
  120. if (count > 0) {
  121. fprintf(stderr, " Last message repeated %d times\n", count);
  122. count = 0;
  123. }
  124. colored_fputs(av_clip(level >> 3, 0, 6), line);
  125. av_strlcpy(prev, line, sizeof line);
  126. }
  127. static void (*av_log_callback)(void*, int, const char*, va_list) =
  128. av_log_default_callback;
  129. void av_log(void* avcl, int level, const char *fmt, ...)
  130. {
  131. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  132. va_list vl;
  133. va_start(vl, fmt);
  134. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  135. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  136. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  137. av_vlog(avcl, level, fmt, vl);
  138. va_end(vl);
  139. }
  140. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  141. {
  142. av_log_callback(avcl, level, fmt, vl);
  143. }
  144. int av_log_get_level(void)
  145. {
  146. return av_log_level;
  147. }
  148. void av_log_set_level(int level)
  149. {
  150. av_log_level = level;
  151. }
  152. void av_log_set_flags(int arg)
  153. {
  154. flags = arg;
  155. }
  156. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  157. {
  158. av_log_callback = callback;
  159. }