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.

124 lines
3.2KB

  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 use_ansi_color=-1;
  34. #undef fprintf
  35. static void colored_fputs(int color, const char *str){
  36. if(use_ansi_color<0){
  37. #if HAVE_ISATTY && !defined(_WIN32)
  38. use_ansi_color= getenv("TERM") && !getenv("NO_COLOR") && isatty(2);
  39. #else
  40. use_ansi_color= 0;
  41. #endif
  42. }
  43. if(use_ansi_color){
  44. fprintf(stderr, "\033[%d;3%dm", color>>4, color&15);
  45. }
  46. fputs(str, stderr);
  47. if(use_ansi_color){
  48. fprintf(stderr, "\033[0m");
  49. }
  50. }
  51. const char* av_default_item_name(void* ptr){
  52. return (*(AVClass**)ptr)->class_name;
  53. }
  54. void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
  55. {
  56. static int print_prefix=1;
  57. static int count;
  58. static char line[1024], prev[1024];
  59. static const uint8_t color[]={0x41,0x41,0x11,0x03,9,9,9};
  60. AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
  61. if(level>av_log_level)
  62. return;
  63. #undef fprintf
  64. if(print_prefix && avc) {
  65. snprintf(line, sizeof(line), "[%s @ %p]", avc->item_name(ptr), ptr);
  66. }else
  67. line[0]=0;
  68. vsnprintf(line + strlen(line), sizeof(line) - strlen(line), fmt, vl);
  69. print_prefix= line[strlen(line)-1] == '\n';
  70. if(print_prefix && !strcmp(line, prev)){
  71. count++;
  72. return;
  73. }
  74. if(count>0){
  75. fprintf(stderr, " Last message repeated %d times\n", count);
  76. count=0;
  77. }
  78. colored_fputs(color[av_clip(level>>3, 0, 6)], line);
  79. strcpy(prev, line);
  80. }
  81. static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
  82. void av_log(void* avcl, int level, const char *fmt, ...)
  83. {
  84. AVClass* avc= avcl ? *(AVClass**)avcl : NULL;
  85. va_list vl;
  86. va_start(vl, fmt);
  87. if(avc && avc->version >= (50<<16 | 15<<8 | 2) && avc->log_level_offset_offset && level>=AV_LOG_FATAL)
  88. level += *(int*)(((uint8_t*)avcl) + avc->log_level_offset_offset);
  89. av_vlog(avcl, level, fmt, vl);
  90. va_end(vl);
  91. }
  92. void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
  93. {
  94. av_log_callback(avcl, level, fmt, vl);
  95. }
  96. int av_log_get_level(void)
  97. {
  98. return av_log_level;
  99. }
  100. void av_log_set_level(int level)
  101. {
  102. av_log_level = level;
  103. }
  104. void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
  105. {
  106. av_log_callback = callback;
  107. }