UDK 3.2.7 C/C++ API Reference

sal/log.hxx

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*
00003  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License or as specified alternatively below. You may obtain a copy of
00008  * the License at http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * Major Contributor(s):
00016  * Copyright (C) 2011 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
00017  *  (initial developer)
00018  *
00019  * All Rights Reserved.
00020  *
00021  * For minor contributions see the git repository.
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
00025  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
00026  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
00027  * instead of those above.
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 // Avoid the use of other sal code in this header as much as possible, so that
00044 // this code can be called from other sal code without causing endless
00045 // recursion.
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     // An alternative would be to have sal_detail_log take a std::ostringstream
00060     // pointer (via a C void pointer); the advantage would be smaller client
00061     // code (the ".str().c_str()" part would move into the implementation of
00062     // sal_detail_log) and potential for proper support of embedded null
00063     // characters within the message, but the disadvantage would be dependence
00064     // on the C++ ABI; as a compromise, the ".str().c_str()" part has been moved
00065     // to this inline function so that it is potentially only emitted once per
00066     // dynamic library:
00067     sal_detail_log(level, area, where, stream.str().c_str());
00068 }
00069 
00070 // Special handling of the common case where the message consists of just a
00071 // string literal, to produce smaller call-site code:
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 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines