You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.7KB

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