util.c 1.6 KB

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