1
+ /* *
2
+ * Example to demonstate the speed of ArduinoSimpleLogging
3
+ *
4
+ * https://github.com/janLo/ArduinoSimpleLogging
5
+ *
6
+ * Copyright (c) 2017 Puuu
7
+ * MIT License
8
+ */
9
+
10
+ #include < ArduinoSimpleLogging.h>
11
+ #ifdef ESP8266
12
+ #include < ESP8266WiFi.h>
13
+ #endif
14
+
15
+ #define MSG " This is a test log message!"
16
+
17
+ const char msg[] = MSG;
18
+ const unsigned int repeats = 1000 ;
19
+
20
+ String msgString (MSG);
21
+
22
+ class PrintMock : public Print {
23
+ public:
24
+ unsigned calls;
25
+
26
+ size_t write (uint8_t ) override {
27
+ calls++;
28
+ return 1 ;
29
+ }
30
+
31
+ size_t write (const uint8_t *, size_t length) override {
32
+ calls++;
33
+ return length;
34
+ }
35
+
36
+ } printMock;
37
+
38
+ void log_char (Print &logger) {
39
+ for (unsigned int i = 0 ; i < repeats; i++) {
40
+ logger.println (msg);
41
+ }
42
+ }
43
+
44
+ void log_singlechar (Print &logger) {
45
+ for (unsigned int i = 0 ; i < repeats; i++) {
46
+ for (unsigned int c = 0 ; c < sizeof (msg)-1 ; c++) {
47
+ logger.write ((uint8_t )msg[c]);
48
+ }
49
+ logger.write ((uint8_t )' \n ' );
50
+ }
51
+ }
52
+
53
+ void log_progmem (Print &logger) {
54
+ for (unsigned int i = 0 ; i < repeats; i++) {
55
+ logger.println (F (MSG));
56
+ }
57
+ }
58
+
59
+ void log_String (Print &logger) {
60
+ for (unsigned int i = 0 ; i < repeats; i++) {
61
+ logger.println (msgString);
62
+ }
63
+ }
64
+
65
+ unsigned long measure_it (Print &logger, void (*func)(Print &)) {
66
+ unsigned long start = micros ();
67
+ func (logger);
68
+ unsigned long end = micros ();
69
+ return end - start;
70
+ }
71
+
72
+ void summarize (unsigned long duration) {
73
+ Serial.print (" duration " );
74
+ Serial.print (duration * 1000 / repeats);
75
+ Serial.print (" us, " );
76
+ Serial.print (printMock.calls / repeats);
77
+ Serial.println (" calls" );
78
+ }
79
+
80
+ void log (Print &logger) {
81
+ unsigned long duration;
82
+ Serial.println (" char string" );
83
+ printMock.calls = 0 ;
84
+ duration = measure_it (logger, log_char);
85
+ summarize (duration);
86
+
87
+ Serial.println (" single chars" );
88
+ printMock.calls = 0 ;
89
+ duration = measure_it (logger, log_singlechar);
90
+ summarize (duration);
91
+
92
+ Serial.println (" PROGMEM string" );
93
+ printMock.calls = 0 ;
94
+ duration = measure_it (logger, log_progmem);
95
+ summarize (duration);
96
+
97
+ Serial.println (" String object" );
98
+ printMock.calls = 0 ;
99
+ duration = measure_it (logger, log_String);
100
+ summarize (duration);
101
+ }
102
+
103
+ void setup () {
104
+ Serial.begin (115200 );
105
+
106
+ #ifdef ESP8266
107
+ // disable wifi for benchmarking
108
+ WiFi.mode (WIFI_OFF);
109
+ #endif
110
+
111
+ delay (500 );
112
+ Serial.println ();
113
+
114
+ // No handler configured - no output
115
+ Serial.println (" ------------------------" );
116
+ Serial.println (" No Handler ..." );
117
+ log (Logger.debug );
118
+ Serial.println (" ------------------------" );
119
+ Serial.println ();
120
+
121
+ // Handler for error, logging to debug
122
+ Serial.println (" ------------------------" );
123
+ Serial.println (" Handler for error, logging to debug ..." );
124
+ Logger.addHandler (Logger.ERROR , printMock);
125
+ log (Logger.debug );
126
+ Serial.println (" ------------------------" );
127
+ Serial.println ();
128
+
129
+ // Handler for debug, logging to debug
130
+ Serial.println (" ------------------------" );
131
+ Serial.println (" Handler for error, logging to error ..." );
132
+ log (Logger.error );
133
+ Serial.println (" ------------------------" );
134
+ Serial.println ();
135
+ }
136
+
137
+ void loop () {
138
+ // do nothing
139
+ }
0 commit comments