DPsim
Loading...
Searching...
No Matches
Logger.h
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
24#include <dpsim-models/Attribute.h>
25#include <dpsim-models/Definitions.h>
26#include <dpsim-models/MathUtils.h>
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:
41 Logger();
42 ~Logger();
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);
63 static String sparseMatrixCompToString(const SparseMatrixComp &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);
70 static String getCSVLineFromData(Real time, Real data);
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
static void setLogDir(String path)
Set env variable CPS_LOG_DIR and overwrite.
Definition Logger.cpp:88