Kea 2.7.6
log_formatter.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8#include <log/log_formatter.h>
9
10#include <cassert>
11
12#ifdef ENABLE_LOGGER_CHECKS
13#include <iostream>
14#endif
15
16using namespace std;
17using namespace boost;
18
19namespace isc {
20namespace log {
21
22void
23replacePlaceholder(std::string& message, const string& arg,
24 const unsigned placeholder) {
25 const string mark("%" + lexical_cast<string>(placeholder));
26 size_t pos(message.find(mark));
27 if (pos != string::npos) {
28 do {
29 message.replace(pos, mark.size(), arg);
30 pos = message.find(mark, pos + arg.size());
31 } while (pos != string::npos);
32 } else {
33#ifdef ENABLE_LOGGER_CHECKS
34 // We're missing the placeholder, so throw an exception
35 isc_throw(MismatchedPlaceholders, "Missing logger placeholder '" << mark << "' for value '"
36 << arg << "' in message '"
37 << message << "'");
38#else
39 // We're missing the placeholder, so add some complain
40 message.append(" @@Missing logger placeholder '" + mark + "' for value '" + arg + "'@@");
41#endif /* ENABLE_LOGGER_CHECKS */
42 }
43}
44
45void
46checkExcessPlaceholders(std::string& message,
47 unsigned int placeholder) {
48 const string mark("%" + lexical_cast<string>(placeholder));
49 const size_t pos(message.find(mark));
50 if (pos != string::npos) {
51 // Excess placeholders were found. If we enable the harsh check,
52 // abort it. Note: ideally we'd like to throw MismatchedPlaceholders,
53 // but we can't at least for now because this function is called from
54 // the Formatter's destructor.
55#ifdef ENABLE_LOGGER_CHECKS
56 // Also, make sure we print the message so we can identify which
57 // identifier has the problem.
58 cerr << "Excess logger placeholder '" << mark << "' still exists in message '" << message
59 << "'." << endl;
60 assert(false);
61#else
62 message.append(" @@Excess logger placeholder '" + mark + "' still exists@@");
63#endif /* ENABLE_LOGGER_CHECKS */
64 }
65}
66
67} // namespace log
68} // namespace isc
Mismatched Placeholders.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void checkExcessPlaceholders(std::string &message, unsigned int placeholder)
Internal excess placeholder checker.
void replacePlaceholder(std::string &message, const string &arg, const unsigned placeholder)
The internal replacement routine.
Defines the logger used by the top-level component of kea-lfc.