File: spy_logger.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (243 lines) | stat: -rw-r--r-- 8,035 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "core/log/spy_logger.hpp"

#include "core/log/detail/spy_logger_impl.hxx"
#include "core/spy_log.hpp"

#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/file.hpp>

namespace sight::core::log
{

namespace keywords = boost::log::keywords;
namespace trivial  = boost::log::trivial;

// Static instance of the logger
spy_logger spy_logger::s_instance   = spy_logger();
SIGHT_CORE_API spy_logger& g_logger = spy_logger::s_instance;

spy_logger::spy_logger() :
    m_pimpl(std::make_unique<detail::spy_logger_impl>(this))
{
}

// Defining the destructor here, allows us to use PImpl with a unique_ptr
spy_logger::~spy_logger() = default;

//------------------------------------------------------------------------------

std::shared_ptr<spy_logger> spy_logger::make(
    const std::filesystem::path& _log_path,
    level_t _level,
    const optional_password_t& _password,
    bool _asynchronous
)
{
    SIGHT_THROW_IF("The log file path cannot be empty.", _log_path.empty());

    struct private_spy_logger final : public spy_logger
    {
        explicit private_spy_logger(
            const std::filesystem::path& _log_path,
            level_t _level,
            const optional_password_t& _password,
            bool _asynchronous
        ) :
            m_boost_logger(keywords::channel = _log_path.string())
        {
            // Replace the global logger by our own
            m_pimpl->m_boost_logger = m_boost_logger;

            // Start the logger. It will use _log_path as channel, separating from global logs
            m_pimpl->start(_log_path, _level, _password, _asynchronous, _log_path.string());
        }

        ~private_spy_logger() override
        {
            m_pimpl->stop();
        }

        // The boost logger, with our channel that will be used by global sinks
        detail::boost_logger_t m_boost_logger;
    };

    return std::make_shared<private_spy_logger>(_log_path, _level, _password, _asynchronous);
}

//------------------------------------------------------------------------------

std::ostream& spy_logger::extract(
    std::istream& _input,
    std::ostream& _output,
    const optional_password_t& _password
)
{
    return detail::spy_logger_impl::extract(_input, _output, _password);
}

//-----------------------------------------------------------------------------

boost::shared_ptr<boost::log::sinks::sink> spy_logger::add_global_console_log(std::ostream& _os, level_t _level)
{
    return boost::log::add_console_log(
        _os,
        keywords::format     = (detail::console_stream_format()),
        keywords::filter     = detail::severity >= static_cast<trivial::severity_level>(_level),
        keywords::auto_flush = true
    );
}

//-----------------------------------------------------------------------------

boost::shared_ptr<boost::log::sinks::sink> spy_logger::add_global_file_log(
    const std::filesystem::path& _log_file,
    level_t _level
)
{
    return boost::log::add_file_log(
        // file name pattern
        keywords::file_name = _log_file.string(),
        // rotate files every 10 MiB...
        keywords::rotation_size = 10 * 1024 * 1024,
        // ...or at midnight
        keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
        // log record format
        keywords::format     = (detail::file_stream_format()),
        keywords::filter     = detail::severity >= static_cast<trivial::severity_level>(_level),
        keywords::auto_flush = true
    );
}

//-----------------------------------------------------------------------------

void spy_logger::start(
    const std::filesystem::path& _log_path,
    level_t _level,
    const optional_password_t& _password,
    bool _asynchronous
)
{
    std::unique_lock lock(m_pimpl->m_mutex);
    m_pimpl->start(_log_path, _level, _password, _asynchronous);
}

//------------------------------------------------------------------------------

std::filesystem::path spy_logger::get_current_log_path() const
{
    std::unique_lock lock(m_pimpl->m_mutex);
    return m_pimpl->m_log_paths.empty() ? std::filesystem::path() : m_pimpl->m_log_paths.back();
}

//------------------------------------------------------------------------------

void spy_logger::stop()
{
    std::unique_lock lock(m_pimpl->m_mutex);
    m_pimpl->stop();
}

//------------------------------------------------------------------------------

bool spy_logger::is_log_encrypted() const
{
    std::unique_lock lock(m_pimpl->m_mutex);
    return m_pimpl->m_encrypted;
}

//------------------------------------------------------------------------------

std::filesystem::path spy_logger::change_password(
    const core::crypto::secure_string& _password,
    const optional_password_t& _old_password
)
{
    return relocate(m_pimpl->m_original_log_path, _password, _old_password);
}

//------------------------------------------------------------------------------

std::filesystem::path spy_logger::relocate(
    const std::filesystem::path& _new_path,
    const optional_password_t& _password,
    const optional_password_t& _old_password
)
{
    std::unique_lock lock(m_pimpl->m_mutex);

    return m_pimpl->relocate(_new_path, _password, _old_password);
}

//-----------------------------------------------------------------------------

#define FILE_LINE(msg) /* NOLINT(cppcoreguidelines-macro-usage) */ \
        "[" << (file != nullptr ? file : __FILE__) \
        << ":" << (line >= 0 ? line : __LINE__) << "] " \
        << (msg)

//------------------------------------------------------------------------------

void spy_logger::trace(const std::string_view& _mes, const char* file, int line)
{
    BOOST_LOG_SEV(m_pimpl->m_boost_logger, trivial::trace) << FILE_LINE(_mes);
}

//-----------------------------------------------------------------------------

void spy_logger::debug(const std::string_view& _mes, const char* file, int line)
{
    BOOST_LOG_SEV(m_pimpl->m_boost_logger, trivial::debug) << "[" << file << ":" << line << "] " << FILE_LINE(_mes);
}

//-----------------------------------------------------------------------------

void spy_logger::info(const std::string_view& _mes, const char* file, int line)
{
    BOOST_LOG_SEV(m_pimpl->m_boost_logger, trivial::info) << FILE_LINE(_mes);
}

//-----------------------------------------------------------------------------

void spy_logger::warn(const std::string_view& _mes, const char* file, int line)
{
    BOOST_LOG_SEV(m_pimpl->m_boost_logger, trivial::warning) << FILE_LINE(_mes);
}

//-----------------------------------------------------------------------------

void spy_logger::error(const std::string_view& _mes, const char* file, int line)
{
    BOOST_LOG_SEV(m_pimpl->m_boost_logger, trivial::error) << FILE_LINE(_mes);
}

//-----------------------------------------------------------------------------

void spy_logger::fatal(const std::string_view& _mes, const char* file, int line)
{
    BOOST_LOG_SEV(m_pimpl->m_boost_logger, trivial::fatal) << FILE_LINE(_mes);
}

} // namespace sight::core::log