Kea  1.9.9-git
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 
16 using namespace std;
17 using namespace boost;
18 
19 namespace isc {
20 namespace log {
21 
22 void
23 replacePlaceholder(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 
47 void
48 checkExcessPlaceholders(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
STL namespace.
void replacePlaceholder(std::string &message, const string &arg, const unsigned placeholder)
The internal replacement routine.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Mismatched Placeholders.
Definition: log_formatter.h:42
void checkExcessPlaceholders(std::string &message, unsigned int placeholder)
Internal excess placeholder checker.