Kea  1.5.0
log_formatter.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2015 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(string* message, const string& arg,
24  const unsigned placeholder)
25 {
26  const string mark("%" + lexical_cast<string>(placeholder));
27  size_t pos(message->find(mark));
28  if (pos != string::npos) {
29  do {
30  message->replace(pos, mark.size(), arg);
31  pos = message->find(mark, pos + arg.size());
32  } while (pos != string::npos);
33  }
34 #ifdef ENABLE_LOGGER_CHECKS
35  else {
36  // We're missing the placeholder, so throw an exception
38  "Missing logger placeholder in message: " << *message);
39  }
40 #else
41  else {
42  // We're missing the placeholder, so add some complain
43  message->append(" @@Missing placeholder " + mark + " for '" + arg + "'@@");
44  }
45 #endif /* ENABLE_LOGGER_CHECKS */
46 }
47 
48 void
49 checkExcessPlaceholders(string* message, 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 }
69 }
isc::log::MismatchedPlaceholders
Mismatched Placeholders.
Definition: log_formatter.h:39
boost
Definition: io_service.h:12
isc::log::checkExcessPlaceholders
void checkExcessPlaceholders(string *message, unsigned int placeholder)
Internal excess placeholder checker.
Definition: log_formatter.cc:49
log_formatter.h
isc::log::replacePlaceholder
void replacePlaceholder(string *message, const string &arg, const unsigned placeholder)
The internal replacement routine.
Definition: log_formatter.cc:23
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192