AVRly - AVR Development Resources
log_system.c
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file log_system.c
25 * @ingroup ccs811
26 * @author Jason Duffy
27 * @date 15th March 2022
28 * @brief Driver file providing logging functionality over USART, to print
29 * debug messages and values to a teminal program on your PC.
30 * @bug No known bugs.
31 * @see
32 */
33
34#include <stdbool.h>
35
36#include "usart.h"
37#include "log_system.h"
38
39static const char *p_system_tag = "log_system";
40
41static bool log_system_enabled = false;
42
43// Forward declaration
44void print_tag_and_log_level(const char *p_tag, enum eLogLevel level);
45
46// Initialisation routine
48{
49 init_usart();
51 log_message(p_system_tag, INFO, "Log system initialised");
52};
53
54// Sends only a string message
55void log_message(const char *p_tag, enum eLogLevel level, const char *msg)
56{
57 print_tag_and_log_level(p_tag, level);
58 print_string(msg);
59}
60
61// Sends a string, followed by an integer (3 ASCII digits)
62void log_message_with_dec_val(const char *p_tag, enum eLogLevel level, const char *msg, uint8_t val)
63{
64 print_tag_and_log_level(p_tag, level);
65 print_string(msg);
66 print_string(" ");
67 print_byte(val);
68}
69
70// Sends a string, followed by an integer (5 ASCII digits)
71void log_message_with_16bit_dec_val(const char *p_tag, enum eLogLevel level, const char *msg, uint16_t val)
72{
73 print_tag_and_log_level(p_tag, level);
74 print_string(msg);
75 print_string(" ");
76 print_word(val);
77}
78
79// Sends a string, followed by a binary byte (in 1's and 0's)
80void log_message_with_bin_val(const char *p_tag, enum eLogLevel level, const char *msg, uint8_t val)
81{
82 print_tag_and_log_level(p_tag, level);
83 print_string(msg);
84 print_string(" ");
86}
87
88// Sends a string, followed by an integer
89void log_message_with_hex_val(const char *p_tag, enum eLogLevel level, const char *msg, uint8_t val)
90{
91 print_tag_and_log_level(p_tag, level);
92 print_string(msg);
93 print_string(" ");
94 print_hex_byte(val);
95}
96
97// Sets level of logging required
98void log_set_output_level(const char *p_tag, enum eLogLevel level)
99{
100
101}
102
103// Turns logging system on globally
105{
106 log_system_enabled = true;
107}
108
109// Turns logging system off globally
111{
112 log_system_enabled = false;
113}
114
115// Utility function to print labels over serial.
116void print_tag_and_log_level(const char *p_tag, enum eLogLevel level)
117{
118 if (level == NONE)
119 {
120
121 }
122 print_string("\n");
123 print_string(p_tag);
124
125 if (level == INFO)
126 {
127 print_string(", INFO: ");
128 }
129
130 else if (level == DEBUG)
131 {
132 print_string(", DEBUG: ");
133 }
134
135 else if (level == VERBOSE_DEBUG)
136 {
137 print_string(", VERBOSE_DEBUG: ");
138 }
139
140 else if (level == WARNING)
141 {
142 print_string(", WARNING: ");
143 }
144
145 else if (level == ERROR)
146 {
147 print_string(", ERROR: ");
148 }
149
150 // TODO: Throw exception here to be caught at compile time
151 else
152 {
153 print_string(", INVALID_LOG_LEVEL: ");
154 }
155}
156
157/*** end of file ***/
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
void init_log_system(void)
Initialisation routine - call this function once at startup before using other functions.
Definition: log_system.c:46
void log_global_off(void)
Turns logging system off globally.
Definition: log_system.c:139
void print_word(uint16_t word)
Prints a word (16-bits) out as its 5-digit ascii equivalent.
Definition: usart.c:115
void print_string(const char myString[])
Utility function to transmit an entire string from RAM.
Definition: usart.c:67
void print_binary_byte(uint8_t byte)
Prints a byte out in 1s and 0s.
Definition: usart.c:125
void print_hex_byte(uint8_t byte)
Prints a byte out in hexadecimal.
Definition: usart.c:155
void print_byte(uint8_t byte)
Prints a byte out as its 3-digit ascii equivalent.
Definition: usart.c:106
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.
Definition: log_system.c:116
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
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.
Definition: log_system.c:193
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
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.
Definition: log_system.c:167
Driver file providing logging functionality over USART, to print debug messages and values to a temin...
Driver file providing core USART communication between the target MCU and your PC.