diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 11:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 11:36:04 +0000 |
commit | 040eee1aa49b49df4698d83a05af57c220127fd1 (patch) | |
tree | f635435954e6ccde5eee9893889e24f30ca68346 /src/lib/log/log_formatter.cc | |
parent | Initial commit. (diff) | |
download | isc-kea-040eee1aa49b49df4698d83a05af57c220127fd1.tar.xz isc-kea-040eee1aa49b49df4698d83a05af57c220127fd1.zip |
Adding upstream version 2.2.0.upstream/2.2.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/log/log_formatter.cc')
-rw-r--r-- | src/lib/log/log_formatter.cc | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/lib/log/log_formatter.cc b/src/lib/log/log_formatter.cc new file mode 100644 index 0000000..1ce33f4 --- /dev/null +++ b/src/lib/log/log_formatter.cc @@ -0,0 +1,68 @@ +// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include <config.h> +#include <log/log_formatter.h> + +#include <cassert> + +#ifdef ENABLE_LOGGER_CHECKS +#include <iostream> +#endif + +using namespace std; +using namespace boost; + +namespace isc { +namespace log { + +void +replacePlaceholder(std::string& message, const string& arg, + const unsigned placeholder) { + const string mark("%" + lexical_cast<string>(placeholder)); + size_t pos(message.find(mark)); + if (pos != string::npos) { + do { + message.replace(pos, mark.size(), arg); + pos = message.find(mark, pos + arg.size()); + } while (pos != string::npos); + } else { +#ifdef ENABLE_LOGGER_CHECKS + // We're missing the placeholder, so throw an exception + isc_throw(MismatchedPlaceholders, "Missing logger placeholder '" << mark << "' for value '" + << arg << "' in message '" + << message << "'"); +#else + // We're missing the placeholder, so add some complain + message.append(" @@Missing logger placeholder '" + mark + "' for value '" + arg + "'@@"); +#endif /* ENABLE_LOGGER_CHECKS */ + } +} + +void +checkExcessPlaceholders(std::string& message, + unsigned int placeholder) { + const string mark("%" + lexical_cast<string>(placeholder)); + const size_t pos(message.find(mark)); + if (pos != string::npos) { + // Excess placeholders were found. If we enable the harsh check, + // abort it. Note: ideally we'd like to throw MismatchedPlaceholders, + // but we can't at least for now because this function is called from + // the Formatter's destructor. +#ifdef ENABLE_LOGGER_CHECKS + // Also, make sure we print the message so we can identify which + // identifier has the problem. + cerr << "Excess logger placeholder '" << mark << "' still exists in message '" << message + << "'." << endl; + assert(false); +#else + message.append(" @@Excess logger placeholder '" + mark + "' still exists@@"); +#endif /* ENABLE_LOGGER_CHECKS */ + } +} + +} // namespace log +} // namespace isc |