util.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util.h"
  3. #include "../util/debug.h"
  4. #include <stdio.h>
  5. /*
  6. * Default error logging functions
  7. */
  8. static int perf_stdio__error(const char *format, va_list args)
  9. {
  10. fprintf(stderr, "Error:\n");
  11. vfprintf(stderr, format, args);
  12. return 0;
  13. }
  14. static int perf_stdio__warning(const char *format, va_list args)
  15. {
  16. if (quiet)
  17. return 0;
  18. fprintf(stderr, "Warning:\n");
  19. vfprintf(stderr, format, args);
  20. return 0;
  21. }
  22. static struct perf_error_ops default_eops =
  23. {
  24. .error = perf_stdio__error,
  25. .warning = perf_stdio__warning,
  26. };
  27. static struct perf_error_ops *perf_eops = &default_eops;
  28. int ui__error(const char *format, ...)
  29. {
  30. int ret;
  31. va_list args;
  32. va_start(args, format);
  33. ret = perf_eops->error(format, args);
  34. va_end(args);
  35. return ret;
  36. }
  37. int ui__warning(const char *format, ...)
  38. {
  39. int ret;
  40. va_list args;
  41. if (quiet)
  42. return 0;
  43. va_start(args, format);
  44. ret = perf_eops->warning(format, args);
  45. va_end(args);
  46. return ret;
  47. }
  48. /**
  49. * perf_error__register - Register error logging functions
  50. * @eops: The pointer to error logging function struct
  51. *
  52. * Register UI-specific error logging functions. Before calling this,
  53. * other logging functions should be unregistered, if any.
  54. */
  55. int perf_error__register(struct perf_error_ops *eops)
  56. {
  57. if (perf_eops != &default_eops)
  58. return -1;
  59. perf_eops = eops;
  60. return 0;
  61. }
  62. /**
  63. * perf_error__unregister - Unregister error logging functions
  64. * @eops: The pointer to error logging function struct
  65. *
  66. * Unregister already registered error logging functions.
  67. */
  68. int perf_error__unregister(struct perf_error_ops *eops)
  69. {
  70. if (perf_eops != eops)
  71. return -1;
  72. perf_eops = &default_eops;
  73. return 0;
  74. }