Kea 2.7.4
message_dictionary.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2022 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>
11#include <log/message_types.h>
12
13using namespace std;
14
15namespace isc {
16namespace log {
17
18// Constructor
19
20MessageDictionary::MessageDictionary() : dictionary_(), empty_("") {
21}
22
23// (Virtual) Destructor
24
27
28// Add message and note if ID already exists
29
30bool
31MessageDictionary::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
45bool
46MessageDictionary::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
58bool
59MessageDictionary::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
71vector<std::string>
72MessageDictionary::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
99const string&
100MessageDictionary::getText(const std::string& ident) const {
101 Dictionary::const_iterator i = dictionary_.find(ident);
102 if (i == dictionary_.end()) {
103 return (empty_);
104 } else {
105 return (i->second);
106 }
107}
108
109// Return global dictionary
110
113 static MessageDictionaryPtr global(new MessageDictionary());
114 return (global);
115}
116
117
118
119
120} // namespace log
121} // namespace isc
virtual bool replace(const MessageID &ident, const std::string &text)
Replace Message.
static const MessageDictionaryPtr & globalDictionary()
Return Global Dictionary.
virtual const std::string & getText(const MessageID &ident) const
Get Message Text.
virtual std::vector< std::string > load(const char *elements[])
Load Dictionary.
virtual bool erase(const std::string &ident, const std::string &text)
Removes the specified message from the dictionary.
virtual ~MessageDictionary()
Virtual Destructor.
virtual bool add(const MessageID &ident, const std::string &text)
Add Message.
boost::shared_ptr< MessageDictionary > MessageDictionaryPtr
Shared pointer to the MessageDictionary.
const char * MessageID
Defines the logger used by the top-level component of kea-lfc.