MACE  1.0.0
 All Classes Namespaces Files Functions Variables Enumerations Defines
libs/cmt/include/mace/cmt/log/log.hpp
Go to the documentation of this file.
00001 
00005 #ifndef _BOOST_RPC_LOG_HPP_
00006 #define _BOOST_RPC_LOG_HPP_
00007 #include <boost/format.hpp>
00008 #include <stdint.h>
00009 #include <iostream>
00010 #include <iomanip>
00011 #include <boost/exception/diagnostic_information.hpp>
00012 
00013 #ifndef __func__
00014 #define __func__ __FUNCTION__
00015 #endif
00016 
00017 #ifndef WIN32
00018 #define  COLOR_CONSOLE 1
00019 #endif
00020 #include <mace/cmt/log/console_defines.h>
00021 #include <boost/thread/mutex.hpp>
00022 
00023 namespace mace { namespace cmt { 
00024     const char* thread_name();
00025 
00026     namespace detail {
00027     inline boost::mutex& log_mutex() {
00028       static boost::mutex m; return m;
00029     }
00030     inline std::string short_name( const std::string& file_name ) { return file_name.substr( file_name.rfind( '/' ) + 1 ); }
00031 
00032     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const char* text ) {
00033         boost::unique_lock<boost::mutex> lock(log_mutex());
00034         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<<text<< CONSOLE_DEFAULT << std::endl; 
00035     }
00036     template<typename P1>
00037     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const std::string& format, const P1& p1 ) {
00038         boost::unique_lock<boost::mutex> lock(log_mutex());
00039         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<< (boost::format(format) %p1) << CONSOLE_DEFAULT << std::endl;
00040     }
00041     template<typename P1, typename P2>
00042     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const std::string& format, const P1& p1, const P2& p2 ) {
00043         boost::unique_lock<boost::mutex> lock(log_mutex());
00044         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<< boost::format(format) %p1 %p2 << CONSOLE_DEFAULT << std::endl;
00045     }
00046     template<typename P1, typename P2, typename P3>
00047     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const std::string& format, const P1& p1, const P2& p2, const P3& p3 ) {
00048         boost::unique_lock<boost::mutex> lock(log_mutex());
00049         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<< (boost::format(format) %p1 %p2 %p3) << CONSOLE_DEFAULT << std::endl;
00050     }
00051     template<typename P1, typename P2, typename P3, typename P4>
00052     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const std::string& format, const P1& p1, const P2& p2, const P3& p3, const P4& p4 ) {
00053         boost::unique_lock<boost::mutex> lock(log_mutex());
00054         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<< (boost::format(format) %p1 %p2 %p3 %p4) << CONSOLE_DEFAULT << std::endl;
00055     }
00056     template<typename P1, typename P2, typename P3, typename P4, typename P5>
00057     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const std::string& format, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5 ) {
00058         boost::unique_lock<boost::mutex> lock(log_mutex());
00059         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<< (boost::format(format) %p1 %p2 %p3 %p4 %p5) << CONSOLE_DEFAULT << std::endl;
00060     }
00061     template<typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
00062     inline void log( std::ostream& os, const char* color, const char* file, uint32_t line, const char* method, const std::string& format, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6 ) {
00063         boost::unique_lock<boost::mutex> lock(log_mutex());
00064         os<<color<<std::left<<std::setw(10)<<thread_name()<<" "<<short_name(file)<<":"<<line<<" "<<method<<"] "<< (boost::format(format) %p1 %p2 %p3 %p4 %p5 %p6) << CONSOLE_DEFAULT << std::endl;
00065     }
00066 
00067 } } } // mace::cmt::detail
00071 #define dlog(...) do {try { mace::cmt::detail::log( std::cerr, CONSOLE_DEFAULT, __FILE__, __LINE__, __func__, __VA_ARGS__ ); } \
00072                 catch (boost::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00073                 catch (std::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00074                 catch (...) {mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs - exception while formating args" ); }  \
00075                 }while(false)
00076 
00080 #define slog(...) do {try {mace::cmt::detail::log( std::cerr, CONSOLE_DEFAULT, __FILE__, __LINE__, __func__, __VA_ARGS__ ); }\
00081                 catch (boost::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00082                 catch (std::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00083                 catch (...) {mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs - exception while formating args" ); }  \
00084                 }while(false)
00085 
00089 #define elog(...) do {try {mace::cmt::detail::log( std::cerr, CONSOLE_RED,     __FILE__, __LINE__, __func__, __VA_ARGS__ ); }\
00090                 catch (boost::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00091                 catch (std::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00092                 catch (...) {mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs - exception while formating args" ); }  \
00093                 }while(false)
00094 
00098 #define wlog(...) do {try {mace::cmt::detail::log( std::cerr, CONSOLE_BROWN,   __FILE__, __LINE__, __func__, __VA_ARGS__ ); }\
00099                 catch (boost::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00100                 catch (std::exception&e){ mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs: %1%", boost::diagnostic_information(e) ); }  \
00101                 catch (...) {mace::cmt::detail::log( std::cerr, CONSOLE_RED, __FILE__, __LINE__, __func__, "Invalid logs - exception while formating args" ); }  \
00102                 }while(false)
00103 
00104 #endif