Skip to content

Commit

Permalink
vs_logging.cpp/.h: ...
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen G authored and Stephen G committed Apr 23, 2024
1 parent b4d36b6 commit 452652d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
35 changes: 27 additions & 8 deletions engine/src/vs_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void VegaStrikeLogger::InitLoggingPart2(const uint8_t debug_level,
break;
}

boost::shared_ptr<FileLogBackEnd> file_log_back_end_ = boost::make_shared<FileLogBackEnd>
file_log_back_end_ = boost::make_shared<FileLogBackEnd>
(
boost::log::keywords::file_name =
logging_dir_name + "/" + "vegastrike_%Y-%m-%d_%H_%M_%S.%f.log", /*< file name pattern >*/
Expand All @@ -85,9 +85,9 @@ void VegaStrikeLogger::InitLoggingPart2(const uint8_t debug_level,
boost::log::keywords::format =
"[%TimeStamp%]: %Message%", /*< log record format >*/
boost::log::keywords::auto_flush =
true, /*false,*/ /*< whether to auto flush to the file after every line >*/
boost::log::keywords::min_free_space = 2UL * 1024UL * 1024UL
* 1024UL /*< stop boost::log when there's only 2 GiB free space left >*/
false, /*true,*/ /*< whether to auto flush to the file after every line >*/
boost::log::keywords::min_free_space = 5UL * 1024UL * 1024UL
* 1024UL /*< stop boost::log when there's only 5 GiB free space left >*/
);
file_log_sink_ = boost::make_shared<FileLogSink>(file_log_back_end_);
logging_core_->add_sink(file_log_sink_);
Expand All @@ -96,6 +96,7 @@ void VegaStrikeLogger::InitLoggingPart2(const uint8_t debug_level,
}

void VegaStrikeLogger::FlushLogs() {
logging_core_->flush();
if (console_log_sink_) {
console_log_sink_->feed_records();
console_log_sink_->flush();
Expand All @@ -104,24 +105,34 @@ void VegaStrikeLogger::FlushLogs() {
file_log_sink_->feed_records();
file_log_sink_->flush();
}

std::cout << std::flush;
std::cerr << std::flush;
std::clog << std::flush;
fflush(stdout);
fflush(stderr);
}

void VegaStrikeLogger::FlushLogsProgramExiting() {
logging_core_->flush();
if (console_log_sink_) {
// logging_core_->remove_sink(console_log_sink_);
console_log_sink_->stop();
console_log_sink_->feed_records();
console_log_sink_->flush();
}
if (console_log_back_end_) {
console_log_back_end_->flush();
}
if (file_log_sink_) {
// logging_core_->remove_sink(file_log_sink_);
file_log_sink_->stop();
file_log_sink_->feed_records();
file_log_sink_->flush();
}
if (file_log_back_end_) {
file_log_back_end_->flush();
}

std::cout << std::flush;
std::cerr << std::flush;
Expand All @@ -132,20 +143,28 @@ void VegaStrikeLogger::FlushLogsProgramExiting() {
logging_core_->remove_all_sinks();
console_log_sink_.reset();
file_log_sink_.reset();
console_log_back_end_.reset();
file_log_back_end_.reset();
}

VegaStrikeLogger::VegaStrikeLogger() : slg_(), file_log_sink_(nullptr) {
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, severity_logger_mt<vega_log_level>) {
boost::log::sources::severity_logger_mt<vega_log_level> lg;
boost::log::add_common_attributes();
return lg;
}

VegaStrikeLogger::VegaStrikeLogger() : slg_(my_logger::get()), file_log_sink_(nullptr) {
boost::filesystem::path::imbue(std::locale("C"));
logging_core_ = boost::log::core::get();
boost::log::add_common_attributes();
// slg_ = my_logger::get();

boost::shared_ptr<ConsoleLogBackEnd> console_log_back_end_ = boost::make_shared<ConsoleLogBackEnd>
console_log_back_end_ = boost::make_shared<ConsoleLogBackEnd>
(
boost::shared_ptr<std::ostream>(&std::cerr, boost::null_deleter()),
boost::log::keywords::format =
"%Message%", /*< log record format specific to the console >*/
boost::log::keywords::auto_flush =
true /*false*/ /*< whether to do the equivalent of fflush(stdout) after every msg >*/
false /*true*/ /*< whether to do the equivalent of fflush(stdout) after every msg >*/
);
console_log_sink_ = boost::make_shared<ConsoleLogSink>(console_log_back_end_);
logging_core_->add_sink(console_log_sink_);
Expand Down
9 changes: 8 additions & 1 deletion engine/src/vs_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#include "boost/smart_ptr/shared_ptr.hpp"
#include "boost/smart_ptr/make_shared_object.hpp"
#include "boost/format.hpp"
#include "boost/log/sources/global_logger_storage.hpp"
//#include "boost/log/sources/logger.hpp"
#include "boost/log/core.hpp"
#include "boost/log/expressions.hpp"
#include "boost/log/sources/severity_logger.hpp"
#include "boost/log/sources/record_ostream.hpp"
#include "boost/log/sinks/async_frontend.hpp"
Expand All @@ -54,6 +57,8 @@ enum vega_log_level {
fatal
};

BOOST_LOG_GLOBAL_LOGGER(my_logger, boost::log::sources::severity_logger_mt<VegaStrikeLogging::vega_log_level>)

BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", vega_log_level)

typedef boost::log::sinks::text_ostream_backend ConsoleLogBackEnd;
Expand All @@ -77,7 +82,9 @@ typedef boost::log::sinks::asynchronous_sink<FileLogBackEnd> FileLogSink;
struct VegaStrikeLogger {
private:
boost::log::core_ptr logging_core_;
boost::log::sources::severity_logger_mt<vega_log_level> slg_;
boost::log::sources::severity_logger_mt<vega_log_level>& slg_;
boost::shared_ptr<ConsoleLogBackEnd> console_log_back_end_;
boost::shared_ptr<FileLogBackEnd> file_log_back_end_;
boost::shared_ptr<ConsoleLogSink> console_log_sink_;
boost::shared_ptr<FileLogSink> file_log_sink_;

Expand Down

0 comments on commit 452652d

Please sign in to comment.