openGPMP
Open Source Mathematics Package
utils.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
40 #ifndef UTILS_HPP
41 #define UTILS_HPP
42 
43 #include <fstream>
44 #include <string>
45 #include <tuple>
46 #include <vector>
47 
49 
51 
52 namespace gpmp {
53 
54 namespace core {
55 
56 class TypeCast {
57  public:
58  void convert();
63  /* std::array -> c array on stack
64  * int arr[4];
65  */
66 
67  /* std::vector -> c array on heap ?
68  * int *arr = new int[5];
69  */
70 
94 };
95 
96 class Logger {
97  public:
108  Logger(LogLevel level = INFO, bool useTimestamp = true);
109 
113  ~Logger();
114 
119  void setLogLevel(LogLevel level);
120 
125  void enableTimestamps(bool enable);
126 
132  void setLogDestination(LogDestination destination);
133 
139  void setLogFile(const std::string &logFile);
140 
146  void log(LogLevel level, const std::string &message);
147 
148  private:
153 
158 
164 
168  bool logToFile;
169 
173  std::ofstream logFileStream;
174 
180  std::string getLogPrefix(LogLevel level);
181 
186  std::string getCurrentTimestamp();
187 
194  std::string formatLogMessage(const std::string &prefix,
195  const std::string &message);
196 };
197 
201 class Misc {
202  public:
211  template <typename T> static T factorial(T n) {
212  if (n < 0)
213  return 0; // Invalid input
214  T result = 1;
215  for (T i = 1; i <= n; ++i) {
216  result *= i;
217  }
218  return result;
219  }
220 };
221 
222 } // namespace core
223 
224 } // namespace gpmp
225 
226 #endif
std::string getCurrentTimestamp()
Gets the current timestamp as a string.
Definition: utils.cpp:110
void setLogLevel(LogLevel level)
Sets the minimum log level.
Definition: utils.cpp:57
std::string formatLogMessage(const std::string &prefix, const std::string &message)
Formats a log message with a specified prefix.
Definition: utils.cpp:125
void setLogFile(const std::string &logFile)
Sets the log file name for FILE_ONLY or CONSOLE_AND_FILE destinations.
Definition: utils.cpp:69
void log(LogLevel level, const std::string &message)
Logs a message with the specified log level.
Definition: utils.cpp:77
LogLevel logLevel
The minimum log level to record.
Definition: utils.hpp:152
LogDestination logDestination
The log destination, which can be CONSOLE, FILE_ONLY, or CONSOLE_AND_FILE.
Definition: utils.hpp:163
void setLogDestination(LogDestination destination)
Sets the log destination.
Definition: utils.cpp:65
std::ofstream logFileStream
The output stream for logging to a file.
Definition: utils.hpp:173
~Logger()
Logger destructor, destroys the Logger instance.
Definition: utils.cpp:51
bool logToFile
Flag indicating whether to log messages to a file.
Definition: utils.hpp:168
void enableTimestamps(bool enable)
Enables or disables timestamps in log messages.
Definition: utils.cpp:61
std::string getLogPrefix(LogLevel level)
Gets the log prefix based on the log level.
Definition: utils.cpp:95
Logger(LogLevel level=INFO, bool useTimestamp=true)
Constructs a Logger instance.
Definition: utils.cpp:46
bool enableTimestamp
Flag indicating whether to include timestamps in log messages.
Definition: utils.hpp:157
A class containing miscellaneous utility functions.
Definition: utils.hpp:201
static T factorial(T n)
Calculates the factorial of the given integer or double.
Definition: utils.hpp:211
The source C++ openGPMP namespace.
LogDestination
Definition: utils.hpp:50
@ CONSOLE
Definition: utils.hpp:50
@ CONSOLE_AND_FILE
Definition: utils.hpp:50
@ FILE_ONLY
Definition: utils.hpp:50
LogLevel
Definition: utils.hpp:48
@ DEBUG
Definition: utils.hpp:48
@ ERROR
Definition: utils.hpp:48
@ INFO
Definition: utils.hpp:48
@ WARNING
Definition: utils.hpp:48