message_dictionary.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <cstddef>
  15. #include <log/message_dictionary.h>
  16. #include <log/message_types.h>
  17. using namespace std;
  18. namespace isc {
  19. namespace log {
  20. // (Virtual) Destructor
  21. MessageDictionary::~MessageDictionary() {
  22. }
  23. // Add message and note if ID already exists
  24. bool
  25. MessageDictionary::add(const std::string& ident, const std::string& text) {
  26. Dictionary::iterator i = dictionary_.find(ident);
  27. bool not_found = (i == dictionary_.end());
  28. if (not_found) {
  29. // Message not already in the dictionary, so add it.
  30. dictionary_[ident] = text;
  31. }
  32. return (not_found);
  33. }
  34. // Add message and note if ID does not already exist
  35. bool
  36. MessageDictionary::replace(const std::string& ident, const std::string& text) {
  37. Dictionary::iterator i = dictionary_.find(ident);
  38. bool found = (i != dictionary_.end());
  39. if (found) {
  40. // Exists, so replace it.
  41. dictionary_[ident] = text;
  42. }
  43. return (found);
  44. }
  45. // Load a set of messages
  46. vector<std::string>
  47. MessageDictionary::load(const char* messages[]) {
  48. vector<std::string> duplicates;
  49. int i = 0;
  50. while (messages[i]) {
  51. // ID present, so note it and point to text.
  52. const MessageID ident(messages[i++]);
  53. if (messages[i]) {
  54. // Text not null, note it and point to next ident.
  55. string text(messages[i++]);
  56. // Add ID and text to message dictionary, noting if the ID was
  57. // already present.
  58. bool added = add(ident, text);
  59. if (!added) {
  60. duplicates.push_back(boost::lexical_cast<string>(ident));
  61. }
  62. }
  63. }
  64. return (duplicates);
  65. }
  66. // Return message text or blank string. A reference is returned to a string
  67. // in the dictionary - this is fine, as the string is immediately used for
  68. // output.
  69. const string&
  70. MessageDictionary::getText(const std::string& ident) const {
  71. static const string empty("");
  72. Dictionary::const_iterator i = dictionary_.find(ident);
  73. if (i == dictionary_.end()) {
  74. return (empty);
  75. }
  76. else {
  77. return (i->second);
  78. }
  79. }
  80. // Return global dictionary
  81. MessageDictionary&
  82. MessageDictionary::globalDictionary() {
  83. static MessageDictionary global;
  84. return (global);
  85. }
  86. } // namespace log
  87. } // namespace isc