]> git.sesse.net Git - casparcg/blob - common/log/log.cpp
2.0.0.2: decklink_consumer: New options, fill_and_key, key_only, fill_only.
[casparcg] / common / log / log.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "../stdafx.h"\r
21 \r
22 #if defined(_MSC_VER)\r
23 #pragma warning (disable : 4100) // 'identifier' : unreferenced formal parameter\r
24 #pragma warning (disable : 4512) // 'class' : assignment operator could not be generated\r
25 #endif\r
26 \r
27 #include "log.h"\r
28 \r
29 #include "../exception/exceptions.h"\r
30 \r
31 #include <ios>\r
32 #include <string>\r
33 #include <ostream>\r
34 \r
35 #include <boost/shared_ptr.hpp>\r
36 #include <boost/make_shared.hpp>\r
37 #include <boost/filesystem/convenience.hpp>\r
38 #include <boost/date_time/posix_time/posix_time.hpp>\r
39 \r
40 #include <boost/log/core/core.hpp>\r
41 \r
42 #include <boost/log/formatters/stream.hpp>\r
43 #include <boost/log/formatters/attr.hpp>\r
44 #include <boost/log/formatters/date_time.hpp>\r
45 #include <boost/log/formatters/message.hpp>\r
46 \r
47 #include <boost/log/filters/attr.hpp>\r
48 \r
49 #include <boost/log/sinks/text_file_backend.hpp>\r
50 \r
51 #include <boost/log/detail/universal_path.hpp>\r
52 \r
53 #include <boost/log/sinks/text_file_backend.hpp>\r
54 #include <boost/log/sinks/text_ostream_backend.hpp>\r
55 #include <boost/log/sinks/sync_frontend.hpp>\r
56 #include <boost/log/sinks/async_frontend.hpp>\r
57 \r
58 #include <boost/log/utility/init/common_attributes.hpp>\r
59 #include <boost/log/utility/empty_deleter.hpp>\r
60 \r
61 \r
62 namespace caspar{ namespace log{\r
63 \r
64 using namespace boost;\r
65 \r
66 namespace internal{\r
67 \r
68 void init()\r
69 {       \r
70         boost::log::add_common_attributes<wchar_t>();\r
71         typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
72 \r
73         typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
74 \r
75         typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::wtext_ostream_backend> stream_sink_type;\r
76 \r
77         auto stream_backend = boost::make_shared<boost::log::sinks::wtext_ostream_backend>();\r
78         stream_backend->add_stream(boost::shared_ptr<std::wostream>(&std::wcout, boost::log::empty_deleter()));\r
79         stream_backend->auto_flush(true);\r
80 \r
81         auto stream_sink = boost::make_shared<stream_sink_type>(stream_backend);\r
82 \r
83         stream_sink->locked_backend()->set_formatter(\r
84                 boost::log::formatters::wstream\r
85                         //<< L"[" << boost::log::formatters::date_time<std::tm>(L"TimeStamp") << L"] "\r
86                         << L"[" << boost::log::formatters::attr<boost::log::attributes::current_thread_id::held_type >(L"ThreadID") << L"] "\r
87                         << L"[" << boost::log::formatters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) << L"] "\r
88                         << boost::log::formatters::message<wchar_t>()\r
89         );\r
90 \r
91         boost::log::wcore::get()->add_sink(stream_sink);\r
92 }\r
93 \r
94 }\r
95 \r
96 void add_file_sink(const std::wstring& folder)\r
97 {       \r
98         boost::log::add_common_attributes<wchar_t>();\r
99         typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
100 \r
101         typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
102 \r
103         try\r
104         {\r
105                 if(!boost::filesystem::is_directory(folder))\r
106                         BOOST_THROW_EXCEPTION(directory_not_found());\r
107 \r
108                 auto file_sink = boost::make_shared<file_sink_type>(\r
109                         boost::log::keywords::file_name = (folder + L"caspar_%Y-%m-%d_%H-%M-%S.%N.log"),\r
110                         boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),\r
111                         boost::log::keywords::auto_flush = true\r
112                 );\r
113 \r
114                 file_sink->locked_backend()->set_formatter(\r
115                         boost::log::formatters::wstream\r
116                                 //<< L"[" << boost::log::formatters::date_time<std::tm>(L"TimeStamp") << L"] "\r
117                                 << L"[" << boost::log::formatters::attr<boost::log::attributes::current_thread_id::held_type >(L"ThreadID") << L"] "\r
118                                 << L"[" << boost::log::formatters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) << L"] "\r
119                                 << boost::log::formatters::message<wchar_t>()\r
120                 );\r
121 \r
122                 file_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= info);\r
123                 \r
124                 boost::log::wcore::get()->add_sink(file_sink);\r
125 \r
126                 CASPAR_LOG(info) << L"Logging [info] or higher severity to " << folder << std::endl << std::endl;\r
127         }\r
128         catch(...)\r
129         {\r
130                 std::wcerr << L"Failed to Setup File Logging Sink" << std::endl << std::endl;\r
131         }\r
132 }\r
133 \r
134 }}