DPsim
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 
25 namespace CPS {
26 
27 class Logger {
28 
29 public:
30  using Level = spdlog::level::level_enum;
31  using Log = std::shared_ptr<spdlog::logger>;
32 
33 private:
34  static Log create(const std::string &name, Level filelevel = Level::info,
35  Level clilevel = Level::off);
36 
37 public:
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
static void setLogDir(String path)
Set env variable CPS_LOG_DIR and overwrite.
Definition: Logger.cpp:88