00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef MECHSYS_EXCEPTION_H
00023 #define MECHSYS_EXCEPTION_H
00024
00025 #include <iostream>
00026 #include <cstdarg>
00027
00028 #include "util/string.h"
00029
00030 class Exception
00031 {
00032 public:
00033
00034 virtual ~Exception() {}
00035
00036 virtual void Cout () const =0;
00037 virtual bool IsFatal () const =0;
00038 virtual bool IsWarning() const =0;
00039 String Msg () const { return _msg; }
00040 protected:
00041
00042 String _msg;
00043 };
00044
00045 class Message : public Exception
00046 {
00047 public:
00048
00049 Message(String const & Fmt, ...);
00050
00051 void Cout () const { std::cout << "[1;32m" << "Message: " << _msg.GetSTL() << "[0m" << std::endl; }
00052 bool IsFatal () const { return false; }
00053 bool IsWarning() const { return false; }
00054 };
00055
00056 class Warning : public Exception
00057 {
00058 public:
00059
00060 Warning(String const & Fmt, ...);
00061
00062 void Cout () const { std::cout << "[1;33m" << "Warning: " << _msg.GetSTL() << "[0m" << std::endl; }
00063 bool IsFatal () const { return false; }
00064 bool IsWarning() const { return true; }
00065 };
00066
00067 class Fatal : public Exception
00068 {
00069 public:
00070
00071 Fatal(String const & Fmt, ...);
00072
00073 void Cout () const { std::cout << "[1;31m" << "Fatal: " << _msg.GetSTL() << "[0m" << std::endl; }
00074 bool IsFatal () const { return true; }
00075 bool IsWarning() const { return false; }
00076 };
00077
00078
00080
00081
00082 inline Message::Message(String const & Fmt, ...)
00083 {
00084 va_list arg_list;
00085 va_start (arg_list, Fmt);
00086 _msg.PrintfV (Fmt, arg_list);
00087 va_end (arg_list);
00088 }
00089
00090 inline Warning::Warning(String const & Fmt, ...)
00091 {
00092 va_list arg_list;
00093 va_start (arg_list, Fmt);
00094 _msg.PrintfV (Fmt, arg_list);
00095 va_end (arg_list);
00096 }
00097
00098 inline Fatal::Fatal(String const & Fmt, ...)
00099 {
00100 va_list arg_list;
00101 va_start (arg_list, Fmt);
00102 _msg.PrintfV (Fmt, arg_list);
00103 va_end (arg_list);
00104 }
00105
00106 #endif // MECHSYS_EXCEPTION_H
00107
00108