AceRoutine  1.5.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
LogBinJsonRenderer.h
1 /*
2 MIT License
3 
4 Copyright (c) 2022 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_ROUTINE_LOG_BIN_JSON_RENDERER_H
26 #define ACE_ROUTINE_LOG_BIN_JSON_RENDERER_H
27 
28 #include <stdint.h> // uint8_t, uint32_t
29 #include <Arduino.h> // Print
30 #include "Coroutine.h" // Coroutine
31 #include "CoroutineProfiler.h"
32 #include "LogBinProfiler.h" // rollupExteriorBins()
33 
34 namespace ace_routine {
35 
47 template <typename T_COROUTINE>
49  public:
52 
64  static void printTo(
65  Print& printer,
66  uint8_t startBin,
67  uint8_t endBin,
68  bool clear = true,
69  bool rollup = true
70  ) {
71  uint16_t bufBins[Profiler::kNumBins];
72 
73  printer.println('{');
74  bool lineNeedsTrailingComma = false;
75  T_COROUTINE** root = T_COROUTINE::getRoot();
76  for (T_COROUTINE** p = root; (*p) != nullptr; p = (*p)->getNext()) {
77  auto* profiler = (Profiler*) (*p)->getProfiler();
78  if (! profiler) continue;
79 
80  // Roll up the exterior bins in to the first and last bins if requested.
81  const uint16_t* bins;
82  if (rollup) {
83  internal::rollupExteriorBins(
84  bufBins, profiler->mBins, Profiler::kNumBins, startBin, endBin);
85  bins = bufBins;
86  } else {
87  bins = profiler->mBins;
88  }
89 
90  if (lineNeedsTrailingComma) printer.println(',');
91  lineNeedsTrailingComma = true;
92 
93  printer.print('"');
94  (*p)->printNameTo(printer);
95  printer.print("\":[");
96 
97  bool elementNeedsTrailingComma = false;
98  for (uint8_t i = startBin; i < endBin; i++) {
99  if (elementNeedsTrailingComma) printer.print(',');
100  elementNeedsTrailingComma = true;
101  printer.print(bins[i]);
102  }
103  printer.print(']');
104 
105  if (clear) {
106  profiler->clear();
107  }
108  }
109  printer.println();
110  printer.println('}');
111  }
112 };
113 
114 using LogBinJsonRenderer = LogBinJsonRendererTemplate<Coroutine>;
115 
116 }
117 
118 #endif
ace_routine::LogBinJsonRendererTemplate
Print the LogBinProfiler bins as a JSON array.
Definition: LogBinJsonRenderer.h:48
ace_routine::LogBinJsonRendererTemplate::printTo
static void printTo(Print &printer, uint8_t startBin, uint8_t endBin, bool clear=true, bool rollup=true)
Loop over all coroutines and print the bin counts as JSON.
Definition: LogBinJsonRenderer.h:64
ace_routine::LogBinProfilerTemplate
Class that maintains the frequency count of the elapsed time of runCoroutine() in an array of bins wh...
Definition: LogBinProfiler.h:54
ace_routine::LogBinProfilerTemplate::kNumBins
static const uint8_t kNumBins
Number of event counter bins used by this class.
Definition: LogBinProfiler.h:57
Coroutine.h