acc_integration_log.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2019-2024
2 // All rights reserved
3 // This file is subject to the terms and conditions defined in the file
4 // 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
5 // of this source code package.
6 
7 #include <stdarg.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 
12 #include "acc_definitions_common.h"
13 #include "acc_integration.h"
14 #include "acc_integration_log.h"
15 
16 #define LOG_BUFFER_MAX_SIZE 150
17 
18 #define LOG_FORMAT "%02u:%02u:%02u.%03u (%c) (%s) %s\n"
19 
20 void acc_integration_log(acc_log_level_t level, const char *module, const char *format, ...)
21 {
22  char log_buffer[LOG_BUFFER_MAX_SIZE];
23  va_list ap;
24 
25  va_start(ap, format);
26 
27  int ret = vsnprintf(log_buffer, LOG_BUFFER_MAX_SIZE, format, ap);
28 
29  if (ret >= LOG_BUFFER_MAX_SIZE)
30  {
31  log_buffer[LOG_BUFFER_MAX_SIZE - 4] = '.';
32  log_buffer[LOG_BUFFER_MAX_SIZE - 3] = '.';
33  log_buffer[LOG_BUFFER_MAX_SIZE - 2] = '.';
34  log_buffer[LOG_BUFFER_MAX_SIZE - 1] = 0;
35  }
36 
37  uint32_t time_ms = acc_integration_get_time();
38  char level_ch;
39 
40  unsigned int timestamp = time_ms;
41  unsigned int hours = timestamp / 1000 / 60 / 60;
42  unsigned int minutes = timestamp / 1000 / 60 % 60;
43  unsigned int seconds = timestamp / 1000 % 60;
44  unsigned int milliseconds = timestamp % 1000;
45 
46  level_ch = (level <= ACC_LOG_LEVEL_DEBUG) ? "EWIVD"[level] : '?';
47 
48  printf(LOG_FORMAT, hours, minutes, seconds, milliseconds, level_ch, module, log_buffer);
49 
50  va_end(ap);
51 }
acc_integration_get_time
uint32_t acc_integration_get_time(void)
Get current time.
Definition: acc_integration_stm32.c:587
acc_integration_log
void acc_integration_log(acc_log_level_t level, const char *module, const char *format,...)
Log function.
Definition: acc_integration_log.c:20
vsnprintf
#define vsnprintf
Definition: printf.h:85
ACC_LOG_LEVEL_DEBUG
@ ACC_LOG_LEVEL_DEBUG
Definition: acc_definitions_common.h:34
LOG_FORMAT
#define LOG_FORMAT
Definition: acc_integration_log.c:18
acc_integration.h
acc_log_level_t
acc_log_level_t
This enum represents the different log levels for RSS.
Definition: acc_definitions_common.h:23
printf
#define printf
Definition: printf.h:60
LOG_BUFFER_MAX_SIZE
#define LOG_BUFFER_MAX_SIZE
Definition: acc_integration_log.c:16
acc_integration_log.h
acc_definitions_common.h