]> git.sesse.net Git - casparcg/blob - common/log.h
- Removed need of non-deterministic sleeps during server shutdown.
[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
28 #include <boost/log/trivial.hpp>
29 #include <boost/log/sources/global_logger_storage.hpp>
30 #include <boost/exception/all.hpp>
31
32 #include <string>
33 #include <locale>
34
35 namespace caspar { namespace log {
36         
37 namespace internal{
38 void init();
39 std::wstring get_call_stack();
40 }
41
42 template<typename T>
43 inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)
44 {
45         std::locale loc;
46         std::replace_if(str.begin(), str.end(), [&](T c)->bool {
47                 return 
48                         (!std::isprint(c, loc) 
49                         && c != '\r' 
50                         && c != '\n')
51                         || c > static_cast<T>(127);
52         }, with);
53 }
54
55 template<typename T>
56 inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)
57 {
58         replace_nonprintable(str, with);
59         return str;
60 }
61
62 void add_file_sink(const std::wstring& folder);
63
64 typedef boost::log::sources::wseverity_logger_mt<boost::log::trivial::severity_level> caspar_logger;
65
66 BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(logger, caspar_logger)
67 {
68         internal::init();
69         return caspar_logger(boost::log::trivial::trace);
70 }
71
72 #define CASPAR_LOG(lvl)\
73         BOOST_LOG_SEV(::caspar::log::logger::get(), ::boost::log::trivial::lvl)
74
75 #define CASPAR_LOG_CALL_STACK() try{\
76                 CASPAR_LOG(info) << L"callstack (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
77         }\
78         catch(...){}
79
80 #define CASPAR_LOG_CURRENT_EXCEPTION() try{\
81                 CASPAR_LOG(error)  << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at (" << caspar::get_thread_info().name << L"):\n" << caspar::get_call_stack();\
82         }\
83         catch(...){}
84         
85 void set_log_level(const std::wstring& lvl);
86
87 }}
88