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