X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=common%2Flog%2Flog.cpp;h=061c3f5b56a99b6d805dcba8a59409c056d1e238;hb=5ab41a6dc4a8c5921b09ecc1590bfb459ffe867f;hp=0ac0278c16a1a53cc83311d2f8bda7024259a7cb;hpb=261958466deb1a8b06fcf7b82f2207595b307deb;p=casparcg diff --git a/common/log/log.cpp b/common/log/log.cpp index 0ac0278c1..061c3f5b5 100644 --- a/common/log/log.cpp +++ b/common/log/log.cpp @@ -1,3 +1,25 @@ +/* +* copyright (c) 2010 Sveriges Television AB +* +* This file is part of CasparCG. +* +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. + +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . +* +*/ +// TODO: Colors in console. +// TODO: Think through filters. + #include "../stdafx.h" #if defined(_MSC_VER) @@ -8,7 +30,7 @@ #include "log.h" #include "../exception/exceptions.h" - +#include "../utility/string.h" #include #include #include @@ -17,6 +39,7 @@ #include #include #include +#include #include @@ -35,17 +58,43 @@ #include #include #include +#include +#include #include #include +#include - -namespace caspar{ namespace log{ +namespace caspar { namespace log { using namespace boost; -namespace internal{ +void my_formatter(std::wostream& strm, boost::log::basic_record const& rec) +{ + namespace lambda = boost::lambda; + + #pragma warning(disable : 4996) + time_t rawtime; + struct tm* timeinfo; + time(&rawtime ); + timeinfo = localtime ( &rawtime ); + char buffer [80]; + strftime (buffer,80, "%c", timeinfo); + strm << L"[" << buffer << L"] "; + + boost::log::attributes::current_thread_id::held_type thread_id; + if(boost::log::extract(L"ThreadID", rec.attribute_values(), lambda::var(thread_id) = lambda::_1)) + strm << L"[" << thread_id << L"] "; + + severity_level severity; + if(boost::log::extract(boost::log::sources::aux::severity_attribute_name::get(), rec.attribute_values(), lambda::var(severity) = lambda::_1)) + strm << L"[" << severity << L"] "; + + strm << rec.message(); +} +namespace internal{ + void init() { boost::log::add_common_attributes(); @@ -60,13 +109,14 @@ void init() stream_backend->auto_flush(true); auto stream_sink = boost::make_shared(stream_backend); + +//#ifdef NDEBUG +// stream_sink->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= debug); +//#else +// stream_sink->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= debug); +//#endif - stream_sink->locked_backend()->set_formatter( - boost::log::formatters::wstream - << L"[" << boost::log::formatters::attr(traits_t::thread_id_attr_name()) - << L"] [" << boost::log::formatters::attr(boost::log::sources::aux::severity_attribute_name::get()) - << L"] " << boost::log::formatters::message() - ); + stream_sink->locked_backend()->set_formatter(&my_formatter); boost::log::wcore::get()->add_sink(stream_sink); } @@ -86,25 +136,22 @@ void add_file_sink(const std::wstring& folder) BOOST_THROW_EXCEPTION(directory_not_found()); auto file_sink = boost::make_shared( - boost::log::keywords::file_name = (folder + L"caspar_%Y-%m-%d_%H-%M-%S.%N.log"), + boost::log::keywords::file_name = (folder + L"caspar_%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 - ); - - file_sink->locked_backend()->set_formatter( - boost::log::formatters::wstream - << boost::log::formatters::attr(traits_t::line_id_attr_name()) - << L" [" << boost::log::formatters::date_time< posix_time::ptime >(traits_t::time_stamp_attr_name()) - << L"] [" << boost::log::formatters::attr(traits_t::thread_id_attr_name()) - << L"] [" << boost::log::formatters::attr(boost::log::sources::aux::severity_attribute_name::get()) - << L"] " << boost::log::formatters::message() + boost::log::keywords::auto_flush = true, + boost::log::keywords::open_mode = std::ios::app ); - - file_sink->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= info); - - std::wcout << L"Logging [info] or higher severity to " << folder << std::endl << std::endl; - + + file_sink->locked_backend()->set_formatter(&my_formatter); + +//#ifdef NDEBUG +// file_sink->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= debug); +//#else +// file_sink->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= debug); +//#endif boost::log::wcore::get()->add_sink(file_sink); + + CASPAR_LOG(info) << L"Logging [info] or higher severity to " << folder << std::endl << std::endl; } catch(...) { @@ -112,4 +159,20 @@ void add_file_sink(const std::wstring& folder) } } +void set_log_level(const std::wstring& lvl) +{ + if(boost::iequals(lvl, L"trace")) + boost::log::wcore::get()->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= trace); + else if(boost::iequals(lvl, L"debug")) + boost::log::wcore::get()->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= debug); + else if(boost::iequals(lvl, L"info")) + boost::log::wcore::get()->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= info); + else if(boost::iequals(lvl, L"warning")) + boost::log::wcore::get()->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= warning); + else if(boost::iequals(lvl, L"error")) + boost::log::wcore::get()->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= error); + else if(boost::iequals(lvl, L"fatal")) + boost::log::wcore::get()->set_filter(boost::log::filters::attr(boost::log::sources::aux::severity_attribute_name::get()) >= fatal); +} + }} \ No newline at end of file