2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
24 #include "os/stack_trace.h"
26 #include "thread_info.h"
28 #include <boost/log/trivial.hpp>
29 #include <boost/log/expressions.hpp>
30 #include <boost/log/sources/global_logger_storage.hpp>
31 #include <boost/log/sources/severity_channel_logger.hpp>
32 #include <boost/exception/all.hpp>
33 #include <boost/property_tree/ptree_fwd.hpp>
40 namespace caspar { namespace log {
44 std::wstring get_call_stack();
48 inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)
51 std::replace_if(str.begin(), str.end(), [&](T c)->bool {
53 (!std::isprint(c, loc)
56 || c > static_cast<T>(127);
61 inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)
63 replace_nonprintable(str, with);
67 void add_file_sink(const std::wstring& file, const boost::log::filter& filter);
68 std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string line)> formatted_line_sink);
70 enum class log_category
76 BOOST_LOG_ATTRIBUTE_KEYWORD(category, "Channel", ::caspar::log::log_category)
78 typedef boost::log::sources::wseverity_channel_logger_mt<boost::log::trivial::severity_level, log_category> caspar_logger;
80 BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(logger, caspar_logger)
84 boost::log::keywords::severity = boost::log::trivial::trace,
85 boost::log::keywords::channel = log_category::normal);
88 #define CASPAR_LOG(lvl)\
89 BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::normal, ::boost::log::trivial::lvl)
90 #define CASPAR_LOG_CALL(lvl)\
91 BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::call, ::boost::log::trivial::lvl)
92 #define CASPAR_LOG_COMMUNICATION(lvl)\
93 BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::communication, ::boost::log::trivial::lvl)
95 #define CASPAR_LOG_CALL_STACK() try{\
96 CASPAR_LOG(info) << L"callstack (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
100 #define CASPAR_LOG_CURRENT_EXCEPTION() try{\
101 CASPAR_LOG(error) << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
105 #define CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(lvl) try{\
106 CASPAR_LOG(lvl) << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
110 void set_log_level(const std::wstring& lvl);
113 boost::log::trivial::severity_level level,
114 const std::wstring& indent,
115 const std::wstring& elem,
116 const boost::property_tree::wptree& tree);