log_test_helpers.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (c) 2015-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define LOG_PRIVATE
  4. #include "torlog.h"
  5. #include "log_test_helpers.h"
  6. static smartlist_t *saved_logs = NULL;
  7. int
  8. setup_capture_of_logs(int new_level)
  9. {
  10. int previous_log = log_global_min_severity_;
  11. log_global_min_severity_ = new_level;
  12. mock_clean_saved_logs();
  13. saved_logs = smartlist_new();
  14. MOCK(logv, mock_saving_logv);
  15. return previous_log;
  16. }
  17. void
  18. teardown_capture_of_logs(int prev)
  19. {
  20. UNMOCK(logv);
  21. log_global_min_severity_ = prev;
  22. mock_clean_saved_logs();
  23. }
  24. void
  25. mock_clean_saved_logs(void)
  26. {
  27. if (!saved_logs)
  28. return;
  29. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  30. { tor_free(m->generated_msg); tor_free(m); });
  31. smartlist_free(saved_logs);
  32. saved_logs = NULL;
  33. }
  34. const smartlist_t *
  35. mock_saved_logs(void)
  36. {
  37. return saved_logs;
  38. }
  39. int
  40. mock_saved_log_has_message(const char *msg)
  41. {
  42. int has_msg = 0;
  43. if (saved_logs) {
  44. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  45. {
  46. if (msg && m->generated_msg &&
  47. !strcmp(msg, m->generated_msg)) {
  48. has_msg = 1;
  49. }
  50. });
  51. }
  52. return has_msg;
  53. }
  54. /* Do the saved logs have any messages with severity? */
  55. int
  56. mock_saved_log_has_severity(int severity)
  57. {
  58. int has_sev = 0;
  59. if (saved_logs) {
  60. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  61. {
  62. if (m->severity == severity) {
  63. has_sev = 1;
  64. }
  65. });
  66. }
  67. return has_sev;
  68. }
  69. /* Do the saved logs have any messages? */
  70. int
  71. mock_saved_log_has_entry(void)
  72. {
  73. if (saved_logs) {
  74. return smartlist_len(saved_logs) > 0;
  75. }
  76. return 0;
  77. }
  78. void
  79. mock_saving_logv(int severity, log_domain_mask_t domain,
  80. const char *funcname, const char *suffix,
  81. const char *format, va_list ap)
  82. {
  83. (void)domain;
  84. char *buf = tor_malloc_zero(10240);
  85. int n;
  86. n = tor_vsnprintf(buf,10240,format,ap);
  87. tor_assert(n < 10240-1);
  88. buf[n]='\n';
  89. buf[n+1]='\0';
  90. mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
  91. e->severity = severity;
  92. e->funcname = funcname;
  93. e->suffix = suffix;
  94. e->format = format;
  95. e->generated_msg = tor_strdup(buf);
  96. tor_free(buf);
  97. if (!saved_logs)
  98. saved_logs = smartlist_new();
  99. smartlist_add(saved_logs, e);
  100. }