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#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
12#include <spdlog/spdlog.h>
13
14#if defined(SPDLOG_VER_MAJOR) && SPDLOG_VER_MAJOR >= 1
15#include <spdlog/sinks/basic_file_sink.h>
16#else
17#include <spdlog/sinks/file_sinks.h>
18#endif
19
20#include <spdlog/fmt/ostr.h>
21
22#include <dpsim-models/Definitions.h>
23#include <dpsim-models/MathUtils.h>
24
25namespace CPS {
26
27class Logger {
28
29public:
30 using Level = spdlog::level::level_enum;
31 using Log = std::shared_ptr<spdlog::logger>;
32
33private:
34 static Log create(const std::string &name, Level filelevel = Level::info,
35 Level clilevel = Level::off);
36
37public:
38 Logger();
39 ~Logger();
40
41 static String prefix();
42 static String logDir();
43 static void setLogDir(String path);
44
45 // #### SPD log wrapper ####
47 static Log get(const std::string &name, Level filelevel = Level::info,
48 Level clilevel = Level::off);
50 static void setLogLevel(std::shared_ptr<spdlog::logger> logger,
51 Logger::Level level);
53 static void setLogPattern(std::shared_ptr<spdlog::logger> logger,
54 std::string pattern);
55
56 // #### to string methods ####
57 static String matrixToString(const Matrix &mat);
58 static String matrixCompToString(const MatrixComp &mat);
59 static String sparseMatrixToString(const SparseMatrix &mat);
60 static String sparseMatrixCompToString(const SparseMatrixComp &mat);
61 static String phasorMatrixToString(const MatrixComp &mat);
62 static String phasorToString(const Complex &num);
63 static String complexToString(const Complex &num);
64 static String realToString(const Real &num);
65
66 static String getCSVColumnNames(std::vector<String> names);
67 static String getCSVLineFromData(Real time, Real data);
68 static String getCSVLineFromData(Real time, const Matrix &data);
69 static String getCSVLineFromData(Real time, const MatrixComp &data);
70};
71} // namespace CPS
72
73#if FMT_VERSION >= 90000
74template <>
75class fmt::formatter<CPS::Complex> : public fmt::ostream_formatter {};
76template <>
77class fmt::formatter<CPS::Vector> : public fmt::ostream_formatter {};
78template <>
79class fmt::formatter<CPS::VectorComp> : public fmt::ostream_formatter {};
80template <>
81class fmt::formatter<CPS::SparseMatrix> : public fmt::ostream_formatter {};
82template <>
83class fmt::formatter<CPS::SparseMatrixRow> : public fmt::ostream_formatter {};
84template <>
85class fmt::formatter<CPS::SparseMatrixComp> : public fmt::ostream_formatter {};
86template <>
87class fmt::formatter<CPS::SparseMatrixCompRow> : public fmt::ostream_formatter {
88};
89template <>
90class fmt::formatter<CPS::Matrix> : public fmt::ostream_formatter {};
91template <>
92class fmt::formatter<CPS::MatrixComp> : public fmt::ostream_formatter {};
93template <>
94class fmt::formatter<CPS::MatrixInt> : public fmt::ostream_formatter {};
95template <>
96class fmt::formatter<CPS::MatrixRow> : public fmt::ostream_formatter {};
97template <>
98class fmt::formatter<CPS::LUFactorized> : public fmt::ostream_formatter {};
99template <>
100class fmt::formatter<CPS::LUFactorizedSparse> : public fmt::ostream_formatter {
101};
102template <typename VarType>
103class fmt::formatter<CPS::MatrixVar<VarType>> : public fmt::ostream_formatter {
104};
105template <int rows, int cols>
106class fmt::formatter<CPS::MatrixFixedSize<rows, cols>>
107 : public fmt::ostream_formatter {};
108template <int rows, int cols>
109class fmt::formatter<CPS::MatrixFixedSizeComp<rows, cols>>
110 : public fmt::ostream_formatter {};
111
112template <>
113class fmt::formatter<Eigen::Block<CPS::Matrix>>
114 : public fmt::ostream_formatter {};
115template <>
116class fmt::formatter<Eigen::Block<CPS::MatrixComp>>
117 : public fmt::ostream_formatter {};
118#endif
static void setLogDir(String path)
Set env variable CPS_LOG_DIR and overwrite.
Definition Logger.cpp:88