vector<string> args(e.arguments());
for (size_t i(0); i < args.size(); ++ i) {
try {
- replacePlaceholder(&text, args[i], i + 1);
+ replacePlaceholder(text, args[i], i + 1);
} catch (...) {
// Error in error handling: nothing right to do...
}
namespace log {
void
-replacePlaceholder(string* message, const string& arg,
- const unsigned placeholder)
-{
+replacePlaceholder(std::string& message, const string& arg,
+ const unsigned placeholder) {
const string mark("%" + lexical_cast<string>(placeholder));
- size_t pos(message->find(mark));
+ size_t pos(message.find(mark));
if (pos != string::npos) {
do {
- message->replace(pos, mark.size(), arg);
- pos = message->find(mark, pos + arg.size());
+ message.replace(pos, mark.size(), arg);
+ pos = message.find(mark, pos + arg.size());
} while (pos != string::npos);
}
#ifdef ENABLE_LOGGER_CHECKS
else {
// We're missing the placeholder, so throw an exception
isc_throw(MismatchedPlaceholders,
- "Missing logger placeholder in message: " << *message);
+ "Missing logger placeholder in message: " << message);
}
#else
else {
// We're missing the placeholder, so add some complain
- message->append(" @@Missing placeholder " + mark + " for '" + arg + "'@@");
+ message.append(" @@Missing placeholder " + mark + " for '" + arg + "'@@");
}
#endif /* ENABLE_LOGGER_CHECKS */
}
void
-checkExcessPlaceholders(string* message, unsigned int placeholder) {
+checkExcessPlaceholders(std::string& message,
+ unsigned int placeholder) {
const string mark("%" + lexical_cast<string>(placeholder));
- const size_t pos(message->find(mark));
+ 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,
#ifdef ENABLE_LOGGER_CHECKS
// Also, make sure we print the message so we can identify which
// identifier has the problem.
- cerr << "Message " << *message << endl;
+ cerr << "Message " << message << endl;
assert("Excess logger placeholders still exist in message" == NULL);
#else
- message->append(" @@Excess logger placeholders still exist@@");
+ message.append(" @@Excess logger placeholders still exist@@");
#endif /* ENABLE_LOGGER_CHECKS */
}
}
-}
-}
+} // namespace log
+} // namespace isc
#include <iostream>
#include <exceptions/exceptions.h>
-#include <boost/lexical_cast.hpp>
#include <log/logger_level.h>
+#include <boost/make_shared.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/lexical_cast.hpp>
+
namespace isc {
namespace log {
/// This is used internally by the Formatter to check for excess
/// placeholders (and fewer arguments).
void
-checkExcessPlaceholders(std::string* message, unsigned int placeholder);
+checkExcessPlaceholders(std::string& message, unsigned int placeholder);
///
/// \brief The internal replacement routine
/// in the message by replacement. If the placeholder is not found,
/// it adds a complain at the end.
void
-replacePlaceholder(std::string* message, const std::string& replacement,
+replacePlaceholder(std::string& message, const std::string& replacement,
const unsigned placeholder);
///
Severity severity_;
/// \brief The messages with %1, %2... placeholders
- std::string* message_;
+ boost::shared_ptr<std::string> message_;
/// \brief Which will be the next placeholder to replace
unsigned nextPlaceholder_;
/// logger is also NULL, but it's not checked.
/// \param logger The logger where the final output will go, or NULL
/// if no output is wanted.
- Formatter(const Severity& severity = NONE, std::string* message = NULL,
+ Formatter(const Severity& severity = NONE,
+ boost::shared_ptr<std::string> message = boost::make_shared<std::string>(),
Logger* logger = NULL) :
logger_(logger), severity_(severity), message_(message),
- nextPlaceholder_(0)
- {
+ nextPlaceholder_(0) {
}
/// \brief Copy constructor
/// object being copied relinquishes that responsibility.
Formatter(const Formatter& other) :
logger_(other.logger_), severity_(other.severity_),
- message_(other.message_), nextPlaceholder_(other.nextPlaceholder_)
- {
+ message_(other.message_), nextPlaceholder_(other.nextPlaceholder_) {
other.logger_ = NULL;
}
/// \brief Destructor.
//
/// This is the place where output happens if the formatter is active.
- ~ Formatter() {
+ ~Formatter() {
if (logger_) {
try {
- checkExcessPlaceholders(message_, ++nextPlaceholder_);
+ checkExcessPlaceholders(*message_, ++nextPlaceholder_);
logger_->output(severity_, *message_);
} catch (...) {
// Catch and ignore all exceptions here.
}
- delete message_;
}
}
// .arg(42).arg("%1") would return "42 %1" - there are no recursive
// replacements).
try {
- replacePlaceholder(message_, arg, ++nextPlaceholder_ );
- }
- catch (...) {
+ replacePlaceholder(*message_, arg, ++nextPlaceholder_ );
+ } catch (...) {
// Something went wrong here, the log message is broken, so
// we don't want to output it, nor we want to check all the
// placeholders were used (because they won't be).
/// the arguments for the message.
void deactivate() {
if (logger_) {
- delete message_;
- message_ = NULL;
+ message_.reset();
logger_ = NULL;
}
}
};
-}
-}
+} // namespace log
+} // namespace isc
#endif
#include <config.h>
-#include <iostream>
-#include <iomanip>
-#include <algorithm>
+#include <algorithm>
+#include <cstring>
+#include <iomanip>
+#include <iostream>
#include <stdarg.h>
#include <stdio.h>
-#include <cstring>
#include <sstream>
+
+#include <boost/make_shared.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/static_assert.hpp>
#include <boost/algorithm/string.hpp>
// Output a general message
-string*
+boost::shared_ptr<string>
LoggerImpl::lookupMessage(const MessageID& ident) {
- return (new string(string(ident) + " " +
- MessageDictionary::globalDictionary()->getText(ident)));
+ return (boost::make_shared<string>(string(ident) + " " +
+ MessageDictionary::globalDictionary()->getText(ident)));
}
// Replace the interprocess synchronization object
void
-LoggerImpl::setInterprocessSync(isc::log::interprocess::InterprocessSync* sync)
-{
+LoggerImpl::setInterprocessSync(interprocess::InterprocessSync* sync) {
if (sync == NULL) {
isc_throw(BadInterprocessSync,
"NULL was passed to setInterprocessSync()");
/// \brief Look up message text in dictionary
///
/// This gets you the unformatted text of message for given ID.
- std::string* lookupMessage(const MessageID& id);
+ boost::shared_ptr<std::string> lookupMessage(const MessageID& id);
/// \brief Replace the interprocess synchronization object
///
outputs.push_back(Output(prefix, message));
}
// Just shortcut for new string
- string* s(const char* text) {
- return (new string(text));
+ boost::shared_ptr<string> s(const char* text) {
+ return (boost::make_shared<string>(text));
}
};