Skip to content

Commit

Permalink
Added logger (#33)
Browse files Browse the repository at this point in the history
* pybind11 msvc compiler bug microsoft/onnxruntime#9735

* changes to use of logger

* GEMS-210 Added function for set up logging level and log to file

* GEMS-210 Added function for set up logging level and log to file for Python API

* updated pipelines CI to macos12

Co-authored-by: svetad <[email protected]>
Co-authored-by: G. Dan Miron <[email protected]>
  • Loading branch information
3 people authored Jul 15, 2022
1 parent 6a4c7c3 commit de3a95d
Show file tree
Hide file tree
Showing 52 changed files with 337 additions and 193 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Require a certain version of cmake
cmake_minimum_required(VERSION 3.9)

# Set the name of the project
project(ThermoFun VERSION 0.3.9 LANGUAGES CXX)

# Set the cmake module path of the project
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

# Use ccache to speed up repeated compilations
include(CCache)

# Set the name of the project
project(ThermoFun VERSION 0.4.0 LANGUAGES CXX)

# Define variables with the GNU standard installation directories
include(GNUInstallDirs)

Expand Down
5 changes: 2 additions & 3 deletions ThermoFun/Batch/OutputBatch.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "OutputBatch.h"
#include "ThermoBatch.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
Expand Down Expand Up @@ -197,7 +196,7 @@ auto Output::foutResults()-> void
{
for (unsigned j=0; j<j_size; j++)
{
double T, P; string symbol;
double T, P; std::string symbol;
if (outSettings.loopOverTPpairsFirst)
{
T = tpPairs[j][0];
Expand Down Expand Up @@ -254,7 +253,7 @@ auto Output::foutResultsTransposed()-> void
{
for (unsigned j=0; j<j_size; j++)
{
double T, P; string property;
double T, P; std::string property;
if (outSettings.loopOverTPpairsFirst)
{
T = tpPairs[j][0];
Expand Down
13 changes: 5 additions & 8 deletions ThermoFun/Batch/ThermoBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
// C++ includes
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>

// ThermoFun includes
#include "ThermoProperties.h"
#include "Database.h"
#include "ThermoEngine.h"
#include "Common/Units.hpp"

#ifdef _WIN32
#if _MSC_VER >= 1929
#include <array>
#else
#endif
Expand Down Expand Up @@ -249,7 +246,7 @@ struct ThermoBatch::Impl

auto calculate(Calculation calculation) -> void
{
double T, P; string symbol; size_t j_size, i_size;
double T, P; std::string symbol; size_t j_size, i_size;
auto defUnitT = defaultPropertyUnits.at("temperature");
auto unitT = givenPropertyUnits.at("temperature");
auto defUnitP = defaultPropertyUnits.at("pressure");
Expand Down Expand Up @@ -320,20 +317,20 @@ struct ThermoBatch::Impl
}

// Calculate functions
auto calculateSubstProp( double T, double &P, string symbol, unsigned index ) -> void
auto calculateSubstProp( double T, double &P, std::string symbol, unsigned index ) -> void
{
results[index] = selectResultsSubst(thermo.thermoPropertiesSubstance(T, P, symbol));
}

auto calculateReactProp( double T, double &P, string symbol, unsigned index ) -> void
auto calculateReactProp( double T, double &P, std::string symbol, unsigned index ) -> void
{
if (outSettings.reactionPropertiesFromReactants)
results[index] = selectResultsReact(thermo.thermoPropertiesReactionFromReactants(T, P, symbol));
else
results[index] = selectResultsReact(thermo.thermoPropertiesReaction(T, P, symbol));
}

auto calculateSolventProp( double T, double &P, string symbol, unsigned index ) -> void
auto calculateSolventProp( double T, double &P, std::string symbol, unsigned index ) -> void
{
results[index] = selectResultsSolvent(thermo.propertiesSolvent(T, P, symbol),
thermo.electroPropertiesSolvent(T, P, symbol));
Expand Down
59 changes: 58 additions & 1 deletion ThermoFun/Common/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,65 @@
// C++ includes
#include <algorithm>
#include <sstream>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
#include "ThermoProperties.h"

#define LOG_PATTERN "[%n] [%^%l%$] %v"

namespace ThermoFun {

// Thread-safe logger to stdout with colors
std::shared_ptr<spdlog::logger> thfun_logger = spdlog::stdout_color_mt("thermofun");

void update_loggers( bool use_cout, const std::string& logfile_name, size_t log_level)
{
auto thermofun_logger = spdlog::get("thermofun");
auto chemicalfun_logger = spdlog::get("chemicalfun");

// change level
spdlog::level::level_enum log_lev = spdlog::level::info;
if( log_level<7 ) {
log_lev = static_cast<spdlog::level::level_enum>(log_level);
}
thermofun_logger->set_level(log_lev);
chemicalfun_logger->set_level(log_lev);

//change sinks
thermofun_logger->sinks().clear();
chemicalfun_logger->sinks().clear();
if(use_cout) {
auto console_output = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_output->set_pattern(LOG_PATTERN);
thermofun_logger->sinks().push_back(console_output);
chemicalfun_logger->sinks().push_back(console_output);
} else
if (!logfile_name.empty())
{
auto file_output = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logfile_name, 1048576, 3);
thermofun_logger->sinks().push_back(file_output);
chemicalfun_logger->sinks().push_back(file_output);
}
}

void clear_loggers( const std::string& logfile_name)
{
auto thermofun_logger = spdlog::get("thermofun");
auto chemicalfun_logger = spdlog::get("chemicalfun");

thermofun_logger->sinks().clear();
chemicalfun_logger->sinks().clear();

if (!logfile_name.empty())
{
auto file_output = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logfile_name, true);
thermofun_logger->sinks().push_back(file_output);
chemicalfun_logger->sinks().push_back(file_output);
}
}


namespace internal {
/// Creates the location string from the file name and line number.
/// The result of this function on the file `/home/user/gitThermoFun/ThermoFun/src/Substance.cpp`
Expand All @@ -24,7 +80,7 @@ std::string location(const std::string& file, int line)
return ss.str();
}

std::string message(const Exception& exception, const std::string& /*file*/, int /*line*/)
std::string message(const Exception& exception, const std::string& /*file*/, int line)
{
std::string error = exception.error.str();
std::string reason = exception.reason.str();
Expand All @@ -39,6 +95,7 @@ std::string message(const Exception& exception, const std::string& /*file*/, int
message << "*** Location: " << loc << std::endl;
message << bar << std::endl;
message << std::endl;
thfun_logger->error(" {} - {} {}", line, error, reason);
return message.str();
}
}
Expand Down
4 changes: 4 additions & 0 deletions ThermoFun/Common/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#include <string>
#include "ThermoScalar.hpp"
//#include "ThermoProperties.h"
#include "spdlog/spdlog.h"

namespace ThermoFun {

/// Default logger for ThermoFun library
extern std::shared_ptr<spdlog::logger> thfun_logger;

struct ThermoPropertiesSubstance;
struct ThermoPropertiesReaction;

Expand Down
24 changes: 12 additions & 12 deletions ThermoFun/Common/OutputToCSV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,32 @@ auto OutputToCSV::openThermoPropertiesSubstanceFile (std::string fileName) ->voi
{
fileThermoPropertiesSubstance = fileName;

string fullPath = outputFolderPath + "/" + fileName;
std::string fullPath = outputFolderPath + "/" + fileName;

fThermoPropertiesSubstance.open( fullPath , ios::trunc );
fThermoPropertiesSubstance.open( fullPath , std::ios::trunc );
}

auto OutputToCSV::openPropertiesSolventFile (std::string fileName) ->void
{
filePropertiesSolvent = fileName;

string fullPath = outputFolderPath + "/" + fileName;
std::string fullPath = outputFolderPath + "/" + fileName;

fPropertiesSolvent.open( fullPath , ios::trunc );
fPropertiesSolvent.open( fullPath , std::ios::trunc );
}

auto OutputToCSV::openElectroPropertiesSolventFile (std::string fileName) ->void
{
fileElectroPropertiesSolvent = fileName;

string fullPath = outputFolderPath + "/" + fileName;
std::string fullPath = outputFolderPath + "/" + fileName;

fElectroPropertiesSolvent.open( fullPath , ios::trunc );
fElectroPropertiesSolvent.open( fullPath , std::ios::trunc );
}

auto OutputToCSV::writeThermoPropertiesSubstance (std::string symbol, double T, double P, ThermoPropertiesSubstance tps ) -> void
{
string c = ",";
std::string c = ",";

if (!isHeaderThermoPropSubst)
{
Expand All @@ -118,12 +118,12 @@ auto OutputToCSV::writeThermoPropertiesSubstance (std::string symbol, double T,
fThermoPropertiesSubstance << symbol << c << T << c << P << c <<
tps.gibbs_energy << c << tps.enthalpy << c << tps.entropy << c <<
tps.heat_capacity_cp << c << tps.heat_capacity_cv << c << tps.volume << c <<
tps.helmholtz_energy << c << tps.internal_energy << endl;
tps.helmholtz_energy << c << tps.internal_energy << std::endl;
}

auto OutputToCSV::writePropertiesSolvent (std::string symbol, double T, double P, PropertiesSolvent ps ) -> void
{
string c = ",";
std::string c = ",";

if (!isHeaderPropSolv)
{
Expand All @@ -133,12 +133,12 @@ auto OutputToCSV::writePropertiesSolvent (std::string symbol, double T, double P

fPropertiesSolvent << symbol << c << T << c << P << c <<
ps.density << c << ps.densityT << c << ps.densityP << c << ps.densityTT << c <<
ps.densityTP << c << ps.densityPP << c << ps.Alpha << c << ps.dAldT << c << ps.Beta << endl;
ps.densityTP << c << ps.densityPP << c << ps.Alpha << c << ps.dAldT << c << ps.Beta << std::endl;
}

auto OutputToCSV::writeElectroPropertiesSolvent (std::string symbol, double T, double P, ElectroPropertiesSolvent eps ) -> void
{
string c = ",";
std::string c = ",";

if (!isHeaderElectroPropSolv)
{
Expand All @@ -150,7 +150,7 @@ auto OutputToCSV::writeElectroPropertiesSolvent (std::string symbol, double T, d
eps.bornN << c << eps.bornQ << c << eps.bornU << c << eps.bornX << c <<
eps.bornX << c << eps.bornY << c << eps.bornZ << c << eps.epsilon << c <<
eps.epsilonP << c << eps.epsilonPP << c << eps.epsilonT << c << eps.epsilonTP << c <<
eps.epsilonTT << endl;
eps.epsilonTT << std::endl;
}

}
Expand Down
1 change: 0 additions & 1 deletion ThermoFun/Common/OutputWaterSteamConventionProp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Common/OutputWaterSteamConventionProp.h"
#include "GlobalVariables.h"
#include <iostream>
#include <fstream>

namespace ThermoFun {
Expand Down
Loading

0 comments on commit de3a95d

Please sign in to comment.