12 #include <spdlog/sinks/null_sink.h>
13 #include <spdlog/sinks/stdout_color_sinks.h>
15 #include <dpsim-models/Filesystem.h>
16 #include <dpsim-models/Logger.h>
20 void Logger::setLogLevel(std::shared_ptr<spdlog::logger> logger,
21 Logger::Level level) {
22 logger->set_level(level);
25 void Logger::setLogPattern(std::shared_ptr<spdlog::logger> logger,
26 std::string pattern) {
27 logger->set_pattern(pattern);
31 String Logger::matrixToString(
const Matrix &mat) {
33 ss << std::scientific <<
"\n" << mat;
37 String Logger::matrixCompToString(
const MatrixComp &mat) {
39 ss << std::scientific <<
"\n" << mat;
43 String Logger::sparseMatrixToString(
const SparseMatrix &mat) {
44 return matrixToString(Matrix(mat));
47 String Logger::sparseMatrixCompToString(
const SparseMatrixComp &mat) {
48 return matrixCompToString(MatrixComp(mat));
51 String Logger::phasorMatrixToString(
const MatrixComp &mat) {
53 ss << std::scientific << Math::abs(mat) <<
"\n\n" << Math::phase(mat);
57 String Logger::phasorToString(
const Complex &num) {
59 ss << std::defaultfloat << Math::abs(num) <<
"<" << Math::phaseDeg(num);
63 String Logger::complexToString(
const Complex &num) {
65 ss << std::defaultfloat << num.real() <<
"+j" << num.imag();
69 String Logger::realToString(
const Real &num) {
71 ss << std::defaultfloat << num;
75 String Logger::prefix() {
76 char *p = getenv(
"CPS_LOG_PREFIX");
81 String Logger::logDir() {
82 char *p = getenv(
"CPS_LOG_DIR");
84 return p ? p :
"logs";
90 setenv(
"CPS_LOG_DIR", path.c_str(), 1);
92 String var =
"CPS_LOG_DIR=" + path;
97 String Logger::getCSVColumnNames(std::vector<String> names) {
99 ss << std::right << std::setw(14) <<
"time";
100 for (
auto name : names) {
101 ss <<
", " << std::right << std::setw(13) << name;
108 String Logger::getCSVLineFromData(Real time, Real data) {
109 std::stringstream ss;
110 ss << std::scientific << std::right << std::setw(14) << time;
111 ss <<
", " << std::right << std::setw(13) << data;
117 String Logger::getCSVLineFromData(Real time,
const Matrix &data) {
118 std::stringstream ss;
119 ss << std::scientific << std::right << std::setw(14) << time;
120 for (Int i = 0; i < data.rows(); i++) {
121 ss <<
", " << std::right << std::setw(13) << data(i, 0);
128 String Logger::getCSVLineFromData(Real time,
const MatrixComp &data) {
129 std::stringstream ss;
130 ss << std::scientific << std::right << std::setw(14) << time;
131 for (Int i = 0; i < data.rows(); i++) {
132 ss <<
", " << std::right << std::setw(13) << data(i, 0);
139 Logger::Log Logger::get(
const std::string &name, Level filelevel,
141 Logger::Log logger = spdlog::get(name);
144 logger = create(name, filelevel, clilevel);
150 Logger::Log Logger::create(
const std::string &name, Level filelevel,
152 String logDir = Logger::logDir();
153 String filename = logDir +
"/" + name +
".log";
154 std::vector<spdlog::sink_ptr> sinks;
158 fs::path p = filename;
159 if (p.has_parent_path() && !fs::exists(p.parent_path()))
160 fs::create_directories(p.parent_path());
162 if (clilevel != Logger::Level::off) {
163 auto console_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
164 console_sink->set_level(clilevel);
165 console_sink->set_pattern(
166 fmt::format(
"{}[%T.%f %n %^%l%$] %v", CPS::Logger::prefix()));
167 sinks.push_back(console_sink);
170 if (filelevel != Logger::Level::off) {
172 std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename,
true);
173 file_sink->set_level(filelevel);
174 file_sink->set_pattern(prefix() +
"[%L] %v");
175 sinks.push_back(file_sink);
178 if (filelevel == Logger::Level::off && clilevel == Logger::Level::off) {
179 ret = spdlog::create<spdlog::sinks::null_sink_st>(name);
181 ret = std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));
static void setLogDir(String path)
Set env variable CPS_LOG_DIR and overwrite.