Skip to content

Commit c14ea35

Browse files
author
deepikabhavnani
committed
Unified logging system: String based only support
This logging system is designed to provide simple API's to log information from different modules on UART or other stream capable devices. ID based tracing is supported in which strings are replaces with ID's and host tool will be required to decode ID and print messages. Log Levels supported are: Critical, Error, Warning, Debug, Info and Trace. Circular buffer is used to save log data, which is then redirected to UART module. API's are thread safe and can be used in ISR context.
1 parent 964e6e7 commit c14ea35

File tree

6 files changed

+485
-2
lines changed

6 files changed

+485
-2
lines changed

mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "platform/mbed_interface.h"
4747
#include "platform/mbed_assert.h"
4848
#include "platform/mbed_debug.h"
49+
#include "platform/mbed_logger.h"
4950

5051
// mbed Peripheral components
5152
#include "drivers/DigitalIn.h"

platform/logger.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if defined(MBED_CONF_RTOS_PRESENT) || !defined(MBED_CONF_ZERO_BUFFER_LOGGING)
18+
19+
#include <string.h>
20+
#include "platform/mbed_logger.h"
21+
#include "platform/CircularBuffer.h"
22+
#include "platform/mbed_wait_api.h"
23+
#include "hal/ticker_api.h"
24+
#include "hal/us_ticker_api.h"
25+
#include "platform/mbed_critical.h"
26+
27+
using namespace mbed;
28+
29+
#if defined (MBED_ID_BASED_TRACING)
30+
CircularBuffer <uint32_t, (MBED_CONF_LOG_MAX_BUFFER_SIZE/4)> log_buffer;
31+
#else
32+
CircularBuffer <char, MBED_CONF_LOG_MAX_BUFFER_SIZE> log_buffer;
33+
#endif
34+
const ticker_data_t *const log_ticker = get_us_ticker_data();
35+
36+
#ifdef __cplusplus
37+
extern "C" {
38+
#endif
39+
40+
#if defined (MBED_ID_BASED_TRACING)
41+
void log_thread(void)
42+
{
43+
uint32_t data = 0;
44+
while (1) {
45+
while (!log_buffer.empty()) {
46+
log_buffer.pop(data);
47+
#if DEVICE_STDIO_MESSAGES && !defined(NDEBUG)
48+
printf("0x%x ", data);
49+
#endif
50+
}
51+
wait_ms(1);
52+
}
53+
}
54+
55+
// uint32_t time | uint32 (ID) | uint32 args ... | uint32_t checksum | 0
56+
void log_buffer_id_data(uint8_t argCount, ...)
57+
{
58+
volatile uint64_t time = ticker_read_us(log_ticker);
59+
uint32_t data = 0;
60+
uint32_t checksum = 0;
61+
62+
data = (uint32_t)(time/1000);
63+
log_buffer.push(data);
64+
checksum ^= data;
65+
66+
va_list args;
67+
va_start(args, argCount);
68+
for (uint8_t i = 0; i < argCount; i++) {
69+
data = va_arg(args, uint32_t);
70+
log_buffer.push(data);
71+
checksum ^= data;
72+
}
73+
log_buffer.push(checksum);
74+
log_buffer.push(0x0);
75+
va_end(args);
76+
}
77+
78+
#else
79+
void log_thread(void)
80+
{
81+
char data;
82+
while (1) {
83+
while (!log_buffer.empty()) {
84+
log_buffer.pop(data);
85+
#if DEVICE_STDIO_MESSAGES && !defined(NDEBUG)
86+
printf("%c", data);
87+
#endif
88+
}
89+
wait_ms(1);
90+
}
91+
}
92+
93+
void log_buffer_string_data(const char *format, ...)
94+
{
95+
volatile uint64_t time = ticker_read_us(log_ticker);
96+
char one_line[MBED_CONF_MAX_LOG_STR_SIZE];
97+
uint8_t count = snprintf(one_line, MBED_CONF_MAX_LOG_STR_SIZE, "%-8lld ", time);
98+
uint8_t bytes_written = 0;
99+
100+
va_list args;
101+
va_start(args, format);
102+
vsnprintf(one_line+count, (MBED_CONF_MAX_LOG_STR_SIZE-count), format, args);
103+
count = strlen(one_line);
104+
while (count) {
105+
log_buffer.push(one_line[bytes_written++]);
106+
count--;
107+
}
108+
va_end(args);
109+
}
110+
#endif
111+
112+
#ifdef __cplusplus
113+
}
114+
#endif
115+
116+
#endif

platform/logger.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/** \addtogroup platform */
18+
/** @{*/
19+
/**
20+
* \defgroup platform_log Logger functions
21+
* @{
22+
*/
23+
24+
#ifndef LOGGER_INTERNAL_H
25+
#define LOGGER_INTERNAL_H
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
#include <stdio.h>
32+
#include <stdarg.h>
33+
#include "platform/mbed_preprocessor.h"
34+
35+
#ifndef MBED_CONF_MAX_LOG_STR_SIZE
36+
#define MBED_CONF_MAX_LOG_STR_SIZE 128
37+
#endif
38+
39+
#ifndef MBED_CONF_LOG_MAX_BUFFER_SIZE
40+
#define MBED_CONF_LOG_MAX_BUFFER_SIZE 1024
41+
#endif
42+
43+
#define LOG_TOTAL_BUFFER_SIZE MBED_CONF_LOG_MAX_BUFFER_SIZE
44+
#define LOG_SINGLE_STR_SIZE MBED_CONF_MAX_LOG_STR_SIZE
45+
46+
#if defined(__ARMCC_VERSION)
47+
#define FILE_NAME_ __MODULE__
48+
#define FILE_INFO_ __MODULE__ " " MBED_STRINGIFY(__LINE__) " "
49+
#else
50+
#define FILE_NAME_ __BASE_FILE__
51+
#define FILE_INFO_ __BASE_FILE__ " " MBED_STRINGIFY(__LINE__) " "
52+
#endif
53+
54+
#define LOG_LEVEL_ERR_CRITICAL 0
55+
#define LOG_LEVEL_ERR 1
56+
#define LOG_LEVEL_WARN 2
57+
#define LOG_LEVEL_DEBUG 3
58+
#define LOG_LEVEL_INFO 4
59+
60+
// Log-Level Strings
61+
#define LOG_ERR_CRITICAL_ "CRT"
62+
#define LOG_ERR_ "ERR"
63+
#define LOG_WARN_ "WRN"
64+
#define LOG_DEBUG_ "DBG"
65+
#define LOG_INFO_ "INF"
66+
67+
#define GET_MODULE_ID_(a, b, c, d) (375 + (a << 1) + (b << 2) + (c >> 2) + (d >> 1))
68+
#define GET_MODULE_ID(x) (GET_MODULE_ID_(x[0], x[1], x[2], x[3]))
69+
70+
#define SET_MODULE(x) (GET_MODULE_ID(x) & 0xFF)
71+
#define SET_COUNTER(y) ((y & 0xFF) << 8)
72+
#define SET_LINE_NUM(z) ((z & 0xFFFF) << 16)
73+
#define TRACE_ID_(x,y,z) (SET_MODULE(x) | SET_COUNTER(y) | SET_LINE_NUM(z))
74+
75+
typedef struct trace_id {
76+
uint32_t t_id;
77+
uint32_t strLen;
78+
char *s;
79+
}trace_id_t;
80+
81+
// Macros to log ID based data
82+
#define MBED_LOG_ID_4(...) log_buffer_id_data(__VA_ARGS__)
83+
#define MBED_LOG_ID_3(counter, id, args, fmt, ...) ({volatile static const __attribute__((section(".keep.log_data"))) char str##counter[] = fmt; \
84+
volatile static const __attribute__((section(".keep.log_data"))) uint32_t len##counter = MBED_STRLEN(fmt); \
85+
volatile static const __attribute__((section(".keep.log_data"))) uint32_t c##counter = id; \
86+
MBED_LOG_ID_4(args, id, ##__VA_ARGS__); \
87+
})
88+
#define MBED_LOG_ID_2(counter, id, ...) MBED_LOG_ID_3(counter, id, MBED_COUNT_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__)
89+
#define MBED_LOG_ID_1(mod, fmt, ll, f, l, c, ...) MBED_LOG_ID_2(c, TRACE_ID_(mod,c,l), mod " " f " " MBED_STRINGIFY(l) " : " fmt "\n", ##__VA_ARGS__)
90+
91+
// Macros to log string data
92+
#define MBED_LOG_STR(...) log_buffer_string_data(__VA_ARGS__)
93+
#define MBED_LOG_STR_1(mod, fmt, ll, f, l, ...) MBED_LOG_STR("%-3.3s %-4.4s %-15s %5d : " fmt "\n", ll, mod, f, l, ##__VA_ARGS__)
94+
95+
void log_buffer_id_data(uint8_t argCount, ...);
96+
void log_buffer_string_data(const char *format, ...);
97+
98+
#ifdef __cplusplus
99+
}
100+
#endif
101+
102+
#endif // LOGGER_INTERNAL_H
103+
/**@}*/
104+
105+
/**@}*/

platform/logger_no_rtos.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if defined(MBED_CONF_ZERO_BUFFER_LOGGING) || !defined(MBED_CONF_RTOS_PRESENT)
18+
19+
#include "platform/mbed_logger.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
void log_buffer_string_data(const char *format, ...)
26+
{
27+
#if DEVICE_STDIO_MESSAGES && !defined(NDEBUG)
28+
va_list args;
29+
va_start(args, format);
30+
vfprintf(stderr, format, args);
31+
va_end(args);
32+
#endif
33+
}
34+
35+
void log_buffer_id_data(uint8_t argCount, ...)
36+
{
37+
#if DEVICE_STDIO_MESSAGES && !defined(NDEBUG)
38+
printf("%d ", argCount);
39+
va_list args;
40+
va_start(args, argCount);
41+
for (uint8_t i = 0; i < argCount; i++) {
42+
printf("%d ", va_arg(args, int));
43+
}
44+
printf("\n");
45+
va_end(args);
46+
#endif
47+
}
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
53+
#endif

0 commit comments

Comments
 (0)