Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
fastlog.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_UTILS_COMMANDLINE
7#define ELEMENTA_UTILS_COMMANDLINE
8
9#include <chrono>
10#include "elementa/base/serial_channels.h"
13
14
15namespace elementa
16{
17
18namespace utils
19{
20
39/* **********************************************************************
40
41 Template class: FastLog
42
43*************************************************************************/
44
46
47template <typename Data>
49{
50 public:
51
53 struct Datum
54 {
55 std::chrono::time_point<std::chrono::steady_clock> moment;
56 std::thread::id author;
57 Data data;
58 };
59
61 FastLog(size_t capacity):log_{capacity} {}
62
63
65 void clear(void);
66
68 bool empty(void) const;
69
71 size_t size(void) const;
72
74 void add(const Data & data);
75
77 void extract(Datum & ld);
78
79
80 private:
81
82 std::mutex logmtx_;
84};
85
86
87
88/* ===========================================================================
89
90 ========== IMPLEMENTATION OF TEMPLATES ==========
91
92============================================================================= */
93
94
95/* ***************************************************************************
96
97 Template class: FastLog
98
99*******************************************************************************/
100
101template <typename Data>
103{
104 std::unique_lock<std::mutex> lock(logmtx_);
105
106 log_.clear();
107}
108
109template <typename Data>
110bool FastLog<Data>::empty(void) const
111{
112 std::unique_lock<std::mutex> lock(const_cast<std::mutex &>(logmtx_));
113
114 return(log_.empty());
115}
116
117template <typename Data>
118size_t FastLog<Data>::size(void) const
119{
120 std::unique_lock<std::mutex> lock(const_cast<std::mutex &>(logmtx_));
121
122 return(log_.size());
123}
124
125template <typename Data>
126void FastLog<Data>::add(const Data & data)
127{
128 std::unique_lock<std::mutex> lock(logmtx_);
129
130 log_.push_back({std::chrono::steady_clock::now(),
131 std::this_thread::get_id(),
132 data});
133}
134
135
136template <typename Data>
138{
139 std::unique_lock<std::mutex> lock(logmtx_);
140
141 ld = log_.front();
142 log_.pop_front();
143}
144
145 // FastLog
147
148} // End os namespace
149
150} // End elementa namespace
151
152
153#endif
154
A circular buffer with fixed size, contiguous storage and generic elements.
Data data
User logged data.
Definition: fastlog.h:57
std::chrono::time_point< std::chrono::steady_clock > moment
When.
Definition: fastlog.h:55
std::thread::id author
Who.
Definition: fastlog.h:56
FastLog(size_t capacity)
Create an empty log with the given capacity.
Definition: fastlog.h:61
A thread-safe circular buffer in memory to store logs.
Definition: fastlog.h:49
bool empty(void) const
Return true if the log is empty.
Definition: fastlog.h:110
void extract(Datum &ld)
Extract the oldest datum or throw if none.
Definition: fastlog.h:137
void add(const Data &data)
Add a log datum.
Definition: fastlog.h:126
void clear(void)
Clear the log.
Definition: fastlog.h:102
size_t size(void) const
Return the number of logged items.
Definition: fastlog.h:118
Actual data stored in the log.
Definition: fastlog.h:54