lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
|
00001 /*****************************************************/ 00002 /* lean Logging (c) Tobias Zirr 2011 */ 00003 /*****************************************************/ 00004 00005 #ifndef LEAN_LOGGING_EXCEPTIONS 00006 #define LEAN_LOGGING_EXCEPTIONS 00007 00008 #include "../lean.h" 00009 #include "../strings/types.h" 00010 #include "../strings/conversions.h" 00011 #include <sstream> 00012 #include "streamconv.h" 00013 00014 namespace lean 00015 { 00016 namespace logging 00017 { 00018 00020 LEAN_MAYBE_EXPORT void throw_error(const char *source); 00022 LEAN_MAYBE_EXPORT void throw_error(const char *source, const char *reason); 00024 LEAN_MAYBE_EXPORT void throw_error(const char *source, const char *reason, const char *context); 00026 LEAN_MAYBE_EXPORT void throw_error_ex(const char *source, const char *reason, const char *origin); 00028 LEAN_MAYBE_EXPORT void throw_error_ex(const char *source, const char *reason, const char *origin, const char *context); 00029 00031 LEAN_MAYBE_EXPORT void throw_invalid(const char *source); 00033 LEAN_MAYBE_EXPORT void throw_invalid(const char *source, const char *reason); 00034 00036 LEAN_MAYBE_EXPORT void throw_bad_alloc(const char *source); 00038 LEAN_MAYBE_EXPORT void throw_bad_alloc(const char *source, size_t size); 00039 00041 LEAN_MAYBE_EXPORT void log_error(const char *source); 00043 LEAN_MAYBE_EXPORT void log_error(const char *source, const char *reason); 00045 LEAN_MAYBE_EXPORT void log_error(const char *source, const char *reason, const char *context); 00047 LEAN_MAYBE_EXPORT void log_error_ex(const char *source, const char *reason, const char *origin); 00049 LEAN_MAYBE_EXPORT void log_error_ex(const char *source, const char *reason, const char *origin, const char *context); 00050 00051 00053 template <class String1> 00054 inline void throw_error(const String1 &source) 00055 { 00056 throw_error(utf_to_utf8(source).c_str()); 00057 } 00059 template <class String1, class String2> 00060 inline void throw_error(const String1 &source, const String2 &reason) 00061 { 00062 throw_error(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str()); 00063 } 00065 template <class String1, class String2, class String3> 00066 inline void throw_error(const String1 &source, const String2 &reason, const String3 &context) 00067 { 00068 throw_error(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str(), utf_to_utf8(context).c_str()); 00069 } 00071 template <class String1, class String2, class String3> 00072 inline void throw_error_ex(const String1 &source, const String2 &reason, const String3 &origin) 00073 { 00074 throw_error_ex(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str(), utf_to_utf8(origin).c_str()); 00075 } 00077 template <class String1, class String2, class String3, class String4> 00078 inline void throw_error_ex(const String1 &source, const String2 &reason, const String3 &origin, const String4 &context) 00079 { 00080 throw_error_ex(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str(), utf_to_utf8(origin).c_str(), utf_to_utf8(context).c_str()); 00081 } 00082 00084 template <class String1> 00085 inline void throw_invalid(const String1 &source) 00086 { 00087 throw_invalid(utf_to_utf8(source).c_str()); 00088 } 00090 template <class String1, class String2> 00091 inline void throw_invalid(const String1 &source, const String2 &reason) 00092 { 00093 throw_invalid(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str()); 00094 } 00095 00096 00098 template <class String1> 00099 inline void log_error(const String1 &source) 00100 { 00101 log_error(utf_to_utf8(source).c_str()); 00102 } 00104 template <class String1, class String2> 00105 inline void log_error(const String1 &source, const String2 &reason) 00106 { 00107 log_error(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str()); 00108 } 00110 template <class String1, class String2, class String3> 00111 inline void log_error(const String1 &source, const String2 &reason, const String3 &context) 00112 { 00113 log_error(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str(), utf_to_utf8(context).c_str()); 00114 } 00116 template <class String1, class String2, class String3> 00117 inline void log_error_ex(const String1 &source, const String2 &reason, const String3 &origin) 00118 { 00119 log_error_ex(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str(), utf_to_utf8(origin).c_str()); 00120 } 00122 template <class String1, class String2, class String3, class String4> 00123 inline void log_error_ex(const String1 &source, const String2 &reason, const String3 &origin, const String4 &context) 00124 { 00125 log_error_ex(utf_to_utf8(source).c_str(), utf_to_utf8(reason).c_str(), utf_to_utf8(origin).c_str(), utf_to_utf8(context).c_str()); 00126 } 00127 00128 } // namespace 00129 00130 using logging::throw_error; 00131 using logging::throw_error_ex; 00132 using logging::throw_invalid; 00133 using logging::throw_bad_alloc; 00134 00135 using logging::log_error; 00136 using logging::log_error_ex; 00137 00138 } // namespace 00139 00143 00145 #define LEAN_THROW_ERROR() ::lean::logging::throw_error(LEAN_SOURCE_STRING) 00146 00147 #define LEAN_THROW_ERROR_MSG(msg) ::lean::logging::throw_error(LEAN_SOURCE_STRING, msg) 00148 00149 #define LEAN_THROW_ERROR_CTX(msg, ctx) ::lean::logging::throw_error(LEAN_SOURCE_STRING, msg, ctx) 00150 00151 #define LEAN_THROW_ERROR_XMSG(msg, orig) ::lean::logging::throw_error_ex(LEAN_SOURCE_STRING, msg, orig) 00152 00153 #define LEAN_THROW_ERROR_XCTX(msg, orig, ctx) ::lean::logging::throw_error_ex(LEAN_SOURCE_STRING, msg, orig, ctx) 00154 00155 #define LEAN_THROW_ERROR_ANY(msg) LEAN_THROW_ERROR_MSG(static_cast<::std::ostringstream&>(::std::ostringstream() << msg).str().c_str()) 00156 00158 #define LEAN_THROW_INVALID() ::lean::logging::throw_invalid(LEAN_SOURCE_STRING) 00159 00160 #define LEAN_THROW_INVALID_MSG(msg) ::lean::logging::throw_invalid(LEAN_SOURCE_STRING, msg) 00161 00162 #define LEAN_THROW_INVALID_ANY(msg) LEAN_THROW_INVALID_MSG(static_cast<::std::ostringstream&>(::std::ostringstream() << msg).str().c_str()) 00163 00165 #define LEAN_THROW_BAD_ALLOC() ::lean::logging::throw_bad_alloc(LEAN_SOURCE_STRING) 00166 00167 #define LEAN_THROW_BAD_ALLOC_SIZE(size) ::lean::logging::throw_bad_alloc(LEAN_SOURCE_STRING, size) 00168 00170 00171 00175 00177 #define LEAN_LOG_ERROR_NIL() ::lean::logging::log_error(LEAN_SOURCE_STRING) 00178 00179 #define LEAN_LOG_ERROR_MSG(msg) ::lean::logging::log_error(LEAN_SOURCE_STRING, msg) 00180 00181 #define LEAN_LOG_ERROR_CTX(msg, ctx) ::lean::logging::log_error(LEAN_SOURCE_STRING, msg, ctx) 00182 00183 #define LEAN_LOG_ERROR_XMSG(msg, orig) ::lean::logging::log_error_ex(LEAN_SOURCE_STRING, msg, orig) 00184 00185 #define LEAN_LOG_ERROR_XCTX(msg, orig, ctx) ::lean::logging::log_error_ex(LEAN_SOURCE_STRING, msg, orig, ctx) 00186 00188 00189 #ifdef LEAN_INCLUDE_LINKED 00190 #include "source/errors.cpp" 00191 #endif 00192 00193 #endif