Display_Lib_RPI 2.7.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 <format>
22#include <iomanip>
23#include <ctime>
24#include <cstdlib>
25#include <filesystem>
26#include <cmath> // For std::cos, std::sin std::lerp
27
29namespace rdlib_time
30{
31 std::string UTC_string();
32}
33
35namespace rdlib_config
36{
37 void loadConfig();
38 bool isLoggingEnabled();
39 bool isDebugEnabled();
40 std::string getErrorLogPath();
41}
42
44namespace rdlib_maths
45{
46 int mapValue(int x, int in_min, int in_max, int out_min, int out_max);
47 float sineFromDegrees(float angle);
48 float cosineFromDegrees(float angle);
49 uint16_t generateColor(uint8_t value);
50 uint16_t blend565(uint16_t c1, uint16_t c2, uint8_t amount);
51}
52
54namespace rdlib_log
55{
56
57std::ostream& operator<<(std::ostream& os, const std::source_location& location);
58
63template<typename DATA_T>
64class logData {
65 public:
72 logData(std::string str, DATA_T data, const std::source_location& loc =
73 std::source_location::current())
74 : errorString(std::move(str)), userData(std::move(data)), errorLocation(loc) {
76 logError();
77 }
78 }
83 std::string& what() { return errorString; }
88 const std::string& what() const noexcept { return errorString; }
93 const std::source_location& where() const noexcept { return errorLocation; }
98 DATA_T& data() { return userData; }
103 const DATA_T& data() const noexcept { return userData; }
104
105private:
106 std::string errorString;
107 DATA_T userData;
108 const std::source_location errorLocation;
113 void logError() const {
114 std::ofstream logFile(rdlib_config::getErrorLogPath(), std::ios::app);
115 if (logFile) {
116 // Get current time
117 auto now = std::chrono::system_clock::now();
118 auto timeT = std::chrono::system_clock::to_time_t(now);
119 logFile << "[" << std::put_time(std::localtime(&timeT), "%Y-%m-%d %H:%M:%S") << "]\n"
120 << "Info: " << errorString << "\n"
121 << "Data: " << userData << "\n"
122 << "Location: " << errorLocation << "\n"
123 << "-----------------------------\n";
124 } else {
125 std::cerr << "Failed to open log file: " << rdlib_config::getErrorLogPath() << "\n";
126 perror("Error reason"); // Print system error message
127 }
128 }
129};
130
131}
Exception class template for handling and logging errors.
Definition utility_data_RDL.hpp:64
const DATA_T & data() const noexcept
Retrieves the user-defined data (const version).
Definition utility_data_RDL.hpp:103
const std::source_location errorLocation
Definition utility_data_RDL.hpp:108
DATA_T & data()
Retrieves the user-defined data.
Definition utility_data_RDL.hpp:98
const std::string & what() const noexcept
Retrieves the error message (const version).
Definition utility_data_RDL.hpp:88
DATA_T userData
Definition utility_data_RDL.hpp:107
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:72
std::string & what()
Retrieves the error message.
Definition utility_data_RDL.hpp:83
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:113
std::string errorString
Definition utility_data_RDL.hpp:106
const std::source_location & where() const noexcept
Retrieves the source location of the exception.
Definition utility_data_RDL.hpp:93
Name space for config file related utilities.
Definition utility_data_RDL.hpp:36
void loadConfig()
This function loads the config file into program.
Definition utility_data_RDL.cpp:177
bool isDebugEnabled()
retrieve the debug enabled setting , true for debug on
Definition utility_data_RDL.cpp:230
std::string getErrorLogPath()
retrieve the log file path setting
Definition utility_data_RDL.cpp:237
bool isLoggingEnabled()
retrieve the logging enabled setting , true for logging on
Definition utility_data_RDL.cpp:223
Name space for log file related utilities.
Definition utility_data_RDL.hpp:55
std::ostream & operator<<(std::ostream &os, const std::source_location &location)
Overloaded stream insertion operator for std::source_location.
Definition utility_data_RDL.cpp:152
Name space for maths related utilities.
Definition utility_data_RDL.hpp:45
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
uint16_t blend565(uint16_t c1, uint16_t c2, uint8_t amount)
: Blend two RGB565 colors using 8-bit mix factor
Definition utility_data_RDL.cpp:98
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
Name space for time related utilities.
Definition utility_data_RDL.hpp:30
std::string UTC_string()
Definition utility_data_RDL.cpp:136