]> git.sesse.net Git - casparcg/blob - common/log.h
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 void add_file_sink(const std::wstring& folder);\r
50 \r
51 enum severity_level\r
52 {\r
53         trace,\r
54         debug,\r
55         info,\r
56         warning,\r
57         error,\r
58         fatal\r
59 };\r
60 \r
61 template< typename CharT, typename TraitsT >\r
62 inline std::basic_ostream< CharT, TraitsT >& operator<< (\r
63         std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)\r
64 {\r
65         if(lvl == trace)\r
66                 strm << "trace";\r
67         else if(lvl == debug)\r
68                 strm << "debug";\r
69         else if(lvl == info)\r
70                 strm << "info";\r
71         else if(lvl == warning)\r
72                 strm << "warning";\r
73         else if(lvl == error)\r
74                 strm << "error";\r
75         else if(lvl == fatal)\r
76                 strm << "fatal";\r
77         else\r
78                 strm << static_cast<int>(lvl);\r
79 \r
80         return strm;\r
81 }\r
82 \r
83 typedef boost::log::sources::wseverity_logger_mt<severity_level> caspar_logger;\r
84 \r
85 BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(logger, caspar_logger)\r
86 {\r
87         internal::init();\r
88         return caspar_logger(boost::log::keywords::severity = trace);\r
89 }\r
90 \r
91 #define CASPAR_LOG(lvl)\\r
92         BOOST_LOG_STREAM_WITH_PARAMS(::caspar::log::get_logger(),\\r
93                 (::boost::log::keywords::severity = ::caspar::log::lvl))\r
94 \r
95 #define CASPAR_LOG_CALL_STACK() try{\\r
96                 CASPAR_LOG(info) << L"callstack:\n" << caspar::log::internal::get_call_stack();\\r
97         }\\r
98         catch(...){}\r
99 \r
100 #define CASPAR_LOG_CURRENT_EXCEPTION() try{\\r
101                 CASPAR_LOG(error)  << caspar::u16(boost::current_exception_diagnostic_information()) << L"Caught at:\n" << caspar::log::internal::get_call_stack();\\r
102         }\\r
103         catch(...){}\r
104         \r
105 void set_log_level(const std::wstring& lvl);\r
106 \r
107 }}\r
108 \r
109 #if defined(_MSC_VER)\r
110 #pragma warning (pop)\r
111 #endif