util.c 1.6 KB

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