]> git.sesse.net Git - casparcg/blob - common/log/log.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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 #include "../utility/string.h"\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 #include <boost/algorithm/string.hpp>\r
40 \r
41 #include <boost/log/core/core.hpp>\r
42 \r
43 #include <boost/log/formatters/stream.hpp>\r
44 #include <boost/log/formatters/attr.hpp>\r
45 #include <boost/log/formatters/date_time.hpp>\r
46 #include <boost/log/formatters/message.hpp>\r
47 \r
48 #include <boost/log/filters/attr.hpp>\r
49 \r
50 #include <boost/log/sinks/text_file_backend.hpp>\r
51 \r
52 #include <boost/log/detail/universal_path.hpp>\r
53 \r
54 #include <boost/log/sinks/text_file_backend.hpp>\r
55 #include <boost/log/sinks/text_ostream_backend.hpp>\r
56 #include <boost/log/sinks/sync_frontend.hpp>\r
57 #include <boost/log/sinks/async_frontend.hpp>\r
58 #include <boost/log/core/record.hpp>\r
59 #include <boost/log/utility/attribute_value_extractor.hpp>\r
60 \r
61 #include <boost/log/utility/init/common_attributes.hpp>\r
62 #include <boost/log/utility/empty_deleter.hpp>\r
63 #include <boost/lambda/lambda.hpp>\r
64 \r
65 namespace caspar { namespace log {\r
66 \r
67 using namespace boost;\r
68 \r
69 void my_formatter(std::wostream& strm, boost::log::basic_record<wchar_t> const& rec)\r
70 {\r
71     namespace lambda = boost::lambda;\r
72         \r
73         #pragma warning(disable : 4996)\r
74         time_t rawtime;\r
75         struct tm* timeinfo;\r
76         time(&rawtime );\r
77         timeinfo = localtime ( &rawtime );\r
78         char buffer [80];\r
79         strftime (buffer,80, "%c", timeinfo);\r
80         strm << L"[" << buffer << L"] ";\r
81                 \r
82     boost::log::attributes::current_thread_id::held_type thread_id;\r
83     if(boost::log::extract<boost::log::attributes::current_thread_id::held_type>(L"ThreadID", rec.attribute_values(), lambda::var(thread_id) = lambda::_1))\r
84         strm << L"[" << thread_id << L"] ";\r
85 \r
86     severity_level severity;\r
87     if(boost::log::extract<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get(), rec.attribute_values(), lambda::var(severity) = lambda::_1))\r
88         strm << L"[" << severity << L"] ";\r
89 \r
90     strm << rec.message();\r
91 }\r
92 \r
93 namespace internal{\r
94         \r
95 void init()\r
96 {       \r
97         boost::log::add_common_attributes<wchar_t>();\r
98         typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
99 \r
100         typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
101 \r
102         typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::wtext_ostream_backend> stream_sink_type;\r
103 \r
104         auto stream_backend = boost::make_shared<boost::log::sinks::wtext_ostream_backend>();\r
105         stream_backend->add_stream(boost::shared_ptr<std::wostream>(&std::wcout, boost::log::empty_deleter()));\r
106         stream_backend->auto_flush(true);\r
107 \r
108         auto stream_sink = boost::make_shared<stream_sink_type>(stream_backend);\r
109         \r
110 #ifdef NDEBUG\r
111         stream_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= info);\r
112 #else\r
113         stream_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
114 #endif\r
115 \r
116         stream_sink->locked_backend()->set_formatter(&my_formatter);\r
117 \r
118         boost::log::wcore::get()->add_sink(stream_sink);\r
119 }\r
120 \r
121 }\r
122 \r
123 void add_file_sink(const std::wstring& folder)\r
124 {       \r
125         boost::log::add_common_attributes<wchar_t>();\r
126         typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
127 \r
128         typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
129 \r
130         try\r
131         {\r
132                 if(!boost::filesystem::is_directory(folder))\r
133                         BOOST_THROW_EXCEPTION(directory_not_found());\r
134 \r
135                 auto file_sink = boost::make_shared<file_sink_type>(\r
136                         boost::log::keywords::file_name = (folder + L"caspar_%Y-%m-%d_%H-%M-%S.%N.log"),\r
137                         boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),\r
138                         boost::log::keywords::auto_flush = true\r
139                 );\r
140                 \r
141                 file_sink->locked_backend()->set_formatter(&my_formatter);\r
142 \r
143 #ifdef NDEBUG\r
144                 file_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= info);\r
145 #else\r
146                 file_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= debug);\r
147 #endif\r
148                 boost::log::wcore::get()->add_sink(file_sink);\r
149 \r
150                 CASPAR_LOG(info) << L"Logging [info] or higher severity to " << folder << std::endl << std::endl;\r
151         }\r
152         catch(...)\r
153         {\r
154                 std::wcerr << L"Failed to Setup File Logging Sink" << std::endl << std::endl;\r
155         }\r
156 }\r
157 \r
158 void set_log_level(const std::wstring& lvl)\r
159 {       \r
160         if(boost::iequals(lvl, L"trace"))\r
161                 boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
162         else if(boost::iequals(lvl, L"debug"))\r
163                 boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= debug);\r
164         else if(boost::iequals(lvl, L"info"))\r
165                 boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= info);\r
166         else if(boost::iequals(lvl, L"warning"))\r
167                 boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= warning);\r
168         else if(boost::iequals(lvl, L"error"))\r
169                 boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= error);\r
170         else if(boost::iequals(lvl, L"fatal"))\r
171                 boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= fatal);\r
172 }\r
173 \r
174 }}