message_exception.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef __MESSAGE_EXCEPTION_H
  15. #define __MESSAGE_EXCEPTION_H
  16. #include <stdexcept>
  17. #include <string>
  18. #include <vector>
  19. #include <boost/lexical_cast.hpp>
  20. #include <log/message_types.h>
  21. namespace isc {
  22. namespace log {
  23. /// \brief Message Exception
  24. ///
  25. /// Used in the message reader, this simple exception class allows a message
  26. /// code and its arguments to be encapsulated in an exception and thrown
  27. /// up the stack.
  28. class MessageException : public std::exception {
  29. public:
  30. /// \brief Constructor
  31. ///
  32. /// \param id Message identification.
  33. /// \param lineno Line number on which error occurred (if > 0).
  34. MessageException(MessageID id, int lineno = 0) : id_(id)
  35. {
  36. if (lineno > 0) {
  37. args_.push_back(boost::lexical_cast<std::string>(lineno));
  38. }
  39. }
  40. /// \brief Constructor
  41. ///
  42. /// \param id Message identification.
  43. /// \param arg1 First message argument.
  44. /// \param lineno Line number on which error occurred (if > 0).
  45. MessageException(MessageID id, const std::string& arg1, int lineno = 0)
  46. : id_(id)
  47. {
  48. if (lineno > 0) {
  49. args_.push_back(boost::lexical_cast<std::string>(lineno));
  50. }
  51. args_.push_back(arg1);
  52. }
  53. /// \brief Constructor
  54. ///
  55. /// \param id Message identification.
  56. /// \param arg1 First message argument.
  57. /// \param arg2 Second message argument.
  58. /// \param lineno Line number on which error occurred (if > 0).
  59. MessageException(MessageID id, const std::string& arg1,
  60. const std::string& arg2, int lineno = 0) : id_(id)
  61. {
  62. if (lineno > 0) {
  63. args_.push_back(boost::lexical_cast<std::string>(lineno));
  64. }
  65. args_.push_back(arg1);
  66. args_.push_back(arg2);
  67. }
  68. /// \brief Destructor
  69. ~MessageException() throw() {}
  70. /// \brief Return Message ID
  71. ///
  72. /// \return Message identification
  73. MessageID id() const {
  74. return id_;
  75. }
  76. /// \brief Return Arguments
  77. ///
  78. /// \return Exception Arguments
  79. std::vector<std::string> arguments() const {
  80. return (args_);
  81. }
  82. private:
  83. MessageID id_; // Exception ID
  84. std::vector<std::string> args_; // Exception arguments
  85. };
  86. } // namespace log
  87. } // namespace isc
  88. #endif // __MESSAGE_EXCEPTION_H