log.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, 10 };
  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, 2 };
  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_format_line(void *ptr, int level, const char *fmt, va_list vl,
  87. char *line, int line_size, int *print_prefix)
  88. {
  89. AVClass* avc = ptr ? *(AVClass **) ptr : NULL;
  90. line[0] = 0;
  91. if (*print_prefix && avc) {
  92. if (avc->parent_log_context_offset) {
  93. AVClass** parent = *(AVClass ***) (((uint8_t *) ptr) +
  94. avc->parent_log_context_offset);
  95. if (parent && *parent) {
  96. snprintf(line, line_size, "[%s @ %p] ",
  97. (*parent)->item_name(parent), parent);
  98. }
  99. }
  100. snprintf(line + strlen(line), line_size - strlen(line), "[%s @ %p] ",
  101. avc->item_name(ptr), ptr);
  102. }
  103. vsnprintf(line + strlen(line), line_size - strlen(line), fmt, vl);
  104. *print_prefix = strlen(line) && line[strlen(line) - 1] == '\n';
  105. }
  106. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  107. {
  108. static int print_prefix = 1;
  109. static int count;
  110. static char prev[1024];
  111. char line[1024];
  112. static int is_atty;
  113. if (level > av_log_level)
  114. return;
  115. av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
  116. #if HAVE_ISATTY
  117. if (!is_atty)
  118. is_atty = isatty(2) ? 1 : -1;
  119. #endif
  120. #undef fprintf
  121. if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
  122. count++;
  123. if (is_atty == 1)
  124. fprintf(stderr, " Last message repeated %d times\r", count);
  125. return;
  126. }
  127. if (count > 0) {
  128. fprintf(stderr, " Last message repeated %d times\n", count);
  129. count = 0;
  130. }
  131. strcpy(prev, line);
  132. sanitize(line);
  133. colored_fputs(av_clip(level >> 3, 0, 6), line);
  134. }
  135. static void (*av_log_callback)(void*, int, const char*, va_list) =
  136. av_log_default_callback;
  137. void av_log(void* avcl, int level, const char *fmt, ...)
  138. {
  139. AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
  140. va_list vl;
  141. va_start(vl, fmt);
  142. if (avc && avc->version >= (50 << 16 | 15 << 8 | 2) &&
  143. avc->log_level_offset_offset && level >= AV_LOG_FATAL)
  144. level += *(int *) (((uint8_t *) avcl) + avc->log_level_offset_offset);
  145. av_vlog(avcl, level, fmt, vl);
  146. va_end(vl);
  147. }
  148. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  149. {
  150. av_log_callback(avcl, level, fmt, vl);
  151. }
  152. int av_log_get_level(void)
  153. {
  154. return av_log_level;
  155. }
  156. void av_log_set_level(int level)
  157. {
  158. av_log_level = level;
  159. }
  160. void av_log_set_flags(int arg)
  161. {
  162. flags = arg;
  163. }
  164. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  165. {
  166. av_log_callback = callback;
  167. }