]> git.sesse.net Git - casparcg/blobdiff - common/log.cpp
Fix a few Clang warnings.
[casparcg] / common / log.cpp
index 8ae7c35bba9221604aa13be32c8e2c9abfb89a15..63641a4efeaed8674b5634a3939b07b26bbd77df 100644 (file)
 
 #include "log.h"
 
+#include "os/threading.h"
 #include "except.h"
 #include "utf.h"
 
 #include <ios>
 #include <iomanip>
+#include <mutex>
 #include <string>
+#include <thread>
 #include <ostream>
 
 #include <boost/shared_ptr.hpp>
@@ -47,7 +50,7 @@
 #include <boost/log/sinks/async_frontend.hpp>
 #include <boost/log/core/record.hpp>
 #include <boost/log/attributes/attribute_value.hpp>
-#include <boost/log/attributes/current_thread_id.hpp>
+#include <boost/log/attributes/function.hpp>
 #include <boost/log/utility/setup/common_attributes.hpp>
 
 #include <boost/core/null_deleter.hpp>
@@ -63,9 +66,8 @@ namespace caspar { namespace log {
 using namespace boost;
 
 template<typename Stream>
-void append_timestamp(Stream& stream)
+void append_timestamp(Stream& stream, boost::posix_time::ptime timestamp)
 {
-       auto timestamp = boost::posix_time::microsec_clock::local_time();
        auto date = timestamp.date();
        auto time = timestamp.time_of_day();
        auto milliseconds = time.fractional_seconds() / 1000; // microseconds to milliseconds
@@ -119,8 +121,8 @@ void my_formatter(bool print_all_characters, const boost::log::record_view& rec,
        namespace expr = boost::log::expressions;
 
        std::wstringstream pre_message_stream;
-       append_timestamp(pre_message_stream);
-       thread_id_column.write(pre_message_stream, boost::log::extract<boost::log::attributes::current_thread_id::value_type>("ThreadID", rec).get().native_id());
+       append_timestamp(pre_message_stream, boost::log::extract<boost::posix_time::ptime>("TimestampMillis", rec).get());
+       thread_id_column.write(pre_message_stream, boost::log::extract<std::int64_t>("NativeThreadId", rec));
        severity_column.write(pre_message_stream, boost::log::extract<boost::log::trivial::severity_level>("Severity", rec));
 
        auto pre_message = pre_message_stream.str();
@@ -140,10 +142,15 @@ void my_formatter(bool print_all_characters, const boost::log::record_view& rec,
 }
 
 namespace internal{
-       
+
 void init()
-{      
+{
        boost::log::add_common_attributes();
+       boost::log::core::get()->add_global_attribute("NativeThreadId", boost::log::attributes::make_function(&get_current_thread_id));
+       boost::log::core::get()->add_global_attribute("TimestampMillis", boost::log::attributes::make_function([]
+       {
+               return boost::posix_time::microsec_clock::local_time();
+       }));
        typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::wtext_ostream_backend> stream_sink_type;
 
        auto stream_backend = boost::make_shared<boost::log::sinks::wtext_ostream_backend>();
@@ -151,6 +158,8 @@ void init()
        stream_backend->auto_flush(true);
 
        auto stream_sink = boost::make_shared<stream_sink_type>(stream_backend);
+       // Never log calltrace to console. The terminal is too slow, so the log queue will build up faster than consumed.
+       stream_sink->set_filter(category != log_category::calltrace);
 
        bool print_all_characters = false;
        stream_sink->set_formatter(boost::bind(&my_formatter<boost::log::wformatting_ostream>, print_all_characters, _1, _2));
@@ -158,19 +167,29 @@ void init()
        boost::log::core::get()->add_sink(stream_sink);
 }
 
+std::string current_exception_diagnostic_information()
+{
+       auto e = boost::current_exception_cast<const char*>();
+
+       if (e)
+               return std::string("[char *] = ") + *e + "\n";
+       else
+               return boost::current_exception_diagnostic_information();
 }
 
-void add_file_sink(const std::wstring& folder)
+}
+
+void add_file_sink(const std::wstring& file, const boost::log::filter& filter)
 {
        typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend> file_sink_type;
 
        try
        {
-               if (!boost::filesystem::is_directory(folder))
+               if (!boost::filesystem::is_directory(boost::filesystem::path(file).parent_path()))
                        CASPAR_THROW_EXCEPTION(directory_not_found());
 
                auto file_sink = boost::make_shared<file_sink_type>(
-                       boost::log::keywords::file_name = (folder + L"caspar_%Y-%m-%d.log"),
+                       boost::log::keywords::file_name = (file + L"_%Y-%m-%d.log"),
                        boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
                        boost::log::keywords::auto_flush = true,
                        boost::log::keywords::open_mode = std::ios::app
@@ -179,6 +198,7 @@ void add_file_sink(const std::wstring& folder)
                bool print_all_characters = true;
 
                file_sink->set_formatter(boost::bind(&my_formatter<boost::log::formatting_ostream>, print_all_characters, _1, _2));
+               file_sink->set_filter(filter);
                boost::log::core::get()->add_sink(file_sink);
        }
        catch (...)
@@ -193,7 +213,9 @@ std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string
        {
                std::function<void(std::string line)> formatted_line_sink_;
        public:
-               sink_backend(std::function<void(std::string line)> formatted_line_sink)
+               // The dummy parameter is to work around a bug in newer Boost.Log, where single-argument
+               // constructor forwarders are not recognized unless the parameter uses Boost.Parameter.
+               sink_backend(std::function<void(std::string line)> formatted_line_sink, int)
                        : formatted_line_sink_(std::move(formatted_line_sink))
                {
                }
@@ -213,10 +235,11 @@ std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string
 
        typedef boost::log::sinks::synchronous_sink<sink_backend> sink_type;
 
-       auto sink = boost::make_shared<sink_type>(std::move(formatted_line_sink));
+       auto sink = boost::make_shared<sink_type>(std::move(formatted_line_sink), 0);
        bool print_all_characters = true;
 
        sink->set_formatter(boost::bind(&my_formatter<boost::log::formatting_ostream>, print_all_characters, _1, _2));
+       sink->set_filter(category != log_category::calltrace);
 
        boost::log::core::get()->add_sink(sink);
 
@@ -226,20 +249,79 @@ std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string
        });
 }
 
+std::mutex& get_filter_mutex()
+{
+       static std::mutex instance;
+
+       return instance;
+}
+
+boost::log::trivial::severity_level& get_level()
+{
+       static boost::log::trivial::severity_level instance;
+
+       return instance;
+}
+
+log_category& get_disabled_categories()
+{
+       static log_category instance = log_category::calltrace;
+
+       return instance;
+}
+
+void set_log_filter()
+{
+       auto severity_filter            = boost::log::trivial::severity >= get_level();
+       auto disabled_categories        = get_disabled_categories();
+
+       boost::log::core::get()->set_filter([=](const boost::log::attribute_value_set& attributes)
+       {
+               return severity_filter(attributes)
+                       && static_cast<int>(disabled_categories & attributes["Channel"].extract<log_category>().get()) == 0;
+       });
+}
+
 void set_log_level(const std::wstring& lvl)
 {
+       std::lock_guard<std::mutex> lock(get_filter_mutex());
+
        if (boost::iequals(lvl, L"trace"))
-               boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::trace);
+               get_level() = boost::log::trivial::trace;
        else if (boost::iequals(lvl, L"debug"))
-               boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug);
+               get_level() = boost::log::trivial::debug;
        else if (boost::iequals(lvl, L"info"))
-               boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
+               get_level() = boost::log::trivial::info;
        else if (boost::iequals(lvl, L"warning"))
-               boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::warning);
+               get_level() = boost::log::trivial::warning;
        else if (boost::iequals(lvl, L"error"))
-               boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::error);
+               get_level() = boost::log::trivial::error;
        else if (boost::iequals(lvl, L"fatal"))
-               boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::fatal);
+               get_level() = boost::log::trivial::fatal;
+
+       set_log_filter();
+}
+
+void set_log_category(const std::wstring& cat, bool enabled)
+{
+       log_category category_to_set;
+
+       if (boost::iequals(cat, L"calltrace"))
+               category_to_set = log_category::calltrace;
+       else if (boost::iequals(cat, L"communication"))
+               category_to_set = log_category::communication;
+       else
+               return; // Ignore
+
+       std::lock_guard<std::mutex> lock(get_filter_mutex());
+       auto& disabled_categories = get_disabled_categories();
+
+       if (enabled)
+               disabled_categories &= ~category_to_set;
+       else
+               disabled_categories |= category_to_set;
+
+       set_log_filter();
 }
 
 void print_child(
@@ -259,4 +341,14 @@ void print_child(
                print_child(level, indent + (elem.empty() ? L"" : elem + L"."), child.first, child.second);
 }
 
+const char* remove_source_prefix(const char* file)
+{
+       auto found = boost::ifind_first(file, get_source_prefix().c_str());
+
+       if (found)
+               return found.end();
+       else
+               return file;
+}
+
 }}