This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
u_log_ram.h
146 lines (122 loc) · 4.34 KB
/
u_log_ram.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _U_LOG_RAM_H_
#define _U_LOG_RAM_H_
/* Only header files representing a direct and unavoidable
* dependency between the API of this module and the API
* of another module should be included here; otherwise
* please keep #includes to your .c files. */
#include "stdint.h"
#include "stdbool.h"
#include "u_log_ram_enum.h"
/** @file
* @brief This logging utility allows events to be logged to RAM at minimal
* run-time cost. Each entry includes an event, a 32 bit parameter (which
* is printed with the event) and a millisecond time-stamp. This code
* is not multithreaded in that there can only be a single log buffer
* at any one time, however the functions, aside from uLogRam(),
* are mutex-protected.
*/
#ifdef __cplusplus
extern "C" {
#endif
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/** The number of log entries (must be 1 or greater).
*/
#ifndef U_LOG_RAM_ENTRIES_MAX_NUM
# define U_LOG_RAM_ENTRIES_MAX_NUM 500
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** An entry in the log.
*/
typedef struct {
int32_t timestamp;
uint32_t event; // This will be #uLogRamEvent_t but it is stored as an int
// so that we are guaranteed to get a 32-bit value,
// making it easier to decode logs on another platform
int32_t parameter;
} uLogRamEntry_t;
/** Type used to store logging context data.
*/
typedef struct {
uint32_t magicWord;
int32_t version;
uLogRamEntry_t *pLog;
uLogRamEntry_t *pLogNextEmpty;
uLogRamEntry_t const *pLogFirstFull;
size_t numLogItems;
size_t logEntriesOverwritten;
int32_t lastLogTime;
} uLogRamContext_t;
/** The size of the log store, given the number of entries requested.
*/
#define U_LOG_RAM_STORE_SIZE (sizeof(uLogRamContext_t) + (sizeof(uLogRamEntry_t) * U_LOG_RAM_ENTRIES_MAX_NUM))
/* ----------------------------------------------------------------
* FUNCTIONS
* -------------------------------------------------------------- */
/** Initialise RAM logging.
*
* @param pBuffer must point to #U_LOG_RAM_STORE_SIZE bytes
* of storage. If pBuffer is in RAM which is not
* initialised at a reset then logging to RAM will
* also survive across a reset. If pBuffer
* is NULL then memory will be allocated for the
* log and will be free'ed on deinitialisation.
* @return true if successful, else false.
*/
bool uLogRamInit(void *pBuffer);
/** Close down logging.
*/
void uLogRamDeinit();
/** Log an event plus parameter to RAM.
*
* @param event the event.
* @param parameter the parameter.
*/
void uLogRam(uLogRamEvent_t event, int32_t parameter);
/** Log an event plus parameter to RAM, employing a mutex to protect the
* log contents. This will take longer, potentially a lot longer,
* than uLogRam() so call this only in applications where you don't
* care about speed.
*
* @param event the event.
* @param parameter the parameter.
*/
void uLogRamX(uLogRamEvent_t event, int32_t parameter);
/** Get the first N log entries that are in RAM, removing
* them from the log storage.
*
* @param pEntries a pointer to the place to store the entries.
* @param numEntries the number of entries pointed to by pEntries.
* @return the number of entries returned.
*/
size_t uLogRamGet(uLogRamEntry_t *pEntries, size_t numEntries);
/** Get the number of log entries currently in RAM.
*/
size_t uLogRamGetNumEntries();
/** Print out the currently logged items.
*/
void uLogRamPrint();
#ifdef __cplusplus
}
#endif
/** @}*/
#endif // _U_LOG_RAM_H_
// End of file