]> git.sesse.net Git - casparcg/blobdiff - common/log.cpp
Fix a few Clang warnings.
[casparcg] / common / log.cpp
index b6488739dffac300eb56ea85a2b9d12e445f312d..63641a4efeaed8674b5634a3939b07b26bbd77df 100644 (file)
@@ -29,7 +29,9 @@
 
 #include <ios>
 #include <iomanip>
+#include <mutex>
 #include <string>
+#include <thread>
 #include <ostream>
 
 #include <boost/shared_ptr.hpp>
@@ -56,7 +58,6 @@
 #include <boost/bind.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/property_tree/ptree.hpp>
-#include <boost/thread/mutex.hpp>
 
 #include <tbb/atomic.h>
 
@@ -65,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
@@ -121,7 +121,7 @@ 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);
+       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));
 
@@ -147,6 +147,10 @@ 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>();
@@ -163,6 +167,16 @@ 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& file, const boost::log::filter& filter)
@@ -199,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))
                {
                }
@@ -219,7 +235,7 @@ 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));
@@ -233,9 +249,9 @@ std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string
        });
 }
 
-boost::mutex& get_filter_mutex()
+std::mutex& get_filter_mutex()
 {
-       static boost::mutex instance;
+       static std::mutex instance;
 
        return instance;
 }
@@ -268,7 +284,7 @@ void set_log_filter()
 
 void set_log_level(const std::wstring& lvl)
 {
-       boost::lock_guard<boost::mutex> lock(get_filter_mutex());
+       std::lock_guard<std::mutex> lock(get_filter_mutex());
 
        if (boost::iequals(lvl, L"trace"))
                get_level() = boost::log::trivial::trace;
@@ -297,7 +313,7 @@ void set_log_category(const std::wstring& cat, bool enabled)
        else
                return; // Ignore
 
-       boost::lock_guard<boost::mutex> lock(get_filter_mutex());
+       std::lock_guard<std::mutex> lock(get_filter_mutex());
        auto& disabled_categories = get_disabled_categories();
 
        if (enabled)