summaryrefslogtreecommitdiff
path: root/implementation/logging/src/logger_impl.cpp
blob: 6bf4e08d4eb8a372b866cc689b5fc737d14855af (plain)
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
// Copyright (C) 2014-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <fstream>

#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/phoenix/bind/bind_member_function.hpp>
#include <boost/shared_ptr.hpp>

// The "empty_deleter"-struct was moved from the log-package
// to the more generic "utility"-package in V1.55. If we'd
// use the "old" include, we get a "deprecation" warning
// when compiling with the newer boost version. Therefore a
// version dependent include handling is done here, which
// can/should be removed in case GPT is updating Boost to V1.55.
#if BOOST_VERSION < 105500
#include <boost/log/utility/empty_deleter.hpp>
#elif BOOST_VERSION < 105600
#include <boost/utility/empty_deleter.hpp>
#else
#include <boost/core/null_deleter.hpp>
#endif

#include "../include/logger_impl.hpp"
#include "../../configuration/include/configuration.hpp"

namespace logging = boost::log;
namespace sources = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expressions = boost::log::expressions;
namespace attributes = boost::log::attributes;

using namespace boost::log::trivial;

namespace vsomeip {

std::shared_ptr<logger_impl> & logger_impl::get() {
    static std::shared_ptr<logger_impl> the_logger__ = std::make_shared<
            logger_impl>();
    return the_logger__;
}

logger_impl::logger_impl()
        : loglevel_(debug) {
    logging::add_common_attributes();
}

boost::log::sources::severity_logger<boost::log::trivial::severity_level> &
logger_impl::get_internal() {
    return logger_;
}

void logger_impl::init(const std::shared_ptr<configuration> &_configuration) {
    get()->loglevel_ = _configuration->get_loglevel();

    logging::core::get()->set_filter(
            logging::trivial::severity >= get()->loglevel_);

    if (_configuration->has_console_log())
        get()->enable_console();

    if (_configuration->has_file_log())
        get()->enable_file(_configuration->get_logfile());

    if (_configuration->has_dlt_log())
        get()->enable_dlt();
}

void logger_impl::enable_console() {
    if (console_sink_)
        return;

    auto vsomeip_log_format = expressions::stream
            << expressions::format_date_time<boost::posix_time::ptime>(
                    "TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << " ["
            << expressions::attr<severity_level>("Severity") << "] "
            << expressions::smessage;

    boost::shared_ptr<sinks::text_ostream_backend> backend = boost::make_shared<
            sinks::text_ostream_backend>();
    backend->add_stream(boost::shared_ptr<std::ostream>(&std::clog,
#if BOOST_VERSION < 105500
            boost::log::empty_deleter()
#elif BOOST_VERSION < 105600
            boost::empty_deleter()
#else
            boost::null_deleter()
#endif
            ));

    console_sink_ = boost::make_shared<sink_t>(backend);
    console_sink_->set_formatter(vsomeip_log_format);
    logging::core::get()->add_sink(console_sink_);
}

void logger_impl::enable_file(const std::string &_path) {
    if (file_sink_)
        return;

    auto vsomeip_log_format = expressions::stream
            << expressions::format_date_time<boost::posix_time::ptime>(
                    "TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << " ["
            << expressions::attr<severity_level>("Severity") << "] "
            << expressions::smessage;

    boost::shared_ptr<sinks::text_ostream_backend> backend = boost::make_shared<
            sinks::text_ostream_backend>();
    backend->add_stream(
            boost::shared_ptr<std::ostream>(
                    boost::make_shared<std::ofstream>(_path)));

    file_sink_ = boost::make_shared<sink_t>(backend);
    file_sink_->set_formatter(vsomeip_log_format);
    logging::core::get()->add_sink(file_sink_);
}

void logger_impl::enable_dlt() {
#ifdef USE_DLT
    if (dlt_sink_)
        return;

    boost::shared_ptr<dlt_sink_backend> backend = boost::make_shared<dlt_sink_backend>();
    dlt_sink_ = boost::make_shared<dlt_sink_t>(backend);
    logging::core::get()->add_sink(dlt_sink_);
#endif
}

void logger_impl::use_null_logger() {
    boost::shared_ptr<sinks::text_ostream_backend> backend = boost::make_shared<
            sinks::text_ostream_backend>();
    backend->add_stream(
            boost::shared_ptr<std::ostream>(new std::ofstream("/dev/null") // TODO: how to call this on windows
                    ));

    file_sink_ = boost::make_shared<sink_t>(backend);
    logging::core::get()->add_sink(file_sink_);
}

} // namespace vsomeip