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 mcp48x2
26 * @author Jason Duffy
27 * @date 1st 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 */
32
33#include <stdbool.h>
34
35#include "usart.h"
36#include "log_system.h"
37
38static const char *p_system_tag = "log_system";
39
40static bool log_system_enabled = false;
41
42// Forward declaration
43void print_tag_and_log_level(const char *p_tag, enum eLogLevel level);
44
45// Initialisation routine
47{
48 init_usart();
50 log_message(p_system_tag, INFO, "Log system initialised");
51};
52
53// Sends only a string message
54void log_message(const char *p_tag, enum eLogLevel level, const char *msg)
55{
56 print_tag_and_log_level(p_tag, level);
57 print_string(msg);
58}
59
60// Sends a string, followed by an integer (3 ASCII digits)
61void log_message_with_dec_val(const char *p_tag,
62 enum eLogLevel level,
63 const char *msg,
64 uint8_t val)
65{
66 print_tag_and_log_level(p_tag, level);
67 print_string(msg);
68 print_string(" ");
69 print_byte(val);
70}
71
72
73// Sends a string, followed by an integer (5 ASCII digits)
74void log_message_with_16bit_dec_val(const char *p_tag,
75 enum eLogLevel level,
76 const char *msg,
77 uint16_t val)
78{
79 print_tag_and_log_level(p_tag, level);
80 print_string(msg);
81 print_string(" ");
82 print_word(val);
83}
84
85
86// Sends a string, followed by a binary byte (in 1's and 0's)
87void log_message_with_bin_val(const char *p_tag,
88 enum eLogLevel level,
89 const char *msg,
90 uint8_t val)
91{
92 print_tag_and_log_level(p_tag, level);
93 print_string(msg);
94 print_string(" ");
96}
97
98// Same as the function above, but for 16bit values.
99void log_message_with_16bit_bin_val(const char *p_tag,
100 enum eLogLevel level,
101 const char *msg,
102 uint16_t val)
103{
104 print_tag_and_log_level(p_tag, level);
105 print_string(msg);
106 print_string(" ");
107
108 uint8_t msb = (val >> 8);
109 uint8_t lsb = val;
112}
113
114// Sends a string, followed by an integer
115void log_message_with_hex_val(const char *p_tag,
116 enum eLogLevel level,
117 const char *msg,
118 uint8_t val)
119{
120 print_tag_and_log_level(p_tag, level);
121 print_string(msg);
122 print_string(" ");
123 print_hex_byte(val);
124}
125
126// Sets level of logging required
127void log_set_output_level(const char *p_tag, enum eLogLevel level)
128{
129
130}
131
132// Turns logging system on globally
134{
135 log_system_enabled = true;
136}
137
138// Turns logging system off globally
140{
141 log_system_enabled = false;
142}
143
144
145// ------------------------------------------------------------------------- //
146// ----------------------- Private helper functions ------------------------ //
147// ------------------------------------------------------------------------- //
148
149
150// Utility function to print labels over serial.
151void print_tag_and_log_level(const char *p_tag, enum eLogLevel level)
152{
153 if (level == NONE)
154 {
155 break;
156 }
157 print_string("\n");
158 print_string(p_tag);
159
160 if (level == INFO)
161 {
162 print_string(", INFO: ");
163 }
164
165 else if (level == DEBUG)
166 {
167 print_string(", DEBUG: ");
168 }
169
170 else if (level == VERBOSE_DEBUG)
171 {
172 print_string(", VERBOSE_DEBUG: ");
173 }
174
175 else if (level == WARNING)
176 {
177 print_string(", WARNING: ");
178 }
179
180 else if (level == ERROR)
181 {
182 print_string(", ERROR: ");
183 }
184
185 // TODO: Throw exception here to be caught at compile time
186 else
187 {
188 print_string(", INVALID_LOG_LEVEL: ");
189 }
190}
191
192/*** 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.