AVRly - AVR Development Resources
log_system.c File Reference

Driver file providing logging functionality over USART, to print debug messages and values to a teminal program on your PC. More...

#include <stdbool.h>
#include "usart.h"
#include "log_system.h"

Go to the source code of this file.

Functions

void print_tag_and_log_level (const char *p_tag, log_type_t level)
 Utility function to print labels over serial. More...
 
bool log_message_preference_check (log_system_config_t *p_config, log_type_t level)
 Utility function to test if the log message level meets the preferences set. More...
 
void init_log_system (void)
 Initialisation routine - call this function once at startup before using other functions. More...
 
void log_message (log_system_config_t *p_config, log_type_t level, const char *msg)
 Sends only the system tag, log level and message string. More...
 
void log_message_with_dec_val (log_system_config_t *p_config, log_type_t level, const char *msg, uint8_t val)
 Sends a string, followed by an 8 bit value in decimal format. More...
 
void log_message_with_bin_val (log_system_config_t *p_config, log_type_t level, const char *msg, uint8_t val)
 Sends a string, followed by an 8 bit value in binary format. More...
 
void log_message_with_hex_val (log_system_config_t *p_config, log_type_t level, const char *msg, uint8_t val)
 Sends a string, followed by an 8 bit value in hexadecimal format. More...
 
void log_set_file_max_output_level (log_system_config_t *p_config, log_type_t level)
 Sets maximum output level of logging required, to be used at file scope. More...
 
void log_set_global_max_output_level (log_type_t level)
 Sets maximum output level of logging required, has global effect. More...
 
void log_global_on (void)
 Turns logging system on globally. More...
 
void log_global_off (void)
 Turns logging system off globally. More...
 

Variables

log_system_config_t log_system_log
 Instantiation of log system config object, pass it's address into logging functions. More...
 

Detailed Description

Driver file providing logging functionality over USART, to print debug messages and values to a teminal program on your PC.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author
Jason Duffy
Date
1st March 2022

Definition in file log_system.c.

Function Documentation

◆ print_tag_and_log_level()

void print_tag_and_log_level ( const char *  p_tag,
log_type_t  level 
)

Utility function to print labels over serial.

Definition at line 260 of file log_system.c.

261{
262 if (level == NONE)
263 {
264
265 }
266 usart_print_string("\n");
267 usart_print_string(p_tag);
268
269 if (level == ERROR)
270 {
271 usart_print_string(", ERROR: ");
272 }
273
274 else if (level == WARNING)
275 {
276 usart_print_string(", WARNING: ");
277 }
278
279 else if (level == INFO)
280 {
281 usart_print_string(", INFO: ");
282 }
283
284 else if (level == DEBUG)
285 {
286 usart_print_string(", DEBUG: ");
287 }
288
289 else if (level == VERBOSE_DEBUG)
290 {
291 usart_print_string(", VERBOSE_DEBUG: ");
292 }
293
294 else
295 {
296 usart_print_string(", INVALID_LOG_LEVEL: ");
297 }
298}
void usart_print_string(const char myString[])
Utility function to transmit a string.
Definition: usart.c:74

◆ log_message_preference_check()

bool log_message_preference_check ( log_system_config_t p_config,
log_type_t  level 
)

Utility function to test if the log message level meets the preferences set.

Definition at line 304 of file log_system.c.

306{
307 // Test if log_system is enabled globally, if not then do nothing.
308 if (log_system_enabled)
309 {
310 // Test if the level of this log message meets global log preferences.
311 if (level <= global_max_output_level)
312 {
313 // Test if the level of this log message meets file level preferences.
314 if (level <= p_config->file_log_level)
315 {
316 return true;
317 }
318 }
319 }
320 // If any of the above conditionals evaluate to false, return false.
321 return false;
322}

◆ init_log_system()

void init_log_system ( void  )

Initialisation routine - call this function once at startup before using other functions.

Log system will then be turned on by default - call log_global_off() if you do not wish it to be.

Definition at line 76 of file log_system.c.

77{
78 init_usart();
80 log_message(&log_system_log, INFO, "Log system initialised");
81};
void init_usart(void)
Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, configures the hardware USART ...
Definition: usart.c:50
void log_global_on(void)
Turns logging system on globally.
Definition: log_system.c:133
log_system_config_t log_system_log
Instantiation of log system config object, pass it's address into logging functions.
Definition: log_system.c:43
void log_message(log_system_config_t *p_config, log_type_t level, const char *msg)
Sends only the system tag, log level and message string.
Definition: log_system.c:93

◆ log_message()

void log_message ( log_system_config_t p_config,
log_type_t  level,
const char *  msg 
)

Sends only the system tag, log level and message string.

Parameters
p_configis a pointer to the log_system config object. Instantiate the config object at the head of each file where logging is required and pass it's address into this function.
levelis the level status of the log message - see log_type_t for available options.
msgis the message to be logged, enclose it in "" quotation marks.

Definition at line 93 of file log_system.c.

96{
97 // If all test expressions evaluate true, log message.
98 if (log_message_preference_check(p_config, level))
99 {
100 print_tag_and_log_level(p_config->p_system_tag, level);
102 }
103}
bool log_message_preference_check(log_system_config_t *p_config, log_type_t level)
Utility function to test if the log message level meets the preferences set.
Definition: log_system.c:304
void print_tag_and_log_level(const char *p_tag, log_type_t level)
Utility function to print labels over serial.
Definition: log_system.c:260

◆ log_message_with_dec_val()

void log_message_with_dec_val ( log_system_config_t p_config,
log_type_t  level,
const char *  msg,
uint8_t  val 
)

Sends a string, followed by an 8 bit value in decimal format.

Parameters
p_configis a pointer to the log_system config object. Instantiate the config object at the head of each file where logging is required and pass it's address into this function.
levelis the level status of the log message - see log_type_t for available options.
msgis the message to be logged, enclose it in "" quotation marks.
valis the numerical value to be logged - Acceptable values 0 - 255.

Definition at line 116 of file log_system.c.

120{
121 // If all test expressions evaluate true, log message.
122 if (log_message_preference_check(p_config, level))
123 {
124 print_tag_and_log_level(p_config->p_system_tag, level);
127 usart_print_byte(val);
128 }
129}
void usart_print_byte(uint8_t byte)
Prints a byte out as its 3-digit ascii equivalent.
Definition: usart.c:121

◆ log_message_with_bin_val()

void log_message_with_bin_val ( log_system_config_t p_config,
log_type_t  level,
const char *  msg,
uint8_t  val 
)

Sends a string, followed by an 8 bit value in binary format.

Parameters
p_configis a pointer to the log_system config object. Instantiate the config object at the head of each file where logging is required and pass it's address into this function.
levelis the level status of the log message - see log_type_t for available options.
msgis the message to be logged, enclose it in "" quotation marks.
valis the numerical value to be logged - Acceptable values 0 - 255.

Definition at line 167 of file log_system.c.

171{
172 // If all test expressions evaluate true, log message.
173 if (log_message_preference_check(p_config, level))
174 {
175 print_tag_and_log_level(p_config->p_system_tag, level);
179 }
180}
void usart_print_binary_byte(uint8_t byte)
Prints a byte out in 1s and 0s.
Definition: usart.c:169

◆ log_message_with_hex_val()

void log_message_with_hex_val ( log_system_config_t p_config,
log_type_t  level,
const char *  msg,
uint8_t  val 
)

Sends a string, followed by an 8 bit value in hexadecimal format.

Parameters
p_configis a pointer to the log_system config object. Instantiate the config object at the head of each file where logging is required and pass it's address into this function.
levelis the level status of the log message - see log_type_t for available options.
msgis the message to be logged, enclose it in "" quotation marks.
valis the numerical value to be logged - Acceptable values 0 - 255.

Definition at line 193 of file log_system.c.

197{
198 // If all test expressions evaluate true, log message.
199 if (log_message_preference_check(p_config, level))
200 {
201 print_tag_and_log_level(p_config->p_system_tag, level);
205 }
206}
void usart_print_hex_byte(uint8_t byte)
Prints a byte out in hexadecimal format.
Definition: usart.c:207

◆ log_set_file_max_output_level()

void log_set_file_max_output_level ( log_system_config_t p_config,
log_type_t  level 
)

Sets maximum output level of logging required, to be used at file scope.

Parameters
p_configis a pointer to the log_system config object. Instantiate the config object at the head of each file where logging is required and pass it's address into this function.
levelis the maximum level required - see log_type_t for available options.

Definition at line 217 of file log_system.c.

219{
220 p_config->file_log_level = level;
221}

◆ log_set_global_max_output_level()

void log_set_global_max_output_level ( log_type_t  level)

Sets maximum output level of logging required, has global effect.

Parameters
levelis the maximum level required - see log_type_t for available options.

Definition at line 229 of file log_system.c.

230{
231 global_max_output_level = level;
232}

◆ log_global_on()

void log_global_on ( void  )

Turns logging system on globally.

Definition at line 238 of file log_system.c.

239{
240 log_system_enabled = true;
241}

◆ log_global_off()

void log_global_off ( void  )

Turns logging system off globally.

Definition at line 247 of file log_system.c.

248{
249 log_system_enabled = false;
250}

Variable Documentation

◆ log_system_log

log_system_config_t log_system_log
Initial value:
=
{
.p_system_tag = "Log_System",
.file_log_level = INFO,
}

Instantiation of log system config object, pass it's address into logging functions.

Definition at line 43 of file log_system.c.