Kea  1.9.9-git
message_dictionary.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <cstddef>
10 #include <log/message_dictionary.h>
11 #include <log/message_types.h>
12 
13 using namespace std;
14 
15 namespace isc {
16 namespace log {
17 
18 // Constructor
19 
20 MessageDictionary::MessageDictionary() : dictionary_(), empty_("") {
21 }
22 
23 // (Virtual) Destructor
24 
26 }
27 
28 // Add message and note if ID already exists
29 
30 bool
31 MessageDictionary::add(const std::string& ident, const std::string& text) {
32  Dictionary::iterator i = dictionary_.find(ident);
33  bool not_found = (i == dictionary_.end());
34  if (not_found) {
35 
36  // Message not already in the dictionary, so add it.
37  dictionary_[ident] = text;
38  }
39 
40  return (not_found);
41 }
42 
43 // Add message and note if ID does not already exist
44 
45 bool
46 MessageDictionary::replace(const std::string& ident, const std::string& text) {
47  Dictionary::iterator i = dictionary_.find(ident);
48  bool found = (i != dictionary_.end());
49  if (found) {
50 
51  // Exists, so replace it.
52  dictionary_[ident] = text;
53  }
54 
55  return (found);
56 }
57 
58 bool
59 MessageDictionary::erase(const std::string& ident, const std::string& text) {
60  Dictionary::iterator mes = dictionary_.find(ident);
61  // Both the ID and the text must match.
62  bool found = (mes != dictionary_.end() && (mes->second == text));
63  if (found) {
64  dictionary_.erase(mes);
65  }
66  return (found);
67 }
68 
69 // Load a set of messages
70 
71 vector<std::string>
72 MessageDictionary::load(const char* messages[]) {
73  vector<std::string> duplicates;
74  int i = 0;
75  while (messages[i]) {
76  // ID present, so point to text.
77  ++i;
78  if (messages[i]) {
79  const MessageID ident(messages[i - 1]);
80  // Text not null, note it and point to next ident.
81  const std::string text(messages[i]);
82  ++i;
83 
84  // Add ID and text to message dictionary, noting if the ID was
85  // already present.
86  bool added = add(ident, text);
87  if (!added) {
88  duplicates.push_back(boost::lexical_cast<string>(ident));
89  }
90  }
91  }
92  return (duplicates);
93 }
94 
95 // Return message text or blank string. A reference is returned to a string
96 // in the dictionary - this is fine, as the string is immediately used for
97 // output.
98 
99 const string&
100 MessageDictionary::getText(const std::string& ident) const {
101  Dictionary::const_iterator i = dictionary_.find(ident);
102  if (i == dictionary_.end()) {
103  return (empty_);
104  }
105  else {
106  return (i->second);
107  }
108 }
109 
110 // Return global dictionary
111 
114  static MessageDictionaryPtr global(new MessageDictionary());
115  return (global);
116 }
117 
118 
119 
120 
121 } // namespace log
122 } // namespace isc
static const MessageDictionaryPtr & globalDictionary()
Return Global Dictionary.
virtual bool erase(const std::string &ident, const std::string &text)
Removes the specified message from the dictionary.
STL namespace.
virtual const std::string & getText(const MessageID &ident) const
Get Message Text.
virtual ~MessageDictionary()
Virtual Destructor.
virtual bool replace(const MessageID &ident, const std::string &text)
Replace Message.
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< MessageDictionary > MessageDictionaryPtr
Shared pointer to the MessageDictionary.
virtual bool add(const MessageID &ident, const std::string &text)
Add Message.
virtual std::vector< std::string > load(const char *elements[])
Load Dictionary.
const char * MessageID
Definition: message_types.h:15