log_formatter.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "config.h"
  15. #include <log/log_formatter.h>
  16. using namespace std;
  17. using namespace boost;
  18. namespace isc {
  19. namespace log {
  20. void
  21. replacePlaceholder(string* message, const string& arg,
  22. const unsigned placeholder)
  23. {
  24. string mark("%" + lexical_cast<string>(placeholder));
  25. size_t pos(message->find(mark));
  26. if (pos != string::npos) {
  27. do {
  28. message->replace(pos, mark.size(), arg);
  29. pos = message->find(mark, pos + arg.size());
  30. } while (pos != string::npos);
  31. }
  32. #ifdef ENABLE_LOGGER_CHECKS
  33. else {
  34. // We're missing the placeholder, so throw an exception
  35. isc_throw(MismatchedPlaceholders,
  36. "Missing logger placeholder in message: " << message);
  37. }
  38. #else
  39. else {
  40. // We're missing the placeholder, so add some complain
  41. message->append(" @@Missing placeholder " + mark + " for '" + arg + "'@@");
  42. }
  43. #endif /* ENABLE_LOGGER_CHECKS */
  44. }
  45. void
  46. checkExcessPlaceholders(string* message, unsigned int placeholder)
  47. {
  48. #ifdef ENABLE_LOGGER_CHECKS
  49. string mark("%" + lexical_cast<string>(placeholder));
  50. size_t pos(message->find(mark));
  51. if (pos != string::npos) {
  52. // Excess placeholders were found, so throw an exception
  53. isc_throw(MismatchedPlaceholders,
  54. "Excess logger placeholders still exist in message: " << message);
  55. }
  56. #endif /* ENABLE_LOGGER_CHECKS */
  57. }
  58. }
  59. }