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