Logger refactoring
This commit is contained in:
		
							parent
							
								
									654c1b031a
								
							
						
					
					
						commit
						944038c9d0
					
				
							
								
								
									
										5
									
								
								program/SVET/.vscode/settings.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								program/SVET/.vscode/settings.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| { | ||||
|     "files.associations": { | ||||
|         "string": "cpp" | ||||
|     } | ||||
| } | ||||
| @ -1,5 +1,6 @@ | ||||
| #include <Arduino.h> | ||||
| #include "Logger.h" | ||||
| #include "PerepheryCore/SerialCore.h" | ||||
| 
 | ||||
| /*
 | ||||
|  * Implementation of Logger instance. Docs on Logger.h | ||||
| @ -17,12 +18,12 @@ Logger::Logger( | ||||
|     { | ||||
|         return; | ||||
|     } | ||||
|     Serial.begin(baudRate); | ||||
|     SerialCore::Start(baudRate); | ||||
| } | ||||
| 
 | ||||
| Logger::~Logger() | ||||
| { | ||||
|     Serial.end(); | ||||
|     SerialCore::Stop(); | ||||
| } | ||||
| 
 | ||||
| const char *Logger::messageTypeToString(MessageType messageType) | ||||
| @ -57,50 +58,31 @@ std::string Logger::getMessage(MessageType messageType, char *text, char *initia | ||||
|     return output; | ||||
| } | ||||
| 
 | ||||
| void Logger::sendMessage(char *data, SerialColor color) | ||||
| { | ||||
|     SerialCore::SetColor(color); | ||||
|     SerialCore::SendLine(data); | ||||
|     SerialCore::SetColor(WHITE); | ||||
| } | ||||
| 
 | ||||
| void Logger::sendMessage(const char *data, SerialColor color) | ||||
| { | ||||
|     SerialCore::SetColor(color); | ||||
|     SerialCore::SendLine(data); | ||||
|     SerialCore::SetColor(WHITE); | ||||
| } | ||||
| 
 | ||||
| void Logger::Info(char *data, char *initiator) | ||||
| { | ||||
|     // TODO: check core and user level
 | ||||
|     if (UseColor) | ||||
|     { | ||||
|         Serial.write(27); | ||||
|         Serial.print("[37m"); // White color
 | ||||
|     } | ||||
|     Serial.println(getMessage(MessageType::INFO, data, initiator).c_str()); | ||||
|     if (UseColor) | ||||
|     { | ||||
|         Serial.write(27); | ||||
|         Serial.print("[37m"); | ||||
|     } | ||||
|     sendMessage(getMessage(MessageType::INFO, data, initiator).c_str(), WHITE); | ||||
| } | ||||
| 
 | ||||
| void Logger::Warn(char *data, char *initiator) | ||||
| { | ||||
|     // TODO: check core and user level
 | ||||
|     if (UseColor) | ||||
|     { | ||||
|         Serial.write(27); | ||||
|         Serial.print("[33m"); // Orange color
 | ||||
|     } | ||||
|     Serial.println(getMessage(MessageType::WARN, data, initiator).c_str()); | ||||
|     if (UseColor) | ||||
|     { | ||||
|         Serial.write(27); | ||||
|         Serial.print("[37m"); | ||||
|     } | ||||
|     sendMessage(getMessage(MessageType::WARN, data, initiator).c_str(), ORANGE); | ||||
| } | ||||
| 
 | ||||
| void Logger::Error(char *data, char *initiator) | ||||
| { | ||||
|     // TODO: check core and user level
 | ||||
|     if (UseColor) | ||||
|     { | ||||
|         Serial.write(27); | ||||
|         Serial.print("[31m"); // Red color
 | ||||
|     } | ||||
|     Serial.println(getMessage(MessageType::ERROR, data, initiator).c_str()); | ||||
|     if (UseColor) | ||||
|     { | ||||
|         Serial.write(27); | ||||
|         Serial.print("[37m"); | ||||
|     } | ||||
|     sendMessage(getMessage(MessageType::ERROR, data, initiator).c_str(), RED); | ||||
| } | ||||
| @ -1,25 +1,14 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| #include <string> | ||||
| #include "SerialColor.h" | ||||
| #include "MessageType.h" | ||||
| /*
 | ||||
|  * Definition of Logger instance. | ||||
|  * Writed on 14.06.2024 21:11 | ||||
|  * By Lisoveliy | ||||
|  */ | ||||
| 
 | ||||
| // Next briefs added for set logger statuses, not explaining logger work!
 | ||||
| /// @brief Type of messages for logger
 | ||||
| enum MessageType | ||||
| { | ||||
|     /// @brief For info, warn and error messages (default)
 | ||||
|     INFO, | ||||
|     /// @brief For warn and error messages
 | ||||
|     WARN, | ||||
|     /// @brief For only error messages
 | ||||
|     ERROR, | ||||
|     /// @brief For disable logger
 | ||||
|     NONE | ||||
| }; | ||||
| 
 | ||||
| /// @brief Logger instance for messages on Serial port
 | ||||
| class Logger | ||||
| { | ||||
| @ -30,6 +19,8 @@ private: | ||||
| 
 | ||||
|     const char *messageTypeToString(MessageType messageType); | ||||
|     std::string getMessage(MessageType messageType, char *text, char *initiator); | ||||
|     void sendMessage(char *output, SerialColor color); | ||||
|     void sendMessage(const char *output, SerialColor color); | ||||
| 
 | ||||
| public: | ||||
|     /// @brief Create instance of logger
 | ||||
|  | ||||
							
								
								
									
										15
									
								
								program/SVET/lib/SVET/Logger/MessageType.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								program/SVET/lib/SVET/Logger/MessageType.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| // Next briefs added for set logger statuses, not explaining logger work!
 | ||||
| /// @brief Type of messages for logger
 | ||||
| enum MessageType | ||||
| { | ||||
|     /// @brief For info, warn and error messages (default)
 | ||||
|     INFO, | ||||
|     /// @brief For warn and error messages
 | ||||
|     WARN, | ||||
|     /// @brief For only error messages
 | ||||
|     ERROR, | ||||
|     /// @brief For disable logger
 | ||||
|     NONE | ||||
| }; | ||||
							
								
								
									
										12
									
								
								program/SVET/lib/SVET/Logger/SerialColor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								program/SVET/lib/SVET/Logger/SerialColor.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| /// @brief Color of serial output
 | ||||
| enum SerialColor | ||||
| { | ||||
|     /// @brief White color
 | ||||
|     WHITE, | ||||
|     /// @brief Red color
 | ||||
|     RED, | ||||
|     /// @brief Orange color
 | ||||
|     ORANGE | ||||
| }; | ||||
							
								
								
									
										65
									
								
								program/SVET/lib/SVET/PerepheryCore/SerialCore.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								program/SVET/lib/SVET/PerepheryCore/SerialCore.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,65 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| #include "Logger/SerialColor.h" | ||||
| 
 | ||||
| /*
 | ||||
|  * Definition of SerialCore. | ||||
|  * Writed on 15.06.2024 14:11 | ||||
|  * By Lisoveliy | ||||
|  */ | ||||
| 
 | ||||
| namespace SerialCore | ||||
| { | ||||
|     void Start(short baudRate); | ||||
|     void Stop(); | ||||
|     void SendLine(char *data); | ||||
|     void SendLine(const char *data); | ||||
|     void SetColor(SerialColor color); | ||||
|     const char *GetEscapeColor(SerialColor color); | ||||
| }; | ||||
| 
 | ||||
| /*
 | ||||
|  * Implementation of SerialCore. | ||||
|  * Writed on 15.06.2024 14:12 | ||||
|  * By Lisoveliy | ||||
|  */ | ||||
| 
 | ||||
| void SerialCore::Start(short baudRate) | ||||
| { | ||||
|     Serial.begin(baudRate); | ||||
| } | ||||
| 
 | ||||
| void SerialCore::Stop() | ||||
| { | ||||
|     Serial.end(); | ||||
| } | ||||
| 
 | ||||
| void SerialCore::SendLine(char *data) | ||||
| { | ||||
|     Serial.println(data); | ||||
| } | ||||
| void SerialCore::SendLine(const char *data) | ||||
| { | ||||
|     Serial.println(data); | ||||
| } | ||||
| 
 | ||||
| void SerialCore::SetColor(SerialColor color) | ||||
| { | ||||
|     Serial.write(27); | ||||
|     Serial.print(GetEscapeColor(color)); // White color
 | ||||
| } | ||||
| 
 | ||||
| const char *SerialCore::GetEscapeColor(SerialColor color) | ||||
| { | ||||
|     switch (color) | ||||
|     { | ||||
|     case SerialColor::WHITE: | ||||
|         return "[37m"; | ||||
|     case SerialColor::ORANGE: | ||||
|         return "[33m"; | ||||
|     case SerialColor::RED: | ||||
|         return "[31m"; | ||||
|     default: | ||||
|         return ""; | ||||
|     } | ||||
| } | ||||
| @ -1,5 +1,6 @@ | ||||
| #pragma once | ||||
| #include "SVETBuilder/SVETBuilder.h" | ||||
| #include "Logger/Logger.h" | ||||
| #include <memory> | ||||
| 
 | ||||
| /*
 | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| #pragma once | ||||
| #include "options/LoggerOptions/LoggerOptions.h" | ||||
| #include <memory> | ||||
| 
 | ||||
| /*
 | ||||
|  * Definition of SVETBuilder. | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| #pragma once | ||||
| #include <Logger/Logger.h> | ||||
| #include "Logger/MessageType.h" | ||||
| 
 | ||||
| /*
 | ||||
|  * Definition of Logger Options. | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| #include <Arduino.h> | ||||
| #include <memory> | ||||
| #include "SVETBuilder/SVETBuilder.h" | ||||
| #include "SVET.h" | ||||
| 
 | ||||
| std::unique_ptr<SVET> svet; | ||||
| @ -15,9 +14,9 @@ void setup() | ||||
| 
 | ||||
| void loop() | ||||
| { | ||||
|   logger->Info("Hello", "main.cpp"); | ||||
|   logger->Warn("Warn asd", "main.cpp"); | ||||
|   logger->Error("Warferr", "main.cpp"); | ||||
|   delay(1000); | ||||
|   logger->Info("Hello INFO", "main.cpp"); | ||||
|   logger->Warn("Hello WARN", "main.cpp"); | ||||
|   logger->Error("Hello ERROR", "main.cpp"); | ||||
|   svet->Loop(); | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Lisoveliy
						Lisoveliy