log_test_helpers.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #define LOG_PRIVATE
  2. #include "torlog.h"
  3. #include "log_test_helpers.h"
  4. static smartlist_t *saved_logs = NULL;
  5. int
  6. setup_capture_of_logs(int new_level)
  7. {
  8. int previous_log = log_global_min_severity_;
  9. log_global_min_severity_ = new_level;
  10. mock_clean_saved_logs();
  11. MOCK(logv, mock_saving_logv);
  12. return previous_log;
  13. }
  14. void
  15. teardown_capture_of_logs(int prev)
  16. {
  17. UNMOCK(logv);
  18. log_global_min_severity_ = prev;
  19. mock_clean_saved_logs();
  20. }
  21. void
  22. mock_clean_saved_logs(void)
  23. {
  24. if (!saved_logs)
  25. return;
  26. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  27. { tor_free(m->generated_msg); tor_free(m); });
  28. smartlist_free(saved_logs);
  29. saved_logs = NULL;
  30. }
  31. const char *
  32. mock_saved_log_at(int ix)
  33. {
  34. int saved_log_count = mock_saved_log_number();
  35. if (ix < 0) {
  36. ix = saved_log_count + ix;
  37. }
  38. if (saved_log_count <= ix)
  39. return "";
  40. return ((mock_saved_log_entry_t *)
  41. smartlist_get(saved_logs, ix))->generated_msg;
  42. }
  43. int
  44. mock_saved_severity_at(int ix)
  45. {
  46. int saved_log_count = mock_saved_log_number();
  47. if (ix < 0) {
  48. ix = saved_log_count + ix;
  49. }
  50. if (saved_log_count <= ix)
  51. return -1;
  52. return ((mock_saved_log_entry_t *)smartlist_get(saved_logs, ix))->severity;
  53. }
  54. int
  55. mock_saved_log_number(void)
  56. {
  57. if (!saved_logs)
  58. return 0;
  59. return smartlist_len(saved_logs);
  60. }
  61. const smartlist_t *
  62. mock_saved_logs(void)
  63. {
  64. return saved_logs;
  65. }
  66. void
  67. mock_saving_logv(int severity, log_domain_mask_t domain, const char *funcname,
  68. const char *suffix, const char *format, va_list ap)
  69. {
  70. (void)domain;
  71. char *buf = tor_malloc_zero(10240);
  72. int n;
  73. n = tor_vsnprintf(buf,10240,format,ap);
  74. buf[n]='\n';
  75. buf[n+1]='\0';
  76. mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
  77. e->severity = severity;
  78. e->funcname = funcname;
  79. e->suffix = suffix;
  80. e->format = format;
  81. e->generated_msg = buf;
  82. if (!saved_logs)
  83. saved_logs = smartlist_new();
  84. smartlist_add(saved_logs, e);
  85. }