2015-10-02 13:13:58 +02:00
|
|
|
/* Copyright (c) 2015, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
2015-09-15 17:20:44 +02:00
|
|
|
#define LOG_PRIVATE
|
|
|
|
#include "torlog.h"
|
|
|
|
#include "log_test_helpers.h"
|
|
|
|
|
|
|
|
static smartlist_t *saved_logs = NULL;
|
|
|
|
|
|
|
|
int
|
|
|
|
setup_capture_of_logs(int new_level)
|
|
|
|
{
|
|
|
|
int previous_log = log_global_min_severity_;
|
|
|
|
log_global_min_severity_ = new_level;
|
|
|
|
mock_clean_saved_logs();
|
|
|
|
MOCK(logv, mock_saving_logv);
|
|
|
|
return previous_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
teardown_capture_of_logs(int prev)
|
|
|
|
{
|
|
|
|
UNMOCK(logv);
|
|
|
|
log_global_min_severity_ = prev;
|
|
|
|
mock_clean_saved_logs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mock_clean_saved_logs(void)
|
|
|
|
{
|
|
|
|
if (!saved_logs)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
|
|
|
|
{ tor_free(m->generated_msg); tor_free(m); });
|
|
|
|
smartlist_free(saved_logs);
|
|
|
|
saved_logs = NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-02 13:03:43 +02:00
|
|
|
static mock_saved_log_entry_t *
|
|
|
|
mock_get_log_entry(int ix)
|
2015-09-15 17:20:44 +02:00
|
|
|
{
|
|
|
|
int saved_log_count = mock_saved_log_number();
|
2015-10-02 13:13:58 +02:00
|
|
|
if (ix < 0) {
|
2015-09-15 17:20:44 +02:00
|
|
|
ix = saved_log_count + ix;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saved_log_count <= ix)
|
2015-10-02 13:03:43 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return smartlist_get(saved_logs, ix);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
mock_saved_log_at(int ix)
|
|
|
|
{
|
|
|
|
mock_saved_log_entry_t *ent = mock_get_log_entry(ix);
|
|
|
|
if (ent)
|
|
|
|
return ent->generated_msg;
|
|
|
|
else
|
2015-09-15 17:20:44 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
mock_saved_severity_at(int ix)
|
|
|
|
{
|
2015-10-02 13:03:43 +02:00
|
|
|
mock_saved_log_entry_t *ent = mock_get_log_entry(ix);
|
|
|
|
if (ent)
|
|
|
|
return ent->severity;
|
|
|
|
else
|
2015-09-15 17:20:44 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
mock_saved_log_number(void)
|
|
|
|
{
|
|
|
|
if (!saved_logs)
|
|
|
|
return 0;
|
|
|
|
return smartlist_len(saved_logs);
|
|
|
|
}
|
|
|
|
|
|
|
|
const smartlist_t *
|
|
|
|
mock_saved_logs(void)
|
|
|
|
{
|
|
|
|
return saved_logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-02 13:03:43 +02:00
|
|
|
mock_saving_logv(int severity, log_domain_mask_t domain,
|
|
|
|
const char *funcname, const char *suffix,
|
|
|
|
const char *format, va_list ap)
|
2015-09-15 17:20:44 +02:00
|
|
|
{
|
2015-10-02 13:03:43 +02:00
|
|
|
(void)domain;
|
2015-09-15 17:20:44 +02:00
|
|
|
char *buf = tor_malloc_zero(10240);
|
|
|
|
int n;
|
|
|
|
n = tor_vsnprintf(buf,10240,format,ap);
|
2015-10-02 13:03:43 +02:00
|
|
|
tor_assert(n < 10240-1);
|
2015-09-15 17:20:44 +02:00
|
|
|
buf[n]='\n';
|
|
|
|
buf[n+1]='\0';
|
|
|
|
|
|
|
|
mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
|
|
|
|
e->severity = severity;
|
|
|
|
e->funcname = funcname;
|
|
|
|
e->suffix = suffix;
|
|
|
|
e->format = format;
|
2015-10-02 13:03:43 +02:00
|
|
|
e->generated_msg = tor_strdup(buf);
|
|
|
|
tor_free(buf);
|
2015-09-15 17:20:44 +02:00
|
|
|
|
|
|
|
if (!saved_logs)
|
|
|
|
saved_logs = smartlist_new();
|
|
|
|
smartlist_add(saved_logs, e);
|
|
|
|
}
|
2016-01-15 16:57:03 +01:00
|
|
|
|