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.

120 lines
3.1KB

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