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.

118 lines
2.9KB

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