DPsim
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
9#pragma once
10
11#include <memory>
12
13#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
14#include <spdlog/spdlog.h>
15
16#if defined(SPDLOG_VER_MAJOR) && SPDLOG_VER_MAJOR >= 1
17#include <spdlog/sinks/basic_file_sink.h>
18#else
19#include <spdlog/sinks/file_sinks.h>
20#endif
21
22#include <spdlog/fmt/ostr.h>
23
27
28namespace CPS {
29
30class Logger {
31
32public:
33 using Level = spdlog::level::level_enum;
34 using Log = std::shared_ptr<spdlog::logger>;
35
36private:
37 static Log create(const std::string &name, Level filelevel = Level::info,
38 Level clilevel = Level::off);
39
40public:
43
44 static String prefix();
45 static String logDir();
46 static void setLogDir(String path);
47
48 // #### SPD log wrapper ####
50 static Log get(const std::string &name, Level filelevel = Level::info,
51 Level clilevel = Level::off);
53 static void setLogLevel(std::shared_ptr<spdlog::logger> logger,
54 Logger::Level level);
56 static void setLogPattern(std::shared_ptr<spdlog::logger> logger,
57 std::string pattern);
58
59 // #### to string methods ####
60 static String matrixToString(const Matrix &mat);
61 static String matrixCompToString(const MatrixComp &mat);
62 static String sparseMatrixToString(const SparseMatrix &mat);
64 static String phasorMatrixToString(const MatrixComp &mat);
65 static String phasorToString(const Complex &num);
66 static String complexToString(const Complex &num);
67 static String realToString(const Real &num);
68
69 static String getCSVColumnNames(std::vector<String> names);
71 static String getCSVLineFromData(Real time, const Matrix &data);
72 static String getCSVLineFromData(Real time, const MatrixComp &data);
73};
74} // namespace CPS
75
76#if FMT_VERSION >= 90000
77template <>
78class fmt::formatter<CPS::String> : public fmt::ostream_formatter {};
79template <>
80class fmt::formatter<CPS::Complex> : public fmt::ostream_formatter {};
81template <>
82class fmt::formatter<CPS::Vector> : public fmt::ostream_formatter {};
83template <>
84class fmt::formatter<CPS::VectorComp> : public fmt::ostream_formatter {};
85template <>
86class fmt::formatter<CPS::SparseMatrix> : public fmt::ostream_formatter {};
87template <>
88class fmt::formatter<CPS::SparseMatrixRow> : public fmt::ostream_formatter {};
89template <>
90class fmt::formatter<CPS::SparseMatrixComp> : public fmt::ostream_formatter {};
91template <>
92class fmt::formatter<CPS::SparseMatrixCompRow> : public fmt::ostream_formatter {
93};
94template <>
95class fmt::formatter<CPS::Matrix> : public fmt::ostream_formatter {};
96template <>
97class fmt::formatter<CPS::MatrixComp> : public fmt::ostream_formatter {};
98template <>
99class fmt::formatter<CPS::MatrixInt> : public fmt::ostream_formatter {};
100template <>
101class fmt::formatter<CPS::MatrixRow> : public fmt::ostream_formatter {};
102template <>
103class fmt::formatter<CPS::LUFactorized> : public fmt::ostream_formatter {};
104template <>
105class fmt::formatter<CPS::LUFactorizedSparse> : public fmt::ostream_formatter {
106};
107template <typename VarType>
108class fmt::formatter<CPS::MatrixVar<VarType>> : public fmt::ostream_formatter {
109};
110template <int rows, int cols>
111class fmt::formatter<CPS::MatrixFixedSize<rows, cols>>
112 : public fmt::ostream_formatter {};
113template <int rows, int cols>
114class fmt::formatter<CPS::MatrixFixedSizeComp<rows, cols>>
115 : public fmt::ostream_formatter {};
116
117template <>
118class fmt::formatter<Eigen::Block<CPS::Matrix>>
119 : public fmt::ostream_formatter {};
120template <>
121class fmt::formatter<Eigen::Block<CPS::MatrixComp>>
122 : public fmt::ostream_formatter {};
123
124namespace fmt {
125template <typename T> struct formatter<CPS::Attribute<T>> {
126 template <typename ParseContext> constexpr auto parse(ParseContext &ctx) {
127 return ctx.begin();
128 }
129
130 template <typename FormatContext>
131 auto format(const CPS::Attribute<T> &attr, FormatContext &ctx) const {
132 auto &nc = const_cast<CPS::Attribute<T> &>(attr);
133 return fmt::format_to(ctx.out(), "{}", nc.get());
134 }
135};
136
137template <typename T> struct formatter<std::shared_ptr<CPS::Attribute<T>>> {
138 template <typename ParseContext> constexpr auto parse(ParseContext &ctx) {
139 return ctx.begin();
140 }
141
142 template <typename FormatContext>
143 auto format(const std::shared_ptr<CPS::Attribute<T>> &p,
144 FormatContext &ctx) const {
145 if (!p)
146 return fmt::format_to(ctx.out(), "<null>");
147 auto &nc = const_cast<CPS::Attribute<T> &>(*p);
148 return fmt::format_to(ctx.out(), "{}", nc.get());
149 }
150};
151
152} // namespace fmt
153
154#endif
spdlog::level::level_enum Level
Definition Logger.h:33
static String prefix()
Definition Logger.cpp:75
std::shared_ptr< spdlog::logger > Log
Definition Logger.h:34
static String matrixCompToString(const MatrixComp &mat)
Definition Logger.cpp:37
static String phasorMatrixToString(const MatrixComp &mat)
Definition Logger.cpp:51
static String sparseMatrixToString(const SparseMatrix &mat)
Definition Logger.cpp:43
static void setLogDir(String path)
Set env variable CPS_LOG_DIR and overwrite.
Definition Logger.cpp:88
static String logDir()
Definition Logger.cpp:81
static void setLogPattern(std::shared_ptr< spdlog::logger > logger, std::string pattern)
Definition Logger.cpp:25
static Log get(const std::string &name, Level filelevel=Level::info, Level clilevel=Level::off)
Definition Logger.cpp:139
static String complexToString(const Complex &num)
Definition Logger.cpp:63
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static void setLogLevel(std::shared_ptr< spdlog::logger > logger, Logger::Level level)
Definition Logger.cpp:20
static String realToString(const Real &num)
Definition Logger.cpp:69
static String getCSVLineFromData(Real time, Real data)
Definition Logger.cpp:108
static String getCSVColumnNames(std::vector< String > names)
Definition Logger.cpp:97
static String sparseMatrixCompToString(const SparseMatrixComp &mat)
Definition Logger.cpp:47
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
struct dps_magma_data data
Eigen::SparseMatrix< Real, Eigen::ColMajor > SparseMatrix
Sparse matrix for real numbers.
Definition Definitions.h:72
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:81
std::string String
Definition Definitions.h:65
Eigen::SparseMatrix< Complex, Eigen::ColMajor > SparseMatrixComp
Sparse matrix for complex numbers.
Definition Definitions.h:76
Eigen::Matrix< Int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixInt
Dense matrix for integers.
Definition Definitions.h:87
Eigen::PartialPivLU< Matrix > LUFactorized
Definition Definitions.h:92
Eigen::SparseMatrix< Complex, Eigen::RowMajor > SparseMatrixCompRow
Sparse matrix for complex numbers (row major).
Definition Definitions.h:78
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
double Real
Definition Definitions.h:62
Eigen::Matrix< Complex, Eigen::Dynamic, 1 > VectorComp
Dense vector for complex numbers.
Definition Definitions.h:68
Eigen::Matrix< Real, Eigen::Dynamic, 1 > Vector
Dense vector for real numbers.
Definition Definitions.h:70
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixRow
Definition Definitions.h:90
std::complex< Real > Complex
Definition Definitions.h:63
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
Eigen::Matrix< Complex, rows, cols, Eigen::ColMajor > MatrixFixedSizeComp
Dense matrix for complex numbers with fixed dimension.
Eigen::SparseLU< SparseMatrix > LUFactorizedSparse
Definition Definitions.h:94
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74
CPS::Complex Complex
Definition Definitions.h:19
CPS::MatrixVar< T > MatrixVar
Definition Definitions.h:29
CPS::Matrix Matrix
Definition Definitions.h:24
CPS::SparseMatrixRow SparseMatrix
Definition Definitions.h:26
CPS::SparseMatrixCompRow SparseMatrixComp
Definition Definitions.h:27
CPS::MatrixComp MatrixComp
Definition Definitions.h:25