void init(const fs::path &rErrEvtPath, const fs::path &rStatusPath) { // register common attributes - ThreadID, LineID, TimeStamp etc. logging::add_common_attributes(); // Construct the sink for the "event_log" channel typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink; auto sink = boost::make_shared<text_sink>(); sink->locked_backend()->auto_flush(true); sink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(rErrEvtPath.c_str())); sink->set_formatter(expr::format("%1% [%2%] tid[%3%] %4%") % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f") % logging::trivial::severity % expr::attr<attrs::current_thread_id::value_type>("ThreadID") % expr::smessage); sink->set_filter(expr::attr<std::string>("Channel") == "event"); logging::core::get()->add_sink(sink); // Construct the sink for the "status_log" channel sink = boost::make_shared<text_sink>(); sink->locked_backend()->auto_flush(true); sink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(rStatusPath.c_str())); sink->set_formatter(expr::format("%1% [%2%] tid[%3%] %4%") % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f") % logging::trivial::severity % expr::attr<attrs::current_thread_id::value_type>("ThreadID") % expr::smessage); sink->set_filter(expr::attr<std::string>("Channel") == "status"); logging::core::get()->add_sink(sink); }
void logging::file_writer::init() { if (_is_active) return; _sink = boost::log::add_file_log(_file_path.c_str()); _sink->locked_backend()->auto_flush(true); set_formatter(); _is_active = true; }
logger& logger::init() { typedef sinks::debug_output_backend backend_t; auto pDebugBackend = boost::make_shared< backend_t >(); auto pDebugSink = boost::make_shared< sinks::synchronous_sink< backend_t > >(pDebugBackend); pDebugSink->set_filter(expr::attr<severity_level>("Severity") >= trace); pDebugSink->set_formatter( expr::stream << "[" << expr::attr<UINT>("RecordID") << "][" << expr::format_date_time(_timestamp, "%Y-%m-%d %H:%M:%S.%f") << "][" << _severity << "]" << expr::message << std::endl); logging::core::get()->add_sink(pDebugSink); sink_ = logging::add_file_log( keywords::open_mode = std::ios::app, // append write keywords::file_name = file_name_ + "%Y-%m-%d.log", keywords::rotation_size = rotation_size_, keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), keywords::min_free_space = min_free_space_, keywords::format = ( expr::stream << "[" << expr::attr<UINT>("RecordID") << "][" << expr::format_date_time(_timestamp, "%Y-%m-%d %H:%M:%S.%f") << "][" << _severity << "]" << expr::message) ); sink_->locked_backend()->auto_flush(auto_flush_); logging::add_common_attributes(); attrs::counter<UINT> RecordID(1); logging::core::get()->add_global_attribute("RecordID", RecordID); this->set_filter_trace(); return *this; }
void add_cout_sink() { boost::log::add_common_attributes(); // boost::log::core::get()->add_global_attribute("NativeThreadId", // boost::log::attributes::make_function(&std::this_thread::get_id)); boost::log::core::get()->add_global_attribute("TimestampMillis", boost::log::attributes::make_function([] { return boost::posix_time::microsec_clock::local_time(); })); using stream_sink_type = sinks::asynchronous_sink<sinks::wtext_ostream_backend>; auto stream_backend = boost::make_shared<boost::log::sinks::wtext_ostream_backend>(); stream_backend->add_stream(boost::shared_ptr<std::wostream>(&std::wcout, boost::null_deleter())); stream_backend->auto_flush(true); auto stream_sink = boost::make_shared<stream_sink_type>(stream_backend); stream_sink->set_formatter(boost::bind(&my_formatter<boost::log::wformatting_ostream>, false, _1, _2)); logging::core::get()->add_sink(stream_sink); }
void add_file_sink(const std::wstring& file) { using file_sink_type = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>; try { 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 = 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); file_sink->set_formatter(boost::bind(&my_formatter<boost::log::formatting_ostream>, true, _1, _2)); boost::log::core::get()->add_sink(file_sink); } catch (...) { std::wcerr << L"Failed to Setup File Logging Sink" << std::endl << std::endl; } }
BOOST_LOG_GLOBAL_LOGGER_INIT(__logger, bl::sources::severity_logger_mt) { bl::sources::severity_logger_mt<boost::log::trivial::severity_level> logger; logger.add_attribute("TimeStamp", bl::attributes::local_clock()); using text_sink = bl::sinks::asynchronous_sink<bl::sinks::text_ostream_backend>; auto consoleSink = boost::make_shared<text_sink>(); consoleSink->locked_backend()->add_stream(boost::shared_ptr<std::ostream>(&std::cout, boost::null_deleter())); consoleSink->set_filter(severity >= boost::log::trivial::severity_level::warning); using file_sink = bl::sinks::asynchronous_sink<bl::sinks::text_file_backend>; auto logfileSink = boost::make_shared<file_sink>(); { auto logfileBackend = logfileSink->locked_backend(); logfileBackend->set_time_based_rotation(bl::sinks::file::rotation_at_time_point(0, 0, 0)); logfileBackend->set_file_name_pattern("Logs\\ashbot_%Y%m%d.log"); logfileBackend->auto_flush(); logfileBackend->set_file_collector( bl::sinks::file::make_collector(bl::keywords::target = "Logs\\Archive") ); } namespace expr = bl::expressions; bl::formatter fmt = expr::stream << format_date_time(timestamp, "%Y-%m-%d %H:%M:%S.%f") << " [" << boost::log::trivial::severity << "] " << expr::smessage; logfileSink->set_formatter(fmt); bl::core::get()->add_sink(consoleSink); bl::core::get()->add_sink(logfileSink); return logger; }
//Defines a global logger initialization routine BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t) { logger_t lg; logging::add_common_attributes(); #if 0 auto flsink = logging::add_file_log( boost::log::keywords::file_name = SYS_LOGFILE, boost::log::keywords::format = ( expr::stream << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << " [" << expr::attr< boost::log::trivial::severity_level >("Severity") << "]: " << expr::wmessage ) ); flsink->imbue(std::locale(std::locale(), new std::codecvt_utf8_utf16<wchar_t>)); #endif boost::shared_ptr<logging::core> core = logging::core::get(); #if 0 boost::shared_ptr<sinks::text_ostream_backend> ostream_backend = boost::make_shared<sinks::text_ostream_backend>(); ostream_backend->add_stream( boost::shared_ptr<std::ostream>(&std::clog, logging::empty_deleter())); ostream_backend->auto_flush(true); typedef sinks::synchronous_sink<sinks::text_ostream_backend> sink_ostream_t; boost::shared_ptr<sink_ostream_t> sink_ostream(new sink_ostream_t(ostream_backend)); sink_ostream->set_formatter( expr::format("[%1%] [%2%]\t%3%") % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") % logging::trivial::severity % expr::message); core->add_sink(sink_ostream); #endif // ログのファイル出力を設定 auto text_backend = boost::make_shared<sinks::text_file_backend>( boost::log::keywords::file_name = static_cast<LPCWSTR>(Misc::GetExeDirectory() + SYS_LOGFILE) ); text_backend->auto_flush(true); typedef sinks::synchronous_sink<sinks::text_file_backend> sink_text_t; boost::shared_ptr<sink_text_t> sink_text(new sink_text_t(text_backend)); sink_text->set_formatter( expr::format("%1% [%2%]: %3%") % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S") % logging::trivial::severity % expr::wmessage); sink_text->imbue(std::locale(std::locale(), new std::codecvt_utf8_utf16<wchar_t>)); core->add_sink(sink_text); #ifdef _DEBUG // ログのコンソール出力を設定 ::AllocConsole(); //::SetConsoleOutputCP(CP_UTF8); FILE* out = nullptr; freopen_s(&out, "CON", "w", stdout); auto console_sink = logging::add_console_log(std::wcout); console_sink->set_formatter( expr::format(L"%1% [%2%]: %3%") % expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S") % logging::trivial::severity % expr::wmessage); //console_sink->imbue(std::locale(std::locale(), new std::codecvt_utf8_utf16<wchar_t>)); #endif logging::core::get()->set_filter ( #ifdef _DEBUG logging::trivial::severity >= logging::trivial::info #else logging::trivial::severity >= logging::trivial::error #endif ); return lg; }
void chiselinfo::init(string filterstr, string formatterstr) { set_filter(filterstr); set_formatter(formatterstr); }