message_exception.h 3.44 KB
Newer Older
1 2
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7 8
#ifndef MESSAGE_EXCEPTION_H
#define MESSAGE_EXCEPTION_H
9

10 11 12
#include <exceptions/exceptions.h>
#include <log/message_types.h>

13 14 15 16
#include <stdexcept>
#include <string>
#include <vector>

Stephen Morris's avatar
Stephen Morris committed
17
#include <boost/lexical_cast.hpp>
18 19 20 21 22 23 24 25 26 27

namespace isc {
namespace log {

/// \brief Message Exception
///
/// Used in the message reader, this simple exception class allows a message
/// code and its arguments to be encapsulated in an exception and thrown
/// up the stack.

28
class MessageException : public isc::Exception {
29 30 31 32
public:

    /// \brief Constructor
    ///
33 34 35
    /// \param file Filename where the exception occurred.
    /// \param line Line where exception occurred.
    /// \param what Text description of the problem.
Stephen Morris's avatar
Stephen Morris committed
36 37
    /// \param id Message identification.
    /// \param lineno Line number on which error occurred (if > 0).
38 39 40
    MessageException(const char* file, size_t line, const char* what,
                     MessageID id, int lineno)
        : isc::Exception(file, line, what), id_(id), lineno_(lineno)
Stephen Morris's avatar
Stephen Morris committed
41
    {
42
        if (lineno_ > 0) {
Stephen Morris's avatar
Stephen Morris committed
43 44 45
            args_.push_back(boost::lexical_cast<std::string>(lineno));
        }
    }
46 47 48

    /// \brief Constructor
    ///
49 50 51
    /// \param file Filename where the exception occurred.
    /// \param line Line where exception occurred.
    /// \param what Text description of the problem.
Stephen Morris's avatar
Stephen Morris committed
52 53 54
    /// \param id Message identification.
    /// \param arg1 First message argument.
    /// \param lineno Line number on which error occurred (if > 0).
55 56
    MessageException(const char* file, size_t line, const char* what,
                     MessageID id, const std::string& arg1, int lineno)
57
        : isc::Exception(file, line, what), id_(id), lineno_(lineno)
58
    {
Stephen Morris's avatar
Stephen Morris committed
59 60 61
        if (lineno > 0) {
            args_.push_back(boost::lexical_cast<std::string>(lineno));
        }
62 63 64 65 66
        args_.push_back(arg1);
    }

    /// \brief Constructor
    ///
67 68 69
    /// \param file Filename where the exception occurred.
    /// \param line Line where exception occurred.
    /// \param what Text description of the problem.
Stephen Morris's avatar
Stephen Morris committed
70 71 72 73
    /// \param id Message identification.
    /// \param arg1 First message argument.
    /// \param arg2 Second message argument.
    /// \param lineno Line number on which error occurred (if > 0).
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
    {
Stephen Morris's avatar
Stephen Morris committed
79 80 81
        if (lineno > 0) {
            args_.push_back(boost::lexical_cast<std::string>(lineno));
        }
82 83 84 85 86
        args_.push_back(arg1);
        args_.push_back(arg2);
    }

    /// \brief Destructor
Stephen Morris's avatar
Stephen Morris committed
87
    ~MessageException() throw() {}
88 89 90 91 92 93 94 95 96 97 98 99

    /// \brief Return Message ID
    ///
    /// \return Message identification
    MessageID id() const {
        return id_;
    }

    /// \brief Return Arguments
    ///
    /// \return Exception Arguments
    std::vector<std::string> arguments() const {
100
        return (args_);
101 102 103 104 105
    }

private:
    MessageID                   id_;        // Exception ID
    std::vector<std::string>    args_;      // Exception arguments
106
    int lineno_;
107 108 109 110 111
};

} // namespace log
} // namespace isc

112
#endif // MESSAGE_EXCEPTION_H