00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef INCLUDED_SAL_LOG_HXX
00031 #define INCLUDED_SAL_LOG_HXX
00032
00033 #include "sal/config.h"
00034
00035 #include <cstdlib>
00036 #include <sstream>
00037 #include <string>
00038
00039 #include "sal/detail/log.h"
00040 #include "sal/saldllapi.h"
00041 #include "sal/types.h"
00042
00043
00044
00045
00046
00048
00049 extern "C" SAL_DLLPUBLIC void SAL_CALL sal_detail_log(
00050 enum sal_detail_LogLevel level, char const * area, char const * where,
00051 char const * message);
00052
00053 namespace sal { namespace detail {
00054
00055 inline void SAL_CALL log(
00056 sal_detail_LogLevel level, char const * area, char const * where,
00057 std::ostringstream const & stream)
00058 {
00059
00060
00061
00062
00063
00064
00065
00066
00067 sal_detail_log(level, area, where, stream.str().c_str());
00068 }
00069
00070
00071
00072
00073 struct StreamStart {};
00074
00075 struct StreamString {
00076 StreamString(char const * s): string(s) {}
00077
00078 char const * string;
00079
00080 typedef char Result;
00081 };
00082
00083 struct StreamIgnore {
00084 typedef struct { char a[2]; } Result;
00085 };
00086
00087 inline StreamString operator <<(
00088 SAL_UNUSED_PARAMETER StreamStart const &, char const * s)
00089 {
00090 return StreamString(s);
00091 }
00092
00093 template< typename T > inline StreamIgnore operator <<(
00094 SAL_UNUSED_PARAMETER StreamStart const &, SAL_UNUSED_PARAMETER T const &)
00095 {
00096 std::abort();
00097 #if defined _MSC_VER && _MSC_VER < 1700
00098 return StreamIgnore();
00099 #endif
00100 }
00101
00102 template< typename T > inline StreamIgnore operator <<(
00103 SAL_UNUSED_PARAMETER StreamString const &, SAL_UNUSED_PARAMETER T const &)
00104 {
00105 std::abort();
00106 #if defined _MSC_VER && _MSC_VER < 1700
00107 return StreamIgnore();
00108 #endif
00109 }
00110
00111 template< typename T > inline StreamIgnore operator <<(
00112 SAL_UNUSED_PARAMETER StreamIgnore const &, SAL_UNUSED_PARAMETER T const &)
00113 {
00114 std::abort();
00115 #if defined _MSC_VER && _MSC_VER < 1700
00116 return StreamIgnore();
00117 #endif
00118 }
00119
00120 template< typename T > typename T::Result getResult(T const &);
00121
00122 inline char const * unwrapStream(StreamString const & s) { return s.string; }
00123
00124 inline char const * unwrapStream(SAL_UNUSED_PARAMETER StreamIgnore const &) {
00125 std::abort();
00126 #if defined _MSC_VER && _MSC_VER < 1700
00127 return 0;
00128 #endif
00129 }
00130
00131 } }
00132
00133 #define SAL_DETAIL_LOG_STREAM(condition, level, area, where, stream) \
00134 do { \
00135 if (condition) { \
00136 if (sizeof ::sal::detail::getResult( \
00137 ::sal::detail::StreamStart() << stream) == 1) \
00138 { \
00139 ::sal_detail_log( \
00140 (level), (area), (where), \
00141 ::sal::detail::unwrapStream( \
00142 ::sal::detail::StreamStart() << stream)); \
00143 } else { \
00144 ::std::ostringstream sal_detail_stream; \
00145 sal_detail_stream << stream; \
00146 ::sal::detail::log( \
00147 (level), (area), (where), sal_detail_stream); \
00148 } \
00149 } \
00150 } while (false)
00151
00153
00164 #define SAL_WHERE SAL_DETAIL_WHERE
00165
00180 #define SAL_STREAM(stream) \
00181 (dynamic_cast< ::std::ostringstream & >(::std::ostringstream() << stream). \
00182 str())
00183
00281 #define SAL_INFO(area, stream) \
00282 SAL_DETAIL_LOG_STREAM( \
00283 SAL_DETAIL_ENABLE_LOG_INFO, ::SAL_DETAIL_LOG_LEVEL_INFO, area, \
00284 SAL_WHERE, stream)
00285
00291 #define SAL_INFO_IF(condition, area, stream) \
00292 SAL_DETAIL_LOG_STREAM( \
00293 SAL_DETAIL_ENABLE_LOG_INFO && (condition), \
00294 ::SAL_DETAIL_LOG_LEVEL_INFO, area, SAL_WHERE, stream)
00295
00301 #define SAL_WARN(area, stream) \
00302 SAL_DETAIL_LOG_STREAM( \
00303 SAL_DETAIL_ENABLE_LOG_WARN, ::SAL_DETAIL_LOG_LEVEL_WARN, area, \
00304 SAL_WHERE, stream)
00305
00311 #define SAL_WARN_IF(condition, area, stream) \
00312 SAL_DETAIL_LOG_STREAM( \
00313 SAL_DETAIL_ENABLE_LOG_WARN && (condition), \
00314 ::SAL_DETAIL_LOG_LEVEL_WARN, area, SAL_WHERE, stream)
00315
00322 #define SAL_DEBUG(stream) \
00323 SAL_DETAIL_LOG_STREAM( \
00324 SAL_LOG_TRUE, ::SAL_DETAIL_LOG_LEVEL_DEBUG, 0, 0, stream)
00325
00326 #endif
00327
00328