rusEFI
The most advanced open source ECU
datalogging.h
Go to the documentation of this file.
1 /**
2  * @file datalogging.h
3  * @brief Buffered console output stream header
4  *
5  * @date Feb 25, 2013
6  * @author Andrey Belomutskiy, (c) 2012-2020
7  */
8 
9 #pragma once
10 
11 #include <cstdarg>
12 #include <cstdint>
13 #include <cstddef>
14 
15 // todo: migrate to external buffer so that different instances have different
16 // size of buffers?
17 class Logging {
18 public:
19  Logging() = delete;
20  Logging(const char *name, char *buffer, int bufferSize);
21 
22  void reset();
23 
24  void append(const char *text);
25  void appendFast(const char *text);
26  void appendPrintf(const char *fmt, ...);
27  void appendFloat(float value, int precision);
28 
29  void terminate() {
30  linePointer[0] = '\0';
31  }
32 
33  /**
34  * This macro breaks the normal zero=termination constraint, please take care of this outside of this function
35  */
36  void appendChar(char c) {
37  *linePointer = c;
38  linePointer++;
39  }
40 
41  size_t loggingSize() const {
42  return (uintptr_t)linePointer - (uintptr_t)buffer;
43  }
44 
45  size_t remainingSize() const {
46  return bufferSize - loggingSize();
47  }
48 
49 //private:
50  bool validateBuffer(uint32_t extraLen);
51 
52  const char* const name = nullptr;
53 
54  /**
55  * Zero-terminated buffer of pending debug message
56  *
57  * Unless a larger external buffer is specified, this is just a pointer to DEFAULT_BUFFER
58  */
59  char* const buffer = nullptr;
60  const int bufferSize = 0;
61 
62  /**
63  * This pointer is always pointing at the position within the buffer into which next
64  * write operation would append additional data
65  */
66  char *linePointer = nullptr;
67 };
68 
69 class LoggingWithStorage : public Logging {
70 public:
71  explicit LoggingWithStorage(const char *name);
72  char DEFAULT_BUFFER[100];
73 };
74 
75 void initLoggingExt(Logging *logging, const char *name, char *buffer, int bufferSize);
void appendFast(const char *text)
Definition: datalogging.cpp:73
void appendChar(char c)
Definition: datalogging.h:36
const char *const name
Definition: datalogging.h:52
void appendFloat(float value, int precision)
Definition: datalogging.cpp:97
size_t remainingSize() const
Definition: datalogging.h:45
void appendPrintf(const char *fmt,...)
Definition: datalogging.cpp:80
void terminate()
Definition: datalogging.h:29
size_t loggingSize() const
Definition: datalogging.h:41
void reset()
char * linePointer
Definition: datalogging.h:66
char *const buffer
Definition: datalogging.h:59
Logging()=delete
const int bufferSize
Definition: datalogging.h:60
LoggingWithStorage(const char *name)
char DEFAULT_BUFFER[100]
Definition: datalogging.h:72
void initLoggingExt(Logging *logging, const char *name, char *buffer, int bufferSize)
static BigBufferHandle buffer