aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/issue.cpp
blob: 134672f34595ac3f7b843e0344fc1202eeaf38d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "issue.h"
#include "stringfmt.h"

#include <vespa/log/log.h>
LOG_SETUP(".vespalib.issue");

namespace vespalib {

namespace {

using Link = Issue::Binding::Link;

struct LogIssues : Issue::Handler {
    void handle(const Issue &issue) override {
        LOG(warning, "%s", issue.message().c_str());
    }
};

Link *get_root() {
    static LogIssues log_issues;
    static Link root{log_issues, nullptr};
    return &root;
}

Link **get_head() {
    thread_local Link *head = get_root();
    return &head;
}

} // <unnamed>

Issue::Issue(vespalib::string message)
  : _message(std::move(message))
{
}

Issue::Binding::Binding(Handler &handler)
  : _link{handler, nullptr}
{
    Link **head = get_head();
    _link.next = *head;
    *head = &_link;
}

Issue::Binding::~Binding()
{
    Link **head = get_head();
    LOG_ASSERT(*head == &_link);
    *head = (*head)->next;
}

void
Issue::report(const Issue &issue)
{
    (*get_head())->handler.handle(issue);
}

Issue::Binding
Issue::listen(Handler &handler)
{
    return Binding(handler);
}

void
Issue::report(vespalib::string msg)
{
    report(Issue(std::move(msg)));
}

void
Issue::report(const std::exception &e)
{
    report(Issue(e.what()));
}

void
Issue::report(const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    vespalib::string msg = make_string_va(format, ap);
    va_end(ap);
    report(Issue(std::move(msg)));
}

}