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.

110 lines
2.7KB

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