|
7 | 7 | //! is collected using the panics component, the logs will be decoded and displayed in the Memfault |
8 | 8 | //! cloud UI. |
9 | 9 |
|
10 | | -#include "memfault/core/log.h" |
11 | | -#include "memfault/core/log_impl.h" |
12 | | -#include "memfault_log_private.h" |
13 | | - |
14 | 10 | #include <stdarg.h> |
15 | 11 | #include <stddef.h> |
16 | 12 | #include <stdio.h> |
17 | 13 | #include <string.h> |
18 | 14 |
|
19 | 15 | #include "memfault/config.h" |
| 16 | +#include "memfault/core/compact_log_serializer.h" |
20 | 17 | #include "memfault/core/compiler.h" |
21 | 18 | #include "memfault/core/debug_log.h" |
| 19 | +#include "memfault/core/log.h" |
| 20 | +#include "memfault/core/log_impl.h" |
22 | 21 | #include "memfault/core/math.h" |
23 | 22 | #include "memfault/core/platform/debug_log.h" |
24 | 23 | #include "memfault/core/platform/overrides.h" |
| 24 | +#include "memfault/core/sdk_assert.h" |
| 25 | +#include "memfault/util/base64.h" |
25 | 26 | #include "memfault/util/circular_buffer.h" |
26 | 27 | #include "memfault/util/crc16_ccitt.h" |
27 | | -#include "memfault/core/compact_log_serializer.h" |
28 | | - |
29 | | -#include "memfault/config.h" |
| 28 | +#include "memfault_log_private.h" |
30 | 29 |
|
31 | 30 | #if MEMFAULT_LOG_DATA_SOURCE_ENABLED |
32 | 31 | #include "memfault_log_data_source_private.h" |
@@ -262,6 +261,61 @@ bool memfault_log_read(sMemfaultLog *log) { |
262 | 261 | return found_unread_log; |
263 | 262 | } |
264 | 263 |
|
| 264 | +MEMFAULT_WEAK void memfault_log_export_msg(MEMFAULT_UNUSED eMemfaultPlatformLogLevel level, |
| 265 | + const char *msg, MEMFAULT_UNUSED size_t msg_len) { |
| 266 | + memfault_platform_log_raw("%s", msg); |
| 267 | +} |
| 268 | + |
| 269 | +void memfault_log_export_log(sMemfaultLog *log) { |
| 270 | + MEMFAULT_SDK_ASSERT(log != NULL); |
| 271 | + |
| 272 | + char base64[MEMFAULT_LOG_EXPORT_BASE64_CHUNK_MAX_LEN]; |
| 273 | + size_t log_read_offset = 0; |
| 274 | + |
| 275 | + switch (log->type) { |
| 276 | + case kMemfaultLogRecordType_Compact: |
| 277 | + while (log_read_offset < log->msg_len) { |
| 278 | + memcpy(base64, MEMFAULT_LOG_EXPORT_BASE64_CHUNK_PREFIX, |
| 279 | + MEMFAULT_LOG_EXPORT_BASE64_CHUNK_PREFIX_LEN); |
| 280 | + |
| 281 | + size_t log_read_len = |
| 282 | + MEMFAULT_MIN(log->msg_len - log_read_offset, MEMFAULT_LOG_EXPORT_CHUNK_MAX_LEN); |
| 283 | + size_t write_offset = MEMFAULT_LOG_EXPORT_BASE64_CHUNK_PREFIX_LEN; |
| 284 | + |
| 285 | + memfault_base64_encode(&log->msg[log_read_offset], log_read_len, &base64[write_offset]); |
| 286 | + log_read_offset += log_read_len; |
| 287 | + write_offset += MEMFAULT_BASE64_ENCODE_LEN(log_read_len); |
| 288 | + |
| 289 | + memcpy(&base64[write_offset], MEMFAULT_LOG_EXPORT_BASE64_CHUNK_SUFFIX, |
| 290 | + MEMFAULT_LOG_EXPORT_BASE64_CHUNK_SUFFIX_LEN); |
| 291 | + write_offset += MEMFAULT_LOG_EXPORT_BASE64_CHUNK_SUFFIX_LEN; |
| 292 | + |
| 293 | + base64[write_offset] = '\0'; |
| 294 | + |
| 295 | + memfault_log_export_msg(log->level, base64, write_offset); |
| 296 | + } |
| 297 | + break; |
| 298 | + case kMemfaultLogRecordType_Preformatted: |
| 299 | + memfault_log_export_msg(log->level, log->msg, log->msg_len); |
| 300 | + break; |
| 301 | + case kMemfaultLogRecordType_NumTypes: // silences -Wswitch-enum |
| 302 | + default: |
| 303 | + break; |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +void memfault_log_export_logs(void) { |
| 308 | + while (1) { |
| 309 | + sMemfaultLog log = {0}; |
| 310 | + const bool log_found = memfault_log_read(&log); |
| 311 | + if (!log_found) { |
| 312 | + break; |
| 313 | + } |
| 314 | + |
| 315 | + memfault_log_export_log(&log); |
| 316 | + } |
| 317 | +} |
| 318 | + |
265 | 319 | static bool prv_should_log(eMemfaultPlatformLogLevel level) { |
266 | 320 | if (!s_memfault_ram_logger.enabled) { |
267 | 321 | return false; |
@@ -342,17 +396,13 @@ static void prv_log_save(eMemfaultPlatformLogLevel level, |
342 | 396 |
|
343 | 397 | #if MEMFAULT_COMPACT_LOG_ENABLE |
344 | 398 |
|
345 | | -static void prv_fill_compact_log_cb(void *ctx, uint32_t offset, const void *buf, size_t buf_len) { |
346 | | - uint8_t *log = (uint8_t *)ctx; |
347 | | - memcpy(&log[offset], buf, buf_len); |
348 | | -} |
349 | | - |
350 | 399 | void memfault_compact_log_save(eMemfaultPlatformLogLevel level, uint32_t log_id, |
351 | 400 | uint32_t compressed_fmt, ...) { |
352 | 401 | char log_buf[MEMFAULT_LOG_MAX_LINE_SAVE_LEN + 1]; |
353 | 402 |
|
354 | 403 | sMemfaultCborEncoder encoder; |
355 | | - memfault_cbor_encoder_init(&encoder, prv_fill_compact_log_cb, log_buf, sizeof(log_buf)); |
| 404 | + memfault_cbor_encoder_init(&encoder, memfault_cbor_encoder_memcpy_write, log_buf, |
| 405 | + sizeof(log_buf)); |
356 | 406 |
|
357 | 407 | va_list args; |
358 | 408 | va_start(args, compressed_fmt); |
|
0 commit comments