log.c 4.9 KB

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