1 // Copyright (C) 2011-2017 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 #ifndef MESSAGE_EXCEPTION_H
8 #define MESSAGE_EXCEPTION_H
9 
10 #include <exceptions/exceptions.h>
11 #include <log/message_types.h>
12 
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 
17 #include <boost/lexical_cast.hpp>
18 
19 namespace isc {
20 namespace log {
21 
22 /// \brief Message Exception
23 ///
24 /// Used in the message reader, this simple exception class allows a message
25 /// code and its arguments to be encapsulated in an exception and thrown
26 /// up the stack.
27 
28 class MessageException : public isc::Exception {
29 public:
30 
31     /// \brief Constructor
32     ///
33     /// \param file Filename where the exception occurred.
34     /// \param line Line where exception occurred.
35     /// \param what Text description of the problem.
36     /// \param id Message identification.
37     /// \param lineno Line number on which error occurred (if > 0).
MessageException(const char * file,size_t line,const char * what,MessageID id,int lineno)38     MessageException(const char* file, size_t line, const char* what,
39                      MessageID id, int lineno)
40         : isc::Exception(file, line, what), id_(id), lineno_(lineno)
41     {
42         if (lineno_ > 0) {
43             args_.push_back(boost::lexical_cast<std::string>(lineno));
44         }
45     }
46 
47     /// \brief Constructor
48     ///
49     /// \param file Filename where the exception occurred.
50     /// \param line Line where exception occurred.
51     /// \param what Text description of the problem.
52     /// \param id Message identification.
53     /// \param arg1 First message argument.
54     /// \param lineno Line number on which error occurred (if > 0).
MessageException(const char * file,size_t line,const char * what,MessageID id,const std::string & arg1,int lineno)55     MessageException(const char* file, size_t line, const char* what,
56                      MessageID id, const std::string& arg1, int lineno)
57         : isc::Exception(file, line, what), id_(id), lineno_(lineno)
58     {
59         if (lineno > 0) {
60             args_.push_back(boost::lexical_cast<std::string>(lineno));
61         }
62         args_.push_back(arg1);
63     }
64 
65     /// \brief Constructor
66     ///
67     /// \param file Filename where the exception occurred.
68     /// \param line Line where exception occurred.
69     /// \param what Text description of the problem.
70     /// \param id Message identification.
71     /// \param arg1 First message argument.
72     /// \param arg2 Second message argument.
73     /// \param lineno Line number on which error occurred (if > 0).
MessageException(const char * file,size_t line,const char * what,MessageID id,const std::string & arg1,const std::string & arg2,int lineno)74     MessageException(const char* file, size_t line, const char *what,
75                      MessageID id, const std::string& arg1,
76                      const std::string& arg2, int lineno)
77         : isc::Exception(file, line, what), id_(id), lineno_(lineno)
78     {
79         if (lineno > 0) {
80             args_.push_back(boost::lexical_cast<std::string>(lineno));
81         }
82         args_.push_back(arg1);
83         args_.push_back(arg2);
84     }
85 
86     /// \brief Destructor
~MessageException()87     ~MessageException() {}
88 
89     /// \brief Return Message ID
90     ///
91     /// \return Message identification
id()92     MessageID id() const {
93         return id_;
94     }
95 
96     /// \brief Return Arguments
97     ///
98     /// \return Exception Arguments
arguments()99     std::vector<std::string> arguments() const {
100         return (args_);
101     }
102 
103 private:
104     MessageID                   id_;        // Exception ID
105     std::vector<std::string>    args_;      // Exception arguments
106     int lineno_;
107 };
108 
109 } // namespace log
110 } // namespace isc
111 
112 #endif // MESSAGE_EXCEPTION_H
113