]> git.sesse.net Git - casparcg/blob - common/log.h
OGL image_mixer: Fixed a performance hotspot where an idle channel without any layers...
[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 <boost/log/trivial.hpp>
25 #include <boost/log/sources/global_logger_storage.hpp>
26
27 #include <string>
28 #include <locale>
29
30 namespace caspar { namespace log {
31         
32 namespace internal{
33 void init();
34 std::wstring get_call_stack();
35 }
36
37 template<typename T>
38 inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)
39 {
40         std::locale loc;
41         std::replace_if(str.begin(), str.end(), [&](T c)->bool {
42                 return 
43                         (!std::isprint(c, loc) 
44                         && c != '\r' 
45                         && c != '\n')
46                         || c > static_cast<T>(127);
47         }, with);
48 }
49
50 template<typename T>
51 inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)
52 {
53         replace_nonprintable(str, with);
54         return str;
55 }
56
57 void add_file_sink(const std::wstring& folder);
58
59 /*template< typename CharT, typename TraitsT >
60 inline std::basic_ostream< CharT, TraitsT >& operator<< (
61         std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
62 {
63         if(lvl == trace)
64                 strm << "trace";
65         else if(lvl == debug)
66                 strm << "debug";
67         else if(lvl == info)
68                 strm << "info";
69         else if(lvl == warning)
70                 strm << "warning";
71         else if(lvl == error)
72                 strm << "error";
73         else if(lvl == fatal)
74                 strm << "fatal";
75         else
76                 strm << static_cast<int>(lvl);
77
78         return strm;
79 }*/
80
81 typedef boost::log::sources::wseverity_logger_mt<boost::log::trivial::severity_level> caspar_logger;
82
83 BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(logger, caspar_logger)
84 {
85         internal::init();
86         return caspar_logger(boost::log::trivial::trace);
87 }
88
89 #define CASPAR_LOG(lvl)\
90         BOOST_LOG_SEV(::caspar::log::logger::get(), ::boost::log::trivial::lvl)
91
92 #define CASPAR_LOG_CALL_STACK() try{\
93                 CASPAR_LOG(info) << L"callstack:\n" << caspar::log::internal::get_call_stack();\
94         }\
95         catch(...){}
96
97 #define CASPAR_LOG_CURRENT_EXCEPTION() try{\
98                 CASPAR_LOG(error)  << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at:\n" << caspar::log::internal::get_call_stack();\
99         }\
100         catch(...){}
101         
102 void set_log_level(const std::wstring& lvl);
103
104 }}
105