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.

183 lines
5.1KB

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