log_test_helpers.c 2.4 KB

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