Kea 1.9.11
log_formatter.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2020 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 }
33#ifdef ENABLE_LOGGER_CHECKS
34 else {
35 // We're missing the placeholder, so throw an exception
37 "Missing logger placeholder in message: " << message);
38 }
39#else
40 else {
41 // We're missing the placeholder, so add some complain
42 message.append(" @@Missing placeholder " + mark + " for '" + arg + "'@@");
43 }
44#endif /* ENABLE_LOGGER_CHECKS */
45}
46
47void
48checkExcessPlaceholders(std::string& message,
49 unsigned int placeholder) {
50 const string mark("%" + lexical_cast<string>(placeholder));
51 const size_t pos(message.find(mark));
52 if (pos != string::npos) {
53 // Excess placeholders were found. If we enable the harsh check,
54 // abort it. Note: ideally we'd like to throw MismatchedPlaceholders,
55 // but we can't at least for now because this function is called from
56 // the Formatter's destructor.
57#ifdef ENABLE_LOGGER_CHECKS
58 // Also, make sure we print the message so we can identify which
59 // identifier has the problem.
60 cerr << "Message " << message << endl;
61 assert("Excess logger placeholders still exist in message" == NULL);
62#else
63 message.append(" @@Excess logger placeholders still exist@@");
64#endif /* ENABLE_LOGGER_CHECKS */
65 }
66}
67
68} // namespace log
69} // namespace isc
Mismatched Placeholders.
Definition: log_formatter.h:42
#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.