ruuvi.drivers.c ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Loading...
Searching...
No Matches
ruuvi_driver_error.c
Go to the documentation of this file.
1
14
15#include "ruuvi_driver_error.h"
16#include "ruuvi_interface_log.h"
17
18#include <stdio.h>
19#include <string.h>
20
22static rd_status_t m_errors = RD_SUCCESS;
23
25static rd_error_cb m_cb;
26
28 rd_status_t non_fatal_mask, const char * file, int line)
29{
30 // Do nothing on success
31 if (RD_SUCCESS == error) { return; }
32
33 m_errors |= error;
34 char message[RD_LOG_BUFFER_SIZE];
35 size_t index = 0;
36 bool fatal = ~non_fatal_mask & error;
37 // Cut out the full path
38 const char * filename = strrchr (file, '/');
39
40 // If on Windows
41 if (NULL == filename) { filename = strrchr (file, '\\'); }
42
43 // In case the file was already only the name
44 if (NULL == filename) { filename = file; }
45 // Otherwise skip the slash
46 else { filename++; }
47
48 // Reset on fatal error
49 if (fatal)
50 {
51 index += snprintf (message, sizeof (message), "%s:%d FATAL: ", filename, line);
52 index += ri_error_to_string (error, (message + index),
53 (sizeof (message) - index));
54 snprintf ( (message + index), (sizeof (message) - index), "\r\n");
56 ri_log (RI_LOG_LEVEL_ERROR, message);
58 }
59 // Log non-fatal errors
60 else if (RD_SUCCESS != error)
61 {
62 index += snprintf (message, sizeof (message), "%s:%d WARNING: ", filename, line);
63 index += ri_error_to_string (error, (message + index),
64 (sizeof (message) - index));
65 snprintf ( (message + index), (sizeof (message) - index), "\r\n");
67 }
68
69 // Call error callback
70 if (RD_SUCCESS != error && NULL != m_cb)
71 {
72 m_cb (error, fatal, filename, line);
73 }
74}
75
76/*
77 * @brief reset global error flags and return their value.
78 *
79 * @return errors occured after last call to this function.
80 */
82{
83 rd_status_t errors = m_errors;
84 m_errors = RD_SUCCESS;
85 return errors;
86}
87
89{
90 m_cb = cb;
91}
92
void rd_error_cb_set(rd_error_cb cb)
Configure application callback for errors.
uint32_t rd_status_t
bitfield for representing errors
void(* rd_error_cb)(const rd_status_t error, const bool fatal, const char *file, const int line)
void rd_error_check(rd_status_t error, rd_status_t non_fatal_mask, const char *file, int line)
Check given error code and compare it to non-fatal errors.
#define RD_SUCCESS
Internal Error.
rd_status_t rd_errors_clear()
void ri_log(const ri_log_severity_t severity, const char *const message)
Queues messages into log.
size_t ri_error_to_string(rd_status_t error, char *error_string, size_t space_remaining)
Write text description of error message into given string pointer and null-terminate it....
rd_status_t ri_log_flush(void)
Blocks until remaining log messages are sent out.
@ RI_LOG_LEVEL_ERROR
@ RI_LOG_LEVEL_WARNING
Header to enable and disable module compilation.
#define RD_LOG_BUFFER_SIZE
Maximum length of one log message.
Ruuvi error codes and error check function.