]> git.sesse.net Git - casparcg/blob - common/log.h
Remove most of boost::lexical_cast.
[casparcg] / common / log.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 #include "os/stack_trace.h"
25 #include "utf.h"
26 #include "thread_info.h"
27 #include "enum_class.h"
28
29 #include <boost/log/trivial.hpp>
30 #include <boost/log/expressions.hpp>
31 #include <boost/log/sources/global_logger_storage.hpp>
32 #include <boost/log/sources/severity_channel_logger.hpp>
33 #include <boost/exception/all.hpp>
34 #include <boost/property_tree/ptree_fwd.hpp>
35
36 #include <string>
37 #include <locale>
38 #include <functional>
39 #include <memory>
40
41 namespace caspar { namespace log {
42
43 namespace internal{
44 void init();
45 std::wstring get_call_stack();
46 std::string current_exception_diagnostic_information();
47 }
48
49 const char* remove_source_prefix(const char* file);
50
51 template<typename T>
52 inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)
53 {
54         std::locale loc;
55         std::replace_if(str.begin(), str.end(), [&](T c)->bool {
56                 return
57                         (!std::isprint(c, loc)
58                         && c != '\r'
59                         && c != '\n')
60                         || c > static_cast<T>(127);
61         }, with);
62 }
63
64 template<typename T>
65 inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)
66 {
67         replace_nonprintable(str, with);
68         return str;
69 }
70
71 void add_file_sink(const std::wstring& file, const boost::log::filter& filter);
72 std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string line)> formatted_line_sink);
73
74 enum class log_category
75 {
76         normal                  = 1,
77         calltrace               = 2,
78         communication   = 4
79 };
80 ENUM_ENABLE_BITWISE(log_category)
81 BOOST_LOG_ATTRIBUTE_KEYWORD(category, "Channel", ::caspar::log::log_category)
82
83 typedef boost::log::sources::wseverity_channel_logger_mt<boost::log::trivial::severity_level, log_category> caspar_logger;
84
85 BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(logger, caspar_logger)
86 {
87         internal::init();
88         return caspar_logger(
89                         boost::log::keywords::severity = boost::log::trivial::trace,
90                         boost::log::keywords::channel = log_category::normal);
91 }
92
93 #define CASPAR_LOG(lvl)\
94         BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::normal,                ::boost::log::trivial::lvl)
95 #define CASPAR_LOG_CALL(lvl)\
96         BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::calltrace,             ::boost::log::trivial::lvl)
97 #define CASPAR_LOG_COMMUNICATION(lvl)\
98         BOOST_LOG_CHANNEL_SEV(::caspar::log::logger::get(), ::caspar::log::log_category::communication, ::boost::log::trivial::lvl)
99
100 #define CASPAR_LOG_CALL_STACK() try{\
101                 CASPAR_LOG(info) << L"callstack (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
102         }\
103         catch(...){}
104
105 #define CASPAR_LOG_CURRENT_EXCEPTION() try{\
106                 CASPAR_LOG(error) << caspar::u16(::caspar::log::internal::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
107         }\
108         catch(...){}
109
110 #define CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(lvl) try{\
111                 CASPAR_LOG(lvl) << caspar::u16(::caspar::log::internal::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
112         }\
113         catch(...){}
114
115 void set_log_level(const std::wstring& lvl);
116 void set_log_category(const std::wstring& cat, bool enabled);
117
118 void print_child(
119                 boost::log::trivial::severity_level level,
120                 const std::wstring& indent,
121                 const std::wstring& elem,
122                 const boost::property_tree::wptree& tree);
123
124 }}