]> git.sesse.net Git - casparcg/blob - common/log.h
2b2dd13a4e544a5895519092f3059867b72e2e3c
[casparcg] / common / log.h
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #pragma once\r
23 \r
24 #if defined(_MSC_VER)\r
25 #pragma warning (push)\r
26 #pragma warning (disable : 4100)\r
27 #pragma warning (disable : 4127) // conditional expression is constant\r
28 #pragma warning (disable : 4512)\r
29 #pragma warning (disable : 4714) // marked as __forceinline not inlined\r
30 #pragma warning (disable : 4996) // _CRT_SECURE_NO_WARNINGS\r
31 #endif\r
32 \r
33 #include <boost/log/detail/prologue.hpp>\r
34 #include <boost/log/keywords/severity.hpp>\r
35 #include <boost/log/sources/global_logger_storage.hpp>\r
36 #include <boost/log/sources/severity_logger.hpp>\r
37 #include <boost/log/sources/record_ostream.hpp>\r
38 \r
39 #include <string>\r
40 #include <locale>\r
41 \r
42 namespace caspar { namespace log {\r
43         \r
44 namespace internal{\r
45 void init();\r
46 std::wstring get_call_stack();\r
47 }\r
48 \r
49 template<typename T>\r
50 inline void replace_nonprintable(std::basic_string<T, std::char_traits<T>, std::allocator<T>>& str, T with)\r
51 {\r
52         std::locale loc;\r
53         std::replace_if(str.begin(), str.end(), [&](T c)->bool {\r
54                 return \r
55                         (!std::isprint(c, loc) \r
56                         && c != '\r' \r
57                         && c != '\n')\r
58                         || c > static_cast<T>(127);\r
59         }, with);\r
60 }\r
61 \r
62 template<typename T>\r
63 inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::char_traits<T>, std::allocator<T>> str, T with)\r
64 {\r
65         replace_nonprintable(str, with);\r
66         return str;\r
67 }\r
68 \r
69 void add_file_sink(const std::wstring& folder);\r
70 \r
71 enum severity_level\r
72 {\r
73         trace,\r
74         debug,\r
75         info,\r
76         warning,\r
77         error,\r
78         fatal\r
79 };\r
80 \r
81 template< typename CharT, typename TraitsT >\r
82 inline std::basic_ostream< CharT, TraitsT >& operator<< (\r
83         std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)\r
84 {\r
85         if(lvl == trace)\r
86                 strm << "trace";\r
87         else if(lvl == debug)\r
88                 strm << "debug";\r
89         else if(lvl == info)\r
90                 strm << "info";\r
91         else if(lvl == warning)\r
92                 strm << "warning";\r
93         else if(lvl == error)\r
94                 strm << "error";\r
95         else if(lvl == fatal)\r
96                 strm << "fatal";\r
97         else\r
98                 strm << static_cast<int>(lvl);\r
99 \r
100         return strm;\r
101 }\r
102 \r
103 typedef boost::log::sources::wseverity_logger_mt<severity_level> caspar_logger;\r
104 \r
105 BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(logger, caspar_logger)\r
106 {\r
107         internal::init();\r
108         return caspar_logger(boost::log::keywords::severity = trace);\r
109 }\r
110 \r
111 #define CASPAR_LOG(lvl)\\r
112         BOOST_LOG_STREAM_WITH_PARAMS(::caspar::log::get_logger(),\\r
113                 (::boost::log::keywords::severity = ::caspar::log::lvl))\r
114 \r
115 #define CASPAR_LOG_CALL_STACK() try{\\r
116                 CASPAR_LOG(info) << L"callstack:\n" << caspar::log::internal::get_call_stack();\\r
117         }\\r
118         catch(...){}\r
119 \r
120 #define CASPAR_LOG_CURRENT_EXCEPTION() try{\\r
121                 CASPAR_LOG(error)  << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at:\n" << caspar::log::internal::get_call_stack();\\r
122         }\\r
123         catch(...){}\r
124         \r
125 void set_log_level(const std::wstring& lvl);\r
126 \r
127 }}\r
128 \r
129 #if defined(_MSC_VER)\r
130 #pragma warning (pop)\r
131 #endif