Repository navigation
Expand file tree
/
Copy pathlogging.c
More file actions
139 lines (109 loc) · 3.26 KB
/
Copy pathlogging.c
File metadata and controls
139 lines (109 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
*
* Microvisor C Demos
*
* Copyright © 2024, KORE Wireless
* Licence: MIT
*
*/
#include "main.h"
/*
* STATIC PROTOTYPES
*/
static void log_start(void);
static void log_service_setup(void);
static void post_log(bool is_err, const char* format_string, va_list args);
/*
* GLOBALS
*/
// Entities for Microvisor application logging
static uint8_t log_buffer[LOG_BUFFER_SIZE_B] __attribute__((aligned(512))) = {0};
static uint32_t log_state = USER_HANDLE_LOGGING_OFF;
// Entities for local serial logging
static bool uart_available = false;
// Declared in `uart_logging.c`
extern UART_HandleTypeDef uart;
/**
* @brief Open a logging channel.
*
* Open a data channel for Microvisor logging.
* This call will also request a network connection.
*/
static void log_start(void) {
if (log_state != USER_HANDLE_LOGGING_STARTED) {
// Initiate the Microvisor logging service
log_service_setup();
#if ENABLE_UART_DEBUGGING == true
// Establish UART logging
uart_available = log_uart_init();
#endif
}
}
/**
* @brief Initiate Microvisor application logging.
*/
static void log_service_setup(void) {
// Initialize logging with the standard system call
enum MvStatus status = mvServerLoggingInit(log_buffer, LOG_BUFFER_SIZE_B);
// Set a mock handle as a proxy for a 'logging enabled' flag
if (status == MV_STATUS_OKAY) log_state = USER_HANDLE_LOGGING_STARTED;
do_assert(status == MV_STATUS_OKAY, "Could not start logging");
}
/**
* @brief Issue a debug message.
*
* @param format_string Message string with optional formatting
* @param ... Optional injectable values
*/
void server_log(const char* format_string, ...) {
if (LOG_DEBUG_MESSAGES) {
va_list args;
va_start(args, format_string);
post_log(false, format_string, args);
va_end(args);
}
}
/**
* @brief Issue an error message.
*
* @param format_string Message string with optional formatting
* @param ... Optional injectable values
*/
void server_error(const char* format_string, ...) {
va_list args;
va_start(args, format_string);
post_log(true, format_string, args);
va_end(args);
}
/**
* @brief Issue any log message.
*
* @param is_err Is the message an error?
* @param format_string Message string with optional formatting
* @param args va_list of args from previous call
*/
static void post_log(bool is_err, const char* format_string, va_list args) {
static char buffer[LOG_MESSAGE_MAX_LEN_B] = {0};
// Initialize logging if we need to
log_start();
// Write the message type to the message
sprintf(buffer, is_err ? "[ERROR] " : "[DEBUG] ");
// Write the formatted text to the message
vsnprintf(&buffer[8], sizeof(buffer) - 9, format_string, args);
// Output the message using the system call
mvServerLog((const uint8_t*)buffer, (uint16_t)strlen(buffer));
// Do we output via UART too?
if (uart_available) log_uart_output(buffer);
}
/**
* @brief Wrapper for asserts so we get log output on fail.
*
* @param condition The condition to check.
* @param message The error message.
*/
void do_assert(bool condition, const char* message) {
if (!condition) {
server_error("%s", message);
assert(false);
}
}