log.c 5.0 KB

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