log_test_helpers.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. static int echo_to_real_logs = 1;
  8. int
  9. setup_full_capture_of_logs(int new_level)
  10. {
  11. int result = setup_capture_of_logs(new_level);
  12. echo_to_real_logs = 0;
  13. return result;
  14. }
  15. int
  16. setup_capture_of_logs(int new_level)
  17. {
  18. int previous_log = log_global_min_severity_;
  19. log_global_min_severity_ = new_level;
  20. mock_clean_saved_logs();
  21. saved_logs = smartlist_new();
  22. MOCK(logv, mock_saving_logv);
  23. echo_to_real_logs = 1;
  24. return previous_log;
  25. }
  26. void
  27. teardown_capture_of_logs(int prev)
  28. {
  29. UNMOCK(logv);
  30. log_global_min_severity_ = prev;
  31. mock_clean_saved_logs();
  32. }
  33. void
  34. mock_clean_saved_logs(void)
  35. {
  36. if (!saved_logs)
  37. return;
  38. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  39. { tor_free(m->generated_msg); tor_free(m); });
  40. smartlist_free(saved_logs);
  41. saved_logs = NULL;
  42. }
  43. const smartlist_t *
  44. mock_saved_logs(void)
  45. {
  46. return saved_logs;
  47. }
  48. int
  49. mock_saved_log_has_message(const char *msg)
  50. {
  51. int has_msg = 0;
  52. if (saved_logs) {
  53. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  54. {
  55. if (msg && m->generated_msg &&
  56. !strcmp(msg, m->generated_msg)) {
  57. has_msg = 1;
  58. }
  59. });
  60. }
  61. return has_msg;
  62. }
  63. int
  64. mock_saved_log_has_message_containing(const char *msg)
  65. {
  66. if (saved_logs) {
  67. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  68. {
  69. if (msg && m->generated_msg &&
  70. strstr(m->generated_msg, msg)) {
  71. return 1;
  72. }
  73. });
  74. }
  75. return 0;
  76. }
  77. /* Do the saved logs have any messages with severity? */
  78. int
  79. mock_saved_log_has_severity(int severity)
  80. {
  81. int has_sev = 0;
  82. if (saved_logs) {
  83. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  84. {
  85. if (m->severity == severity) {
  86. has_sev = 1;
  87. }
  88. });
  89. }
  90. return has_sev;
  91. }
  92. /* Do the saved logs have any messages? */
  93. int
  94. mock_saved_log_has_entry(void)
  95. {
  96. if (saved_logs) {
  97. return smartlist_len(saved_logs) > 0;
  98. }
  99. return 0;
  100. }
  101. void
  102. mock_saving_logv(int severity, log_domain_mask_t domain,
  103. const char *funcname, const char *suffix,
  104. const char *format, va_list ap)
  105. {
  106. char *buf = tor_malloc_zero(10240);
  107. int n;
  108. n = tor_vsnprintf(buf,10240,format,ap);
  109. tor_assert(n < 10240-1);
  110. buf[n]='\n';
  111. buf[n+1]='\0';
  112. mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
  113. e->severity = severity;
  114. e->funcname = funcname;
  115. e->suffix = suffix;
  116. e->format = format;
  117. e->generated_msg = tor_strdup(buf);
  118. tor_free(buf);
  119. if (!saved_logs)
  120. saved_logs = smartlist_new();
  121. smartlist_add(saved_logs, e);
  122. if (echo_to_real_logs) {
  123. tor_log(severity, domain|LD_NO_MOCK, "%s", e->generated_msg);
  124. }
  125. }