log.c 4.8 KB

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