message_dictionary.cc 2.9 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. // $Id$
  15. #include <cstddef>
  16. #include <log/message_dictionary.h>
  17. #include <log/message_types.h>
  18. using namespace std;
  19. namespace isc {
  20. namespace log {
  21. // (Virtual) Destructor
  22. MessageDictionary::~MessageDictionary() {
  23. }
  24. // Add message and note if ID already exists
  25. bool MessageDictionary::add(const MessageID& ident, const std::string& text)
  26. {
  27. map<MessageID, string>::iterator i = dictionary_.find(ident);
  28. bool not_found = (i == dictionary_.end());
  29. if (not_found) {
  30. // Message not already in the dictionary, so add it.
  31. dictionary_[ident] = text;
  32. }
  33. return (not_found);
  34. }
  35. // Add message and note if ID does not already exist
  36. bool MessageDictionary::replace(const MessageID& ident, const std::string& text)
  37. {
  38. map<MessageID, string>::iterator i = dictionary_.find(ident);
  39. bool found = (i != dictionary_.end());
  40. if (found) {
  41. // Exists, so replace it.
  42. dictionary_[ident] = text;
  43. }
  44. return (found);
  45. }
  46. // Load a set of messages
  47. vector<MessageID> MessageDictionary::load(const char* messages[]) {
  48. vector<MessageID> duplicates;
  49. int i = 0;
  50. while (messages[i]) {
  51. // ID present, so note it and point to text.
  52. 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(ident);
  61. }
  62. }
  63. }
  64. return duplicates;
  65. }
  66. // Return message text or blank string
  67. string MessageDictionary::getText(const MessageID& ident) const {
  68. map<MessageID, string>::const_iterator i = dictionary_.find(ident);
  69. if (i == dictionary_.end()) {
  70. return string("");
  71. }
  72. else {
  73. return i->second;
  74. }
  75. }
  76. // Return global dictionary
  77. MessageDictionary* MessageDictionary::globalDictionary() {
  78. static MessageDictionary* global = NULL;
  79. if (global == NULL) {
  80. global = new MessageDictionary();
  81. }
  82. return global;
  83. }
  84. } // namspace log
  85. } // namespace isc