]> git.sesse.net Git - casparcg/blobdiff - common/log/log.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / log / log.cpp
index a307b2980ee8048bd1bca30075bf72360b75d2d2..802eb93492de3440298253e8a89100e49aa7fd4c 100644 (file)
@@ -1,3 +1,25 @@
+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+*  This file is part of CasparCG.\r
+*\r
+*    CasparCG is free software: you can redistribute it and/or modify\r
+*    it under the terms of the GNU General Public License as published by\r
+*    the Free Software Foundation, either version 3 of the License, or\r
+*    (at your option) any later version.\r
+*\r
+*    CasparCG is distributed in the hope that it will be useful,\r
+*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+*    GNU General Public License for more details.\r
+\r
+*    You should have received a copy of the GNU General Public License\r
+*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
+// TODO: Colors in console.\r
+// TODO: Think through filters.\r
+\r
 #include "../stdafx.h"\r
 \r
 #if defined(_MSC_VER)\r
@@ -8,7 +30,7 @@
 #include "log.h"\r
 \r
 #include "../exception/exceptions.h"\r
-\r
+#include "../utility/string.h"\r
 #include <ios>\r
 #include <string>\r
 #include <ostream>\r
@@ -17,6 +39,7 @@
 #include <boost/make_shared.hpp>\r
 #include <boost/filesystem/convenience.hpp>\r
 #include <boost/date_time/posix_time/posix_time.hpp>\r
+#include <boost/algorithm/string.hpp>\r
 \r
 #include <boost/log/core/core.hpp>\r
 \r
 #include <boost/log/sinks/text_ostream_backend.hpp>\r
 #include <boost/log/sinks/sync_frontend.hpp>\r
 #include <boost/log/sinks/async_frontend.hpp>\r
+#include <boost/log/core/record.hpp>\r
+#include <boost/log/utility/attribute_value_extractor.hpp>\r
 \r
 #include <boost/log/utility/init/common_attributes.hpp>\r
 #include <boost/log/utility/empty_deleter.hpp>\r
+#include <boost/lambda/lambda.hpp>\r
 \r
-\r
-namespace caspar{ namespace log{\r
+namespace caspar { namespace log {\r
 \r
 using namespace boost;\r
 \r
-namespace internal{\r
+void my_formatter(std::wostream& strm, boost::log::basic_record<wchar_t> const& rec)\r
+{\r
+    namespace lambda = boost::lambda;\r
+       \r
+       #pragma warning(disable : 4996)\r
+       time_t rawtime;\r
+       struct tm* timeinfo;\r
+       time(&rawtime );\r
+       timeinfo = localtime ( &rawtime );\r
+       char buffer [80];\r
+       strftime (buffer,80, "%c", timeinfo);\r
+       strm << L"[" << buffer << L"] ";\r
+               \r
+    boost::log::attributes::current_thread_id::held_type thread_id;\r
+    if(boost::log::extract<boost::log::attributes::current_thread_id::held_type>(L"ThreadID", rec.attribute_values(), lambda::var(thread_id) = lambda::_1))\r
+        strm << L"[" << thread_id << L"] ";\r
+\r
+    severity_level severity;\r
+    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
+        strm << L"[" << severity << L"] ";\r
+\r
+    strm << rec.message();\r
+}\r
 \r
+namespace internal{\r
+       \r
 void init()\r
 {      \r
-    boost::log::add_common_attributes<wchar_t>();\r
-    typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
+       boost::log::add_common_attributes<wchar_t>();\r
+       typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
 \r
-    typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
+       typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
 \r
        typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::wtext_ostream_backend> stream_sink_type;\r
 \r
-    auto stream_backend = boost::make_shared<boost::log::sinks::wtext_ostream_backend>();\r
+       auto stream_backend = boost::make_shared<boost::log::sinks::wtext_ostream_backend>();\r
        stream_backend->add_stream(boost::shared_ptr<std::wostream>(&std::wcout, boost::log::empty_deleter()));\r
        stream_backend->auto_flush(true);\r
 \r
        auto stream_sink = boost::make_shared<stream_sink_type>(stream_backend);\r
+       \r
+#ifdef NDEBUG\r
+       stream_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
+#else\r
+       stream_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
+#endif\r
 \r
-    stream_sink->locked_backend()->set_formatter(\r
-        boost::log::formatters::wstream\r
-            << L"[" << boost::log::formatters::attr<boost::log::attributes::current_thread_id::held_type >(traits_t::thread_id_attr_name())\r
-            << L"] [" << boost::log::formatters::attr<severity_level >(boost::log::sources::aux::severity_attribute_name<wchar_t>::get())\r
-            << L"] " << boost::log::formatters::message<wchar_t>()\r
-    );\r
+       stream_sink->locked_backend()->set_formatter(&my_formatter);\r
 \r
        boost::log::wcore::get()->add_sink(stream_sink);\r
 }\r
@@ -75,10 +125,10 @@ void init()
 \r
 void add_file_sink(const std::wstring& folder)\r
 {      \r
-    boost::log::add_common_attributes<wchar_t>();\r
-    typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
+       boost::log::add_common_attributes<wchar_t>();\r
+       typedef boost::log::aux::add_common_attributes_constants<wchar_t> traits_t;\r
 \r
-    typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
+       typedef boost::log::sinks::synchronous_sink<boost::log::sinks::wtext_file_backend> file_sink_type;\r
 \r
        try\r
        {\r
@@ -90,26 +140,38 @@ void add_file_sink(const std::wstring& folder)
                        boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),\r
                        boost::log::keywords::auto_flush = true\r
                );\r
+               \r
+               file_sink->locked_backend()->set_formatter(&my_formatter);\r
 \r
-               file_sink->locked_backend()->set_formatter(\r
-                       boost::log::formatters::wstream\r
-                               << boost::log::formatters::attr<unsigned int>(traits_t::line_id_attr_name())\r
-                               << L" [Time:" << boost::log::formatters::date_time< posix_time::ptime >(traits_t::time_stamp_attr_name())\r
-                               << L"] [Thread:" << boost::log::formatters::attr<boost::log::attributes::current_thread_id::held_type >(traits_t::thread_id_attr_name())\r
-                               << L"] [Severity:" << boost::log::formatters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get())\r
-                               << L"] " << boost::log::formatters::message<wchar_t>()\r
-               );\r
-\r
-               file_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= info);\r
-\r
-               std::wcout << L"Logging [info] or higher severity to " << folder << std::endl;\r
-\r
+#ifdef NDEBUG\r
+               file_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
+#else\r
+               file_sink->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
+#endif\r
                boost::log::wcore::get()->add_sink(file_sink);\r
+\r
+               CASPAR_LOG(info) << L"Logging [info] or higher severity to " << folder << std::endl << std::endl;\r
        }\r
        catch(...)\r
        {\r
-               std::wcerr << L"Failed to Setup File Logging Sink" << std::endl;\r
+               std::wcerr << L"Failed to Setup File Logging Sink" << std::endl << std::endl;\r
        }\r
 }\r
 \r
+void set_log_level(const std::wstring& lvl)\r
+{      \r
+       if(boost::iequals(lvl, L"trace"))\r
+               boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= trace);\r
+       else if(boost::iequals(lvl, L"debug"))\r
+               boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= debug);\r
+       else if(boost::iequals(lvl, L"info"))\r
+               boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= info);\r
+       else if(boost::iequals(lvl, L"warning"))\r
+               boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= warning);\r
+       else if(boost::iequals(lvl, L"error"))\r
+               boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= error);\r
+       else if(boost::iequals(lvl, L"fatal"))\r
+               boost::log::wcore::get()->set_filter(boost::log::filters::attr<severity_level>(boost::log::sources::aux::severity_attribute_name<wchar_t>::get()) >= fatal);\r
+}\r
+\r
 }}
\ No newline at end of file