• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 950534094

pending completion
950534094

push

gitlab-ci

kacf
chore: Add `StartsWith` and `EndsWith` generic utility functions.

Also use the latter in `states.cpp`.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

1 of 1 new or added line in 1 file covered. (100.0%)

4931 of 6276 relevant lines covered (78.57%)

196.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.69
/common/log/platform/boost/boost_log.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <common/log.hpp>
16

17

18
#include <boost/smart_ptr/shared_ptr.hpp>
19
#include <boost/core/null_deleter.hpp>
20
#include <boost/date_time/posix_time/posix_time.hpp>
21
#include <boost/log/common.hpp>
22
#include <boost/log/expressions.hpp>
23
#include <boost/log/attributes.hpp>
24
#include <boost/log/sinks.hpp>
25
#include <boost/log/sources/logger.hpp>
26
#include <boost/log/utility/manipulators/add_value.hpp>
27
#include <boost/log/attributes/scoped_attribute.hpp>
28
#include <boost/log/support/date_time.hpp>
29

30
#include <string>
31
#include <fstream>
32
#include <common/error.hpp>
33
#include <common/expected.hpp>
34

35
namespace mender {
36
namespace common {
37
namespace log {
38

39
namespace logging = boost::log;
40
namespace expr = boost::log::expressions;
41
namespace sinks = boost::log::sinks;
42
namespace attrs = boost::log::attributes;
43
namespace src = boost::log::sources;
44

45
namespace error = mender::common::error;
46
namespace expected = mender::common::expected;
47

48
using namespace std;
49

50

51
const LogErrorCategoryClass LogErrorCategory;
52

53
const char *LogErrorCategoryClass::name() const noexcept {
×
54
        return "LogErrorCategory";
×
55
}
56

57
string LogErrorCategoryClass::message(int code) const {
×
58
        switch (code) {
×
59
        case NoError:
×
60
                return "Success";
×
61
        case InvalidLogLevelError:
×
62
                return "Invalid log level given";
×
63
        case LogFileError:
×
64
                return "Bad log file";
×
65
        default:
×
66
                return "Unknown";
×
67
        }
68
}
69

70
error::Error MakeError(LogErrorCode code, const string &msg) {
2✔
71
        return error::Error(error_condition(code, LogErrorCategory), msg);
4✔
72
}
73

74
ExpectedLogLevel StringToLogLevel(const string &level_str) {
78✔
75
        if (level_str == "fatal") {
78✔
76
                return ExpectedLogLevel(LogLevel::Fatal);
×
77
        } else if (level_str == "error") {
78✔
78
                return ExpectedLogLevel(LogLevel::Error);
×
79
        } else if (level_str == "warning") {
78✔
80
                return ExpectedLogLevel(LogLevel::Warning);
×
81
        } else if (level_str == "info") {
78✔
82
                return ExpectedLogLevel(LogLevel::Info);
156✔
83
        } else if (level_str == "debug") {
×
84
                return ExpectedLogLevel(LogLevel::Debug);
×
85
        } else if (level_str == "trace") {
×
86
                return ExpectedLogLevel(LogLevel::Trace);
×
87
        } else {
88
                return ExpectedLogLevel(expected::unexpected(MakeError(
×
89
                        LogErrorCode::InvalidLogLevelError, "'" + level_str + "' is not a valid log level")));
×
90
        }
91
}
92

93
static void LogfmtFormatter(logging::record_view const &rec, logging::formatting_ostream &strm) {
7,432✔
94
        strm << "record_id=" << logging::extract<unsigned int>("RecordID", rec) << " ";
7,432✔
95

96
        auto level = logging::extract<LogLevel>("Severity", rec);
7,425✔
97
        if (level) {
7,422✔
98
                std::string lvl = ToStringLogLevel(level.get());
14,855✔
99
                strm << "severity=" << lvl << " ";
7,433✔
100
        }
101

102
        auto val = logging::extract<boost::posix_time::ptime>("TimeStamp", rec);
7,426✔
103
        if (val) {
7,420✔
104
                strm << "time=\"" << val.get() << "\" ";
7,420✔
105
        }
106

107
        auto name = logging::extract<std::string>("Name", rec);
7,428✔
108
        if (name) {
7,412✔
109
                strm << "name=\"" << name.get() << "\" ";
7,412✔
110
        }
111

112
        for (auto f : rec.attribute_values()) {
48,315✔
113
                auto field = logging::extract<LogField>(f.first.string(), rec);
40,919✔
114
                if (field) {
40,881✔
115
                        strm << field.get().key << "=\"" << field.get().value << "\" ";
3,773✔
116
                }
117
        }
118

119
        strm << "msg=\"" << rec[expr::smessage] << "\" ";
7,432✔
120
}
7,424✔
121

122
static void SetupLoggerSinks() {
386✔
123
        typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
124
        boost::shared_ptr<text_sink> sink(new text_sink);
386✔
125

126
        {
127
                text_sink::locked_backend_ptr pBackend = sink->locked_backend();
772✔
128
                boost::shared_ptr<std::ostream> pStream(&std::clog, boost::null_deleter());
772✔
129
                pBackend->add_stream(pStream);
386✔
130
        }
131

132
        sink->set_formatter(&LogfmtFormatter);
386✔
133

134
        logging::core::get()->add_sink(sink);
386✔
135
}
386✔
136

137
static void SetupLoggerAttributes() {
386✔
138
        attrs::counter<unsigned int> RecordID(1);
772✔
139
        logging::core::get()->add_global_attribute("RecordID", RecordID);
386✔
140

141
        attrs::local_clock TimeStamp;
386✔
142
        logging::core::get()->add_global_attribute("TimeStamp", TimeStamp);
386✔
143
}
386✔
144

145
Logger::Logger(const string &name) :
805✔
146
        Logger(name, global_logger_.Level()) {
805✔
147
}
805✔
148

149
Logger::Logger(const string &name, LogLevel level) :
1,192✔
150
        name_(name),
151
        level_(level) {
1,192✔
152
        src::severity_logger<LogLevel> slg;
2,384✔
153
        slg.add_attribute("Name", attrs::constant<std::string>(name));
1,192✔
154
        this->logger = slg;
1,192✔
155
}
1,192✔
156

157
void Logger::Log_(LogLevel level, const string &message) {
7,433✔
158
        BOOST_LOG_SEV(this->logger, level) << message;
14,864✔
159
}
7,433✔
160

161
void Logger::SetLevel(LogLevel level) {
632✔
162
        this->level_ = level;
632✔
163
}
632✔
164

165
LogLevel Logger::Level() {
1,185✔
166
        return this->level_;
1,185✔
167
}
168

169
void Logger::AddField(const LogField &field) {
178✔
170
        this->logger.add_attribute(field.key, attrs::constant<LogField>(field));
178✔
171
        return;
178✔
172
}
173

174
Logger Setup() {
386✔
175
        SetupLoggerSinks();
386✔
176
        SetupLoggerAttributes();
386✔
177
#ifdef NDEBUG
178
        return Logger("Global", LogLevel::Info);
179
#else
180
        return Logger("Global", LogLevel::Debug);
772✔
181
#endif
182
}
183

184
Logger global_logger_ = Setup();
185

186
void SetLevel(LogLevel level) {
455✔
187
        global_logger_.SetLevel(level);
455✔
188
}
455✔
189

190
error::Error SetupFileLogging(const string &log_file_path, bool exclusive) {
2✔
191
        typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
192
        boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
4✔
193

194
        // Add a stream to write log to
195
        auto log_stream = boost::make_shared<std::ofstream>();
4✔
196
        errno = 0;
2✔
197
        log_stream->open(log_file_path);
2✔
198
        if (!(*log_stream.get())) {
2✔
199
                auto io_errno = errno;
1✔
200
                return MakeError(
201
                        LogErrorCode::LogFileError,
202
                        "Failed to open '" + log_file_path + "' for logging: " + strerror(io_errno));
2✔
203
        }
204
        sink->set_formatter(&LogfmtFormatter);
1✔
205

206
        sink->locked_backend()->add_stream(log_stream);
1✔
207
        sink->locked_backend()->auto_flush(true);
1✔
208

209
        if (exclusive) {
1✔
210
                logging::core::get()->remove_all_sinks();
1✔
211
        }
212

213
        // Register the sink in the logging core
214
        logging::core::get()->add_sink(sink);
1✔
215

216
        return error::NoError;
1✔
217
}
218

219
LogLevel Level() {
7✔
220
        return global_logger_.Level();
7✔
221
}
222

223
void Log_(LogLevel level, const string message) {
×
224
        global_logger_.Log(level, message);
×
225
}
×
226

227
void Fatal(const string &message) {
×
228
        global_logger_.Log(LogLevel::Fatal, message);
×
229
        std::abort();
×
230
}
231
void Error(const string &message) {
61✔
232
        return global_logger_.Log(LogLevel::Error, message);
61✔
233
}
234
void Warning(const string &message) {
7✔
235
        return global_logger_.Log(LogLevel::Warning, message);
7✔
236
}
237
void Info(const string &message) {
74✔
238
        return global_logger_.Log(LogLevel::Info, message);
74✔
239
}
240
void Debug(const string &message) {
671✔
241
        return global_logger_.Log(LogLevel::Debug, message);
671✔
242
}
243
void Trace(const string &message) {
4,130✔
244
        return global_logger_.Log(LogLevel::Trace, message);
4,130✔
245
}
246

247

248
} // namespace log
249
} // namespace common
250
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc