Display_Lib_RPI 2.4.0
A C++ Library to connect electronic displays to Linux single board computers.
Loading...
Searching...
No Matches
utility_data_RDL.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <string>
14#include <utility>
15#include <iostream>
16#include <ostream>
17#include <sstream>
18#include <fstream>
19#include <source_location>
20#include <chrono>
21#include <iomanip>
22#include <ctime>
23#include <cstdlib>
24#include <filesystem>
25#include <cmath> // For std::cos, std::sin std::lerp
26
28namespace rdlib_config
29{
30 void loadConfig();
31 bool isLoggingEnabled();
32 bool isDebugEnabled();
33 std::string getErrorLogPath();
34}
35
37namespace rdlib_maths
38{
39 int mapValue(int x, int in_min, int in_max, int out_min, int out_max);
40 float sineFromDegrees(float angle);
41 float cosineFromDegrees(float angle);
42 uint16_t generateColor(uint8_t value);
43}
44
46namespace rdlib_log
47{
48
49std::ostream& operator<<(std::ostream& os, const std::source_location& location);
50
55template<typename DATA_T>
56class logData {
57 public:
64 logData(std::string str, DATA_T data, const std::source_location& loc =
65 std::source_location::current())
66 : errorString(std::move(str)), userData(std::move(data)), errorLocation(loc) {
68 logError();
69 }
70 }
75 std::string& what() { return errorString; }
80 const std::string& what() const noexcept { return errorString; }
85 const std::source_location& where() const noexcept { return errorLocation; }
90 DATA_T& data() { return userData; }
95 const DATA_T& data() const noexcept { return userData; }
96
97private:
98 std::string errorString;
99 DATA_T userData;
100 const std::source_location errorLocation;
105 void logError() const {
106 std::ofstream logFile(rdlib_config::getErrorLogPath(), std::ios::app);
107 if (logFile) {
108 // Get current time
109 auto now = std::chrono::system_clock::now();
110 auto timeT = std::chrono::system_clock::to_time_t(now);
111 logFile << "[" << std::put_time(std::localtime(&timeT), "%Y-%m-%d %H:%M:%S") << "]\n"
112 << "Info: " << errorString << "\n"
113 << "Data: " << userData << "\n"
114 << "Location: " << errorLocation << "\n"
115 << "-----------------------------\n";
116 } else {
117 std::cerr << "Failed to open log file: " << rdlib_config::getErrorLogPath() << "\n";
118 perror("Error reason"); // Print system error message
119 }
120 }
121};
122
123}
Exception class template for handling and logging errors.
Definition utility_data_RDL.hpp:56
const DATA_T & data() const noexcept
Retrieves the user-defined data (const version).
Definition utility_data_RDL.hpp:95
const std::source_location errorLocation
Definition utility_data_RDL.hpp:100
DATA_T & data()
Retrieves the user-defined data.
Definition utility_data_RDL.hpp:90
const std::string & what() const noexcept
Retrieves the error message (const version).
Definition utility_data_RDL.hpp:80
DATA_T userData
Definition utility_data_RDL.hpp:99
logData(std::string str, DATA_T data, const std::source_location &loc=std::source_location::current())
Constructs an OmegaException with an error message and user data.
Definition utility_data_RDL.hpp:64
std::string & what()
Retrieves the error message.
Definition utility_data_RDL.hpp:75
void logError() const
Logs the error message to a file. If the logfile cannot be opened, an error message is printed to std...
Definition utility_data_RDL.hpp:105
std::string errorString
Definition utility_data_RDL.hpp:98
const std::source_location & where() const noexcept
Retrieves the source location of the exception.
Definition utility_data_RDL.hpp:85
Name space for config file related utilities.
Definition utility_data_RDL.hpp:29
void loadConfig()
This function loads the config file into program.
Definition utility_data_RDL.cpp:125
bool isDebugEnabled()
retrieve the debug enabled setting , true for debug on
Definition utility_data_RDL.cpp:178
std::string getErrorLogPath()
retrieve the log file path setting
Definition utility_data_RDL.cpp:185
bool isLoggingEnabled()
retrieve the logging enabled setting , true for logging on
Definition utility_data_RDL.cpp:171
Name space for log file related utilities.
Definition utility_data_RDL.hpp:47
std::ostream & operator<<(std::ostream &os, const std::source_location &location)
Overloaded stream insertion operator for std::source_location.
Definition utility_data_RDL.cpp:100
Name space for maths related utilities.
Definition utility_data_RDL.hpp:38
uint16_t generateColor(uint8_t value)
This function is designed to return a color in the 16-bit RGB format based on the input value,...
Definition utility_data_RDL.cpp:73
float sineFromDegrees(float angle)
Computes the sine of an angle given in degrees. This function converts the input angle from degrees t...
Definition utility_data_RDL.cpp:55
float cosineFromDegrees(float angle)
Computes the cosine of an angle given in degrees. This function converts the input angle from degrees...
Definition utility_data_RDL.cpp:42
int mapValue(int x, int in_min, int in_max, int out_min, int out_max)
Maps a value from one range to another using linear interpolation. This function takes an input value...
Definition utility_data_RDL.cpp:29