Skip to content

Commit

Permalink
Fixes regarding Log smart pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
HomesGH committed Jul 31, 2024
1 parent 7c53add commit 0808152
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 51 deletions.
12 changes: 5 additions & 7 deletions src/MarDyn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <memory>

#include "WrapOpenMP.h"

Expand Down Expand Up @@ -58,7 +59,7 @@ void initOptions(optparse::OptionParser *op) {
/**
* @brief Helper function outputting program build information to given logger
*/
void program_build_info(Log::Logger *log) {
void program_build_info(std::shared_ptr<Log::Logger> log) {
log->info() << "Compilation info:" << std::endl;

char info_str[MAX_INFO_STRING_LENGTH];
Expand All @@ -81,7 +82,7 @@ void program_build_info(Log::Logger *log) {
/**
* @brief Helper function outputting program invocation information to given logger
*/
void program_execution_info(int argc, char **argv, Log::Logger *log) {
void program_execution_info(int argc, char **argv, std::shared_ptr<Log::Logger> log) {
log->info() << "Execution info:" << std::endl;

char info_str[MAX_INFO_STRING_LENGTH];
Expand Down Expand Up @@ -136,7 +137,7 @@ int main(int argc, char** argv) {
#endif

/* Initialize the global log file */
Log::global_log = new Log::Logger(Log::Info);
Log::global_log = std::make_shared<Log::Logger>(Log::Info);
#ifdef ENABLE_MPI
Log::global_log->set_mpi_output_root(0);
//global_log->set_mpi_output_all();
Expand All @@ -160,8 +161,7 @@ int main(int argc, char** argv) {
if( options.is_set_by_user("logfile") ) {
std::string logfileNamePrefix(options.get("logfile"));
Log::global_log->info() << "Using logfile with prefix " << logfileNamePrefix << std::endl;
delete Log::global_log;
Log::global_log = new Log::Logger(Log::Info, logfileNamePrefix);
Log::global_log = std::make_shared<Log::Logger>(Log::Info, logfileNamePrefix);
}
if( options.is_set_by_user("verbose") ) {
Log::global_log->info() << "Enabling verbose log output." << std::endl;
Expand Down Expand Up @@ -281,8 +281,6 @@ int main(int argc, char** argv) {

simulation.finalize();

delete Log::global_log;

#ifdef ENABLE_MPI
MPI_Finalize();
#endif
Expand Down
15 changes: 2 additions & 13 deletions src/utils/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Log {

Logger *global_log;
std::shared_ptr<Logger> global_log;

// Write to stream
Logger::Logger(logLevel level, std::ostream *os) :
Expand Down Expand Up @@ -39,21 +39,10 @@ Logger::Logger(logLevel level, std::string prefix) :
filenamestream << ".log";
_filename = filenamestream.str();

_log_stream = std::make_unique<std::ofstream>(_filename.c_str());
_log_stream = std::make_shared<std::ofstream>(_filename.c_str());
*_log_stream << std::boolalpha; // Print boolean as true/false
}

Logger::~Logger() {
*_log_stream << std::flush;
if (_filename != "") {
// Convert from ostream to ofstream for closing if _log_stream is file
std::ofstream* file = dynamic_cast<std::ofstream*>(_log_stream.get());
if (file && file->is_open()) {
file->close();
}
}
}


/// allow logging only for a single process
void Logger::set_mpi_output_root(int root) {
Expand Down
9 changes: 4 additions & 5 deletions src/utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class Logger;

/**
* Gobal logger variable for use in the entire program.
* Must be initialized with constructor e.g. new Log::Logger().
* Must be initialized with constructor
* Namespace visibility:
* */
#ifndef LOGGER_SRC
extern Log::Logger *global_log;
extern std::shared_ptr<Log::Logger> global_log;
#endif

/**
Expand Down Expand Up @@ -93,7 +93,7 @@ class Logger {
logLevel _msg_log_level;
bool _do_output;
std::string _filename;
std::unique_ptr<std::ostream> _log_stream;
std::shared_ptr<std::ostream> _log_stream;
std::map<logLevel, std::string> logLevelNames;
#ifdef USE_GETTIMEOFDAY
timeval _starttime;
Expand Down Expand Up @@ -129,8 +129,7 @@ class Logger {

Logger(logLevel level, std::string prefix); // Write to file

/// Destructor flushes stream and/or closes file
~Logger();
~Logger() = default;

/// General output template for variables, strings, etc.
template<typename T>
Expand Down
7 changes: 5 additions & 2 deletions src/utils/Testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
*/

#include "utils/Testing.h"

#include <memory>

#include "utils/Logger.h"
#include "utils/FileUtils.h"
#include "utils/mardyn_assert.h"

Log::Logger* test_log;
std::shared_ptr<Log::Logger> test_log;

#ifdef UNIT_TESTS
#ifdef USE_CPPUNIT
Expand All @@ -30,7 +33,7 @@ Log::Logger* test_log;
int runTests(Log::logLevel testLogLevel, std::string& testDataDirectory, const std::string& testcases) {
Log::logLevel globalLogLevel = Log::global_log->get_log_level();

test_log = new Log::Logger(testLogLevel);
test_log = std::make_shared<Log::Logger>(testLogLevel);
test_log->set_do_output(Log::global_log->get_do_output());
if (testLogLevel > Log::Info) {
Log::global_log->set_log_level(Log::Debug);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void setTestDataDirectory(std::string& testDataDirectory);
* Gobal logger variable for use in the test cases.
* Is initialized in runTests().
*/
extern Log::Logger* test_log;
extern std::shared_ptr<Log::Logger> test_log;

#ifdef UNIT_TESTS

Expand Down
23 changes: 7 additions & 16 deletions tools/gui/generators/MDGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "MDGenerator.h"

#include <memory>

#include "parallel/DomainDecompBase.h"
#include "io/CheckpointWriter.h"
#include "particleContainer/LinkedCells.h"
Expand Down Expand Up @@ -42,30 +44,19 @@ const double MDGenerator::abogadro_constant = 6.02214078e23;
const double MDGenerator::boltzmann_constant_kB = 1.38065e-23;

MDGenerator::MDGenerator(std::string name) :
Generator(name), _deleteLogger(true) {
Generator(name) {
// initialize monolithic Mardyn's global_log and silence it...
#ifndef MARDYN
// if mardyn is not the main program, silence it's logger;
Log::global_log = new Log::Logger(Log::Warning, &(std::cout));
_logger = new Log::Logger(Log::Debug, &ScenarioGeneratorApplication::getInstance()->getTextMessageStream());
Log::global_log = std::make_shared<Log::Logger>(Log::Warning, &(std::cout));
_logger = std::make_shared<Log::Logger>(Log::Debug, &ScenarioGeneratorApplication::getInstance()->getTextMessageStream());
#else
_logger = new Log::Logger(Log::Debug, &(std::cout));
_logger = std::make_shared<Log::Logger>(Log::Debug, &(std::cout));
#endif
}

MDGenerator::~MDGenerator() {
if (_deleteLogger) {
delete _logger;
}
}

void MDGenerator::setLogger(Log::Logger* logger) {
if (_logger != NULL && _deleteLogger) {
delete _logger;
}

void MDGenerator::setLogger(std::shared_ptr<Log::Logger> logger) {
_logger = logger;
_deleteLogger = false;
}

void MDGenerator::createSampleObject() const {
Expand Down
8 changes: 4 additions & 4 deletions tools/gui/generators/MDGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <iostream>
#include <list>
#include <vector>
#include <memory>

class Domain;
class DomainDecompBase;
Expand Down Expand Up @@ -72,12 +73,11 @@ class MDGenerator: public Generator, public InputBase {
static const double kelvin_2_mardyn;

protected:
Log::Logger* _logger;
bool _deleteLogger;
std::shared_ptr<Log::Logger> _logger;

MDGenerator(std::string name);

virtual ~MDGenerator();
virtual ~MDGenerator() {};

/**
* determine the velocity according to the temperature.
Expand All @@ -91,7 +91,7 @@ class MDGenerator: public Generator, public InputBase {

public:

virtual void setLogger(Log::Logger* logger);
virtual void setLogger(std::shared_ptr<Log::Logger> logger);

//! NOP
void setPhaseSpaceFile(std::string /*filename*/) {}
Expand Down
6 changes: 5 additions & 1 deletion tools/gui/generators/common/DropletPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

#include "DropletPlacement.h"
#include <cmath>
#include <memory>

#include "utils/Logger.h"


#define SIZE 100

DropletPlacement::DropletPlacement(double fluidVolume, double maxSphereVolume, int numSphereSizes, Log::Logger* logger)
DropletPlacement::DropletPlacement(double fluidVolume, double maxSphereVolume, int numSphereSizes, std::shared_ptr<Log::Logger> logger)
: _fluidVolume(fluidVolume / 100.), _maxSphereRadius(pow((3.0*maxSphereVolume / 100.)/(4.0 * M_PI), 1.0/3.0)),
_numSphereSizes(numSphereSizes), _numOccupied(0), _logger(logger)
{
Expand Down
5 changes: 3 additions & 2 deletions tools/gui/generators/common/DropletPlacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "utils/Logger.h"
#include <vector>
#include <memory>

/**
* Places drops of different sizes in the domain, so that a given percentage
Expand Down Expand Up @@ -41,7 +42,7 @@ class DropletPlacement {
* - each class covering the same volume in total
* - the size of a sphere is determined by pow(0.9, i) * maxSphereRadius; i in [1,maxSphereSize]
*/
DropletPlacement(double fluidVolume, double maxSphereVolume, int numSphereSizes, Log::Logger* logger);
DropletPlacement(double fluidVolume, double maxSphereVolume, int numSphereSizes, std::shared_ptr<Log::Logger> logger);

/**
* Generates droplets with sizes as specified by numSphereSizes, fluidVolume
Expand All @@ -59,7 +60,7 @@ class DropletPlacement {
int _numOccupied;
std::vector<std::vector<std::vector<bool> > > _occupiedFields;

Log::Logger* _logger;
std::shared_ptr<Log::Logger> _logger;

/**
* Initialize vector _occupiedFields
Expand Down

0 comments on commit 0808152

Please sign in to comment.