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.

176 lines
5.0KB

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