]> git.sesse.net Git - casparcg/blobdiff - common/log.h
[framerate_producer] Renamed blend to blend2 and blend_all to blend3 to better descri...
[casparcg] / common / log.h
index 859da6bf299cc2df10259827723b027d895ade94..4cc983e4556f0b787a1c10df62f7bc28a17f352c 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#pragma once\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4100)\r
-#pragma warning (disable : 4127) // conditional expression is constant\r
-#pragma warning (disable : 4512)\r
-#pragma warning (disable : 4714) // marked as __forceinline not inlined\r
-#pragma warning (disable : 4996) // _CRT_SECURE_NO_WARNINGS\r
-#endif\r
-\r
-#include <boost/log/detail/prologue.hpp>\r
-#include <boost/log/keywords/severity.hpp>\r
-#include <boost/log/sources/global_logger_storage.hpp>\r
-#include <boost/log/sources/severity_logger.hpp>\r
-#include <boost/log/sources/record_ostream.hpp>\r
-\r
-#include <string>\r
-#include <locale>\r
-\r
-namespace caspar { namespace log {\r
-       \r
-namespace internal{\r
-void init();\r
-}\r
-\r
-void add_file_sink(const std::wstring& folder);\r
-\r
-enum severity_level\r
-{\r
-       trace,\r
-       debug,\r
-       info,\r
-       warning,\r
-       error,\r
-       fatal\r
-};\r
-\r
-template< typename CharT, typename TraitsT >\r
-inline std::basic_ostream< CharT, TraitsT >& operator<< (\r
-       std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)\r
-{\r
-       if(lvl == trace)\r
-               strm << "trace";\r
-       else if(lvl == debug)\r
-               strm << "debug";\r
-       else if(lvl == info)\r
-               strm << "info";\r
-       else if(lvl == warning)\r
-               strm << "warning";\r
-       else if(lvl == error)\r
-               strm << "error";\r
-       else if(lvl == fatal)\r
-               strm << "fatal";\r
-       else\r
-               strm << static_cast<int>(lvl);\r
-\r
-       return strm;\r
-}\r
-\r
-typedef boost::log::sources::wseverity_logger_mt<severity_level> caspar_logger;\r
-\r
-BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(logger, caspar_logger)\r
-{\r
-       internal::init();\r
-       return caspar_logger(boost::log::keywords::severity = trace);\r
-}\r
-\r
-#define CASPAR_LOG(lvl)\\r
-       BOOST_LOG_STREAM_WITH_PARAMS(::caspar::log::get_logger(),\\r
-               (::boost::log::keywords::severity = ::caspar::log::lvl))\r
-\r
-#define CASPAR_LOG_CURRENT_EXCEPTION() \\r
-       try\\r
-       {CASPAR_LOG(error) << caspar::u16(boost::current_exception_diagnostic_information());}\\r
-       catch(...){}\r
-\r
-void set_log_level(const std::wstring& lvl);\r
-\r
-template<typename T>\r
-inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)\r
-{\r
-       std::locale loc;\r
-       std::replace_if(str.begin(), str.end(), [&](T c)->bool { return !std::isprint(c, loc) && c != '\r' && c != '\n'; }, with);\r
-}\r
-\r
-template<typename T>\r
-inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)\r
-{\r
-       replace_nonprintable(str, with);\r
-       return str;\r
-}\r
-\r
-}}\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#pragma once
+
+#include "os/stack_trace.h"
+#include "utf.h"
+#include "thread_info.h"
+#include "enum_class.h"
+
+#include <boost/log/trivial.hpp>
+#include <boost/log/expressions.hpp>
+#include <boost/log/sources/global_logger_storage.hpp>
+#include <boost/log/sources/severity_channel_logger.hpp>
+#include <boost/exception/all.hpp>
+#include <boost/property_tree/ptree_fwd.hpp>
+
+#include <string>
+#include <locale>
+#include <functional>
+#include <memory>
+
+namespace caspar { namespace log {
+       
+namespace internal{
+void init();
+std::wstring get_call_stack();
+}
+
+template<typename T>
+inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)
+{
+       std::locale loc;
+       std::replace_if(str.begin(), str.end(), [&](T c)->bool {
+               return 
+                       (!std::isprint(c, loc) 
+                       && c != '\r' 
+                       && c != '\n')
+                       || c > static_cast<T>(127);
+       }, with);
+}
+
+template<typename T>
+inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)
+{
+       replace_nonprintable(str, with);
+       return str;
+}
+
+void add_file_sink(const std::wstring& file, const boost::log::filter& filter);
+std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string line)> formatted_line_sink);
+
+enum class log_category
+{
+       normal                  = 1,
+       calltrace               = 2,
+       communication   = 4
+};
+ENUM_ENABLE_BITWISE(log_category)
+BOOST_LOG_ATTRIBUTE_KEYWORD(category, "Channel", ::caspar::log::log_category)
+
+typedef boost::log::sources::wseverity_channel_logger_mt<boost::log::trivial::severity_level, log_category> caspar_logger;
+
+BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(logger, caspar_logger)
+{
+       internal::init();
+       return caspar_logger(
+                       boost::log::keywords::severity = boost::log::trivial::trace,
+                       boost::log::keywords::channel = log_category::normal);
+}
+
+#define CASPAR_LOG(lvl)\
+       BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::normal,                ::boost::log::trivial::lvl)
+#define CASPAR_LOG_CALL(lvl)\
+       BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::calltrace,             ::boost::log::trivial::lvl)
+#define CASPAR_LOG_COMMUNICATION(lvl)\
+       BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::communication, ::boost::log::trivial::lvl)
+
+#define CASPAR_LOG_CALL_STACK()        try{\
+               CASPAR_LOG(info) << L"callstack (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
+       }\
+       catch(...){}
+
+#define CASPAR_LOG_CURRENT_EXCEPTION() try{\
+               CASPAR_LOG(error) << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
+       }\
+       catch(...){}
+
+#define CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(lvl) try{\
+               CASPAR_LOG(lvl) << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
+       }\
+       catch(...){}
+
+void set_log_level(const std::wstring& lvl);
+void set_log_category(const std::wstring& cat, bool enabled);
+
+void print_child(
+               boost::log::trivial::severity_level level,
+               const std::wstring& indent,
+               const std::wstring& elem,
+               const boost::property_tree::wptree& tree);
+
+}}
+