Add logging facility

This commit is contained in:
jun
2023-10-06 11:44:02 -03:00
parent becef215d8
commit ef074c90d5
5 changed files with 143 additions and 15 deletions

View File

@@ -5,8 +5,13 @@ project(molecular_dynamics)
set(CMAKE_CXX_STANDARD 20)
find_package(Boost REQUIRED COMPONENTS log log_setup)
include_directories(${Boost_INCLUDE_DIRS})
add_subdirectory(src/md)
add_executable(molecular_dynamics src/main.cpp)
add_executable(molecular_dynamics
src/main.cpp
src/log/Log.cpp)
target_link_libraries(molecular_dynamics PRIVATE md)
target_link_libraries(molecular_dynamics PRIVATE md ${Boost_LIBRARIES})

92
src/log/Log.cpp Normal file
View File

@@ -0,0 +1,92 @@
/**
* @file Log.cpp
* @author Jun <jun@firmwarejun.net>
* @brief Logging utilities
* @version 0.1
* @date 2023-10-06
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#include "Log.hpp"
#include <boost/core/null_deleter.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
namespace logging = boost::log;
namespace expr = boost::log::expressions;
void my_formatter(logging::record_view const &rec,
logging::formatting_ostream &strm) {
auto severity = rec[logging::trivial::severity];
auto white = "\033[37m";
auto red = "\033[31m";
auto green = "\033[32m";
auto yellow = "\033[33m";
auto blue = "\033[34m";
auto orange = "\033[35m";
auto cyan = "\033[36m";
auto clear = "\033[0m";
auto severityColor = white;
if (severity) {
// Set the color
switch (severity.get()) {
case logging::trivial::severity_level::info:
severityColor = cyan;
break;
case logging::trivial::severity_level::warning:
severityColor = yellow;
break;
case logging::trivial::severity_level::error:
case logging::trivial::severity_level::fatal:
severityColor = red;
break;
default:
// White
severityColor = white;
break;
}
}
strm << "[" << logging::extract<boost::posix_time::ptime>("TimeStamp", rec)
<< "]";
strm << severityColor << "[" << severity.get() << "] " << rec[expr::smessage]
<< clear;
}
void setupLogging() {
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
boost::shared_ptr<sinks::text_ostream_backend> backend =
boost::make_shared<sinks::text_ostream_backend>();
backend->add_stream(
boost::shared_ptr<std::ostream>(&std::clog, boost::null_deleter()));
// typedef sinks::synchronous_sink<ColorSink> sink_t;
typedef sinks::synchronous_sink<sinks::text_ostream_backend> sink_t;
boost::shared_ptr<sink_t> sink(new sink_t(backend));
sink->set_formatter(&my_formatter);
sink->locked_backend()->auto_flush(true);
logging::add_common_attributes();
logging::core::get()->add_sink(sink);
}

17
src/log/Log.hpp Normal file
View File

@@ -0,0 +1,17 @@
/**
* @file Log.hpp
* @author Jun <jun@firmwarejun.net>
* @brief Logging utilities
* @version 0.1
* @date 2023-10-06
*
* Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
*
*/
#pragma once
#include <boost/log/sinks/basic_sink_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
void setupLogging();

View File

@@ -16,12 +16,31 @@
#include "md/Space.hpp"
#include "md/System.hpp"
#include "log/Log.hpp"
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/mpi/environment.hpp>
namespace mpi = boost::mpi;
int main() {
std::cout << "kk eae men" << std::endl;
int main(int argc, char **argv) {
mpi::environment env;
mpi::communicator world;
setupLogging();
// Initialize logging
boost::log::core::get()->set_filter(boost::log::trivial::severity >=
boost::log::trivial::trace);
if (world.rank() == 0)
BOOST_LOG_TRIVIAL(warning)
<< "Initializing with " << world.size() << " processes";
return 0;
// TODO: get these from command line arguments
double lattice = 5.26;
@@ -37,7 +56,8 @@ int main() {
space.addAtom(atoms);
std::cout << "Setting box..." << std::endl;
space.setBox(lattice * numberOfCells, lattice * numberOfCells, lattice * numberOfCells);
space.setBox(lattice * numberOfCells, lattice * numberOfCells,
lattice * numberOfCells);
std::cout << "Preparing space..." << std::endl;
space.prepareSpace();
@@ -48,11 +68,5 @@ int main() {
std::cout << "Getting neighboring cells of index 0" << std::endl;
Cell neighboringCells = space.getNeighboringCells(0);
mpi::environment env;
mpi::communicator world;
std::cout << "Hello, world! I am process " << world.rank() << " of " << world.size() << "."
<< std::endl;
return 0;
}

View File

@@ -1,6 +1,6 @@
# Copyright (c) 2023 jun <https://git.firmwarejun.net/jun/MolecularDynamics2>
cmake_minimum_required(VERSION 3.25.2)
cmake_minimum_required(VERSION 3.25.1)
project(md)