Prop210: Refactor connection_get_* to produce lists and counts
[tor.git] / src / test / log_test_helpers.c
blob51b5f9b7b10c377a759ca9c9cfc6b488d1f4e688
1 /* Copyright (c) 2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 #define LOG_PRIVATE
4 #include "torlog.h"
5 #include "log_test_helpers.h"
7 static smartlist_t *saved_logs = NULL;
9 int
10 setup_capture_of_logs(int new_level)
12 int previous_log = log_global_min_severity_;
13 log_global_min_severity_ = new_level;
14 mock_clean_saved_logs();
15 MOCK(logv, mock_saving_logv);
16 return previous_log;
19 void
20 teardown_capture_of_logs(int prev)
22 UNMOCK(logv);
23 log_global_min_severity_ = prev;
24 mock_clean_saved_logs();
27 void
28 mock_clean_saved_logs(void)
30 if (!saved_logs)
31 return;
32 SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
33 { tor_free(m->generated_msg); tor_free(m); });
34 smartlist_free(saved_logs);
35 saved_logs = NULL;
38 static mock_saved_log_entry_t *
39 mock_get_log_entry(int ix)
41 int saved_log_count = mock_saved_log_number();
42 if (ix < 0) {
43 ix = saved_log_count + ix;
46 if (saved_log_count <= ix)
47 return NULL;
49 return smartlist_get(saved_logs, ix);
52 const char *
53 mock_saved_log_at(int ix)
55 mock_saved_log_entry_t *ent = mock_get_log_entry(ix);
56 if (ent)
57 return ent->generated_msg;
58 else
59 return "";
62 int
63 mock_saved_severity_at(int ix)
65 mock_saved_log_entry_t *ent = mock_get_log_entry(ix);
66 if (ent)
67 return ent->severity;
68 else
69 return -1;
72 int
73 mock_saved_log_number(void)
75 if (!saved_logs)
76 return 0;
77 return smartlist_len(saved_logs);
80 const smartlist_t *
81 mock_saved_logs(void)
83 return saved_logs;
86 void
87 mock_saving_logv(int severity, log_domain_mask_t domain,
88 const char *funcname, const char *suffix,
89 const char *format, va_list ap)
91 (void)domain;
92 char *buf = tor_malloc_zero(10240);
93 int n;
94 n = tor_vsnprintf(buf,10240,format,ap);
95 tor_assert(n < 10240-1);
96 buf[n]='\n';
97 buf[n+1]='\0';
99 mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
100 e->severity = severity;
101 e->funcname = funcname;
102 e->suffix = suffix;
103 e->format = format;
104 e->generated_msg = tor_strdup(buf);
105 tor_free(buf);
107 if (!saved_logs)
108 saved_logs = smartlist_new();
109 smartlist_add(saved_logs, e);