Branch data MC/DC data Line data Source code
1 : : : #include <stdint.h>
2 : : : // cppcheck-suppress misra-c2012-21.6
3 : : : #include <stdio.h>
4 : : : // cppcheck-suppress misra-c2012-21.10
5 : : : #include <time.h>
6 : : :
7 : : : #include "file_utils.h"
8 : : :
9 : : : #include "dtc_logger.h"
10 : : :
11 : : : #define LOG_FILE "dtc_log.txt"
12 : : :
13 : : : /**
14 : : : * @brief Log a Diagnostic Trouble Code (DTC) to a local text file with timestamp.
15 : : : *
16 : : : * This function appends the DTC identifier along with the current timestamp
17 : : : * to the file defined in LOG_FILE. Each log entry represents a failure
18 : : : * point detected by the REB system and can be used for
19 : : : * diagnostics.
20 : : : *
21 : : : * @param dtc_id The 32-bit identifier of the DTC to log.
22 : : : *
23 : : : * @return None.
24 : : : * @requir{SwHLR_F_16}
25 : : : */
26 : : :
27 : : 26 : void log_dtc(uint32_t dtc_id)
28 : : : {
29 : : 26 : int ret = 0;
30 : : 26 : FILE *fp = file_fopen(LOG_FILE, "a");
31 [ + + ]: [ T F ]: 26 : if (fp == NULL)
32 : : : {
33 : : 1 : perror("log_dtc fopen failed.\n");
34 : : 1 : ret = 1;
35 : : : }
36 : : :
37 [ + + ]: [ T F ]: 26 : if (ret == 0)
38 : : : {
39 : : 25 : time_t now = time(NULL);
40 : : : struct tm t;
41 [ + + ]: [ T F ]: 25 : if (file_localtime_r(&now, &t) == NULL)
42 : : : {
43 : : 1 : perror("log_dtc localtime_r failed.\n");
44 : : 1 : ret = 1;
45 : : : }
46 : : :
47 [ + + ]: [ T F ]: 25 : if (ret == 0)
48 : : : {
49 : : :
50 [ + + ]: [ T F ]: 24 : if (file_fprintf(fp, "[%04d-%02d-%02d %02d:%02d:%02d] DTC 0x%08X\n", t.tm_year + 1900,
51 : : 24 : t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, dtc_id) < 0)
52 : : : {
53 : : 1 : perror("log_dtc fprintf failed.\n");
54 : : : }
55 : : : }
56 [ + + ]: [ T F ]: 25 : if (file_fclose(fp) != 0)
57 : : : {
58 : : 1 : perror("log_dtc fclose failed.\n");
59 : : : }
60 : : : }
61 : : 26 : }
|