util.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. fprintf(stderr, "Warning:\n");
  17. vfprintf(stderr, format, args);
  18. return 0;
  19. }
  20. static struct perf_error_ops default_eops =
  21. {
  22. .error = perf_stdio__error,
  23. .warning = perf_stdio__warning,
  24. };
  25. static struct perf_error_ops *perf_eops = &default_eops;
  26. int ui__error(const char *format, ...)
  27. {
  28. int ret;
  29. va_list args;
  30. va_start(args, format);
  31. ret = perf_eops->error(format, args);
  32. va_end(args);
  33. return ret;
  34. }
  35. int ui__warning(const char *format, ...)
  36. {
  37. int ret;
  38. va_list args;
  39. va_start(args, format);
  40. ret = perf_eops->warning(format, args);
  41. va_end(args);
  42. return ret;
  43. }
  44. /**
  45. * perf_error__register - Register error logging functions
  46. * @eops: The pointer to error logging function struct
  47. *
  48. * Register UI-specific error logging functions. Before calling this,
  49. * other logging functions should be unregistered, if any.
  50. */
  51. int perf_error__register(struct perf_error_ops *eops)
  52. {
  53. if (perf_eops != &default_eops)
  54. return -1;
  55. perf_eops = eops;
  56. return 0;
  57. }
  58. /**
  59. * perf_error__unregister - Unregister error logging functions
  60. * @eops: The pointer to error logging function struct
  61. *
  62. * Unregister already registered error logging functions.
  63. */
  64. int perf_error__unregister(struct perf_error_ops *eops)
  65. {
  66. if (perf_eops != eops)
  67. return -1;
  68. perf_eops = &default_eops;
  69. return 0;
  70. }