log.c 4.2 KB

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