aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/qpdflogger-c.cc
blob: 46fd9fbaa24884858492e9c5f9c4506969fe3b2e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <qpdf/qpdflogger-c.h>

#include <qpdf/Pipeline.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QPDFLogger.hh>
#include <functional>
#include <memory>

struct _qpdflogger_handle
{
    _qpdflogger_handle(std::shared_ptr<QPDFLogger> l);
    ~_qpdflogger_handle() = default;

    std::shared_ptr<QPDFLogger> l;
};

namespace
{
    class FunctionPipeline: public Pipeline
    {
      public:
        FunctionPipeline(char const* identifier, qpdf_log_fn_t fn, void* udata);
        virtual ~FunctionPipeline() = default;

        virtual void write(unsigned char const* buf, size_t len) override;
        virtual void finish() override;

      private:
        qpdf_log_fn_t fn;
        void* udata;
    };
}; // namespace

FunctionPipeline::FunctionPipeline(
    char const* identifier, qpdf_log_fn_t fn, void* udata) :
    Pipeline(identifier, nullptr),
    fn(fn),
    udata(udata)
{
}

void
FunctionPipeline::write(unsigned char const* buf, size_t len)
{
    fn(reinterpret_cast<char const*>(buf), QIntC::to_ulong(len), udata);
}

void
FunctionPipeline::finish()
{
    // Nothing needed
}

_qpdflogger_handle::_qpdflogger_handle(std::shared_ptr<QPDFLogger> l) :
    l(l)
{
}

qpdflogger_handle
qpdflogger_default_logger()
{
    return new _qpdflogger_handle(QPDFLogger::defaultLogger());
}

void
qpdflogger_cleanup(qpdflogger_handle* l)
{
    delete *l;
    *l = nullptr;
}

static void
set_log_dest(
    QPDFLogger* l,
    std::function<void(std::shared_ptr<Pipeline>)> method,
    qpdf_log_dest_e dest,
    char const* identifier,
    qpdf_log_fn_t fn,
    void* udata)
{
    switch (dest) {
    case qpdf_log_dest_default:
        method(nullptr);
        break;
    case qpdf_log_dest_stdout:
        method(l->standardOutput());
        break;
    case qpdf_log_dest_stderr:
        method(l->standardError());
        break;
    case qpdf_log_dest_discard:
        method(l->discard());
        break;
    case qpdf_log_dest_custom:
        method(std::make_shared<FunctionPipeline>(identifier, fn, udata));
        break;
    }
}

static void
set_log_dest(
    QPDFLogger* l,
    void (QPDFLogger::*method)(std::shared_ptr<Pipeline>),
    qpdf_log_dest_e dest,
    char const* identifier,
    qpdf_log_fn_t fn,
    void* udata)
{
    set_log_dest(
        l,
        std::bind(std::mem_fn(method), l, std::placeholders::_1),
        dest,
        identifier,
        fn,
        udata);
}

void
qpdflogger_set_info(
    qpdflogger_handle l, qpdf_log_dest_e dest, qpdf_log_fn_t fn, void* udata)
{
    set_log_dest(
        l->l.get(), &QPDFLogger::setInfo, dest, "info logger", fn, udata);
}

void
qpdflogger_set_warn(
    qpdflogger_handle l, qpdf_log_dest_e dest, qpdf_log_fn_t fn, void* udata)
{
    set_log_dest(
        l->l.get(), &QPDFLogger::setWarn, dest, "warn logger", fn, udata);
}

void
qpdflogger_set_error(
    qpdflogger_handle l, qpdf_log_dest_e dest, qpdf_log_fn_t fn, void* udata)
{
    set_log_dest(
        l->l.get(), &QPDFLogger::setError, dest, "error logger", fn, udata);
}

void
qpdflogger_set_save(
    qpdflogger_handle l,
    qpdf_log_dest_e dest,
    qpdf_log_fn_t fn,
    void* udata,
    int only_if_not_set)
{
    auto method = std::bind(
        std::mem_fn(&QPDFLogger::setSave),
        l->l.get(),
        std::placeholders::_1,
        only_if_not_set);
    set_log_dest(l->l.get(), method, dest, "save logger", fn, udata);
}

void
qpdflogger_save_to_standard_output(qpdflogger_handle l, int only_if_not_set)
{
    qpdflogger_set_save(
        l, qpdf_log_dest_stdout, nullptr, nullptr, only_if_not_set);
}