torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MessageTable.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef HAVE_CONFIG_H
17 #include "torc/generic/config.h"
18 #endif
19 
20 #include <cstdio>
21 
23 #include "torc/generic/Message.hpp"
24 
25 namespace torc {
26 namespace generic {
27 
28 /**
29  * Get the message string corresponding to the message inId. This is constructed using the actual
30  * message string and the currently set severity of this message.
31  *
32  * @param[in] inId MessageId object to denote the message.
33  */
35  MessageContainer::const_iterator msg = mMessages.find(inId);
36  if(msg == mMessages.end()) {
37  return std::string();
38  }
39  const Message message((*msg).second);
40  char msgString[BUFSIZ];
41  int nbytes = 0;
42  switch(message.getSeverity()) {
44  nbytes = snprintf(msgString, BUFSIZ, "SUPPRESSED ");
45  break;
46  }
47  case eMessageSeverityInfo: {
48  nbytes = snprintf(msgString, BUFSIZ, "INFO ");
49  break;
50  }
51  case eMessageSeverityError: {
52  nbytes = snprintf(msgString, BUFSIZ, "ERROR ");
53  break;
54  }
56  nbytes = snprintf(msgString, BUFSIZ, "WARNING ");
57  break;
58  }
59  }
60  const std::string& actualMsg = message.getMessage();
61  nbytes = snprintf(msgString + nbytes, BUFSIZ - nbytes, "%d : %s", inId, actualMsg.c_str());
62  if(nbytes >= BUFSIZ) {
63  return std::string();
64  }
65  return msgString;
66 }
67 
68 /**
69  * Change the message string for a given Id.
70  *
71  * @param[in] inId MessageId that needs to be changed
72  * @param[in] inMessage New message that needs to be set.
73  */
74 void MessageTable::changeMessageString(MessageId inId, const Message& inMessage) throw (Error) {
75  mMessages[inId] = inMessage;
76 }
77 
78 /**
79  * Change the message severity for a given Id.
80  *
81  * @param[in] inId MessageId that needs to be changed
82  * @param[in] inSeverity New severity that needs to be set.
83  */
85  MessageContainer::const_iterator msg = mMessages.find(inId);
86  if(msg != mMessages.end()) {
87  mMessages[inId].setSeverity(inSeverity);
88  }
89 }
90 
91 /**
92  * Get a pointer to the singleton MessageTable object.
93  */
95  static MessageTable singletonObject;
96  return &singletonObject;
97 }
98 
99 MessageTable::MessageTable() : mMessages() {
100  //Populate message table
102  "Array index size does not match array dimensions", eMessageSeverityError);
103 
104  mMessages[eMessageIdErrorArrayIndexOutOfBounds] = Message("Array index out of bounds",
106 
108 
109  mMessages[eMessageIdErrorNullChildfactory] = Message("Null child factory",
111 
113 
114  mMessages[eMessageIdErrorItemAlreadyExists] = Message("Item already exists",
116 
118 
119  mMessages[eMessageIdErrorPointerToItemDoesNotExist] = Message("Pointer to item does not exist",
121 
122  mMessages[eMessageIdErrorItemSizeMismatch] = Message("Item size does not match",
124 
125  mMessages[eMessageIdErrorConnectionInvalid] = Message("Provided connection is invalid",
127 
128  mMessages[eMessageIdErrorTypeCast] = Message("Type cast not possible", eMessageSeverityError);
129 
131 
132  mMessages[eMessageIdErrorCompositionTypeMismatch] = Message("Composition type mismatch",
135 
138  mMessages[eMessageIdErrorUnsupoortedOperation] = Message("Operation is unsupported on class",
140 }
141 
143 
144 } // namespace generic
145 } // namespace torc
MessageContainer mMessages
static MessageTable * instance()
void changeMessageSeverity(MessageId inId, MessageSeverity inSeverity)
const MessageSeverity getSeverity() const
Definition: Message.hpp:58
std::string string
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
void changeMessageString(MessageId inId, const Message &inMessage)
const std::string getMessage() const
Definition: Message.hpp:54
std::string getMessage(MessageId inId) const