• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PredatorCZ / PreCore / 460

pending completion
460

push

github-actions-ci

PredatorCZ
try fix coverage

3204 of 6095 relevant lines covered (52.57%)

354.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/src/master_printer.cpp
1
/*  a source for MasterPrinter class
2

3
    Copyright 2018-2021 Lukas Cone
4

5
    Licensed under the Apache License, Version 2.0 (the "License");
6
    you may not use this file except in compliance with the License.
7
    You may obtain a copy of the License at
8

9
        http://www.apache.org/licenses/LICENSE-2.0
10

11
    Unless required by applicable law or agreed to in writing, software
12
    distributed under the License is distributed on an "AS IS" BASIS,
13
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
    See the License for the specific language governing permissions and
15
    limitations under the License.
16
*/
17

18
#include "spike/master_printer.hpp"
19
#include "spike/type/flags.hpp"
20
#include <mutex>
21
#include <sstream>
22
#include <thread>
23
#include <vector>
24

25
using namespace es::print;
26

27
static struct MasterPrinter {
28
  struct FuncType {
29
    print_func func;
30
    bool useColor;
31

32
    FuncType(print_func fp, bool cl) : func(fp), useColor(cl) {}
8✔
33
  };
34

35
  std::vector<FuncType> functions;
36
  std::vector<queue_func> queues;
37
  std::stringstream buffer;
38
  std::mutex mutex;
39
  std::thread::id lockedThread;
40
  bool printThreadID = false;
41
  MPType cType = MPType::MSG;
42
} MASTER_PRINTER;
43

44
namespace es::print {
45

46
void AddPrinterFunction(print_func func, bool useColor) {
8✔
47
  for (auto &[func_, _] : MASTER_PRINTER.functions) {
8✔
48
    if (func_ == func) {
×
49
      return;
50
    }
51
  }
52
  MASTER_PRINTER.functions.emplace_back(func, useColor);
8✔
53
}
54

55
void AddQueuer(queue_func func) { MASTER_PRINTER.queues.push_back(func); }
×
56

57
std::ostream &Get(MPType type) {
107✔
58
  if (auto threadID = std::this_thread::get_id();
107✔
59
      !(MASTER_PRINTER.lockedThread == threadID)) {
60
    MASTER_PRINTER.mutex.lock();
107✔
61
    MASTER_PRINTER.lockedThread = threadID;
107✔
62
  }
63

64
  if (type != MPType::PREV) {
107✔
65
    MASTER_PRINTER.cType = type;
107✔
66
  }
67
  return MASTER_PRINTER.buffer;
107✔
68
}
69

70
void FlushAll() {
107✔
71
  Queuer que;
72
  que.payload = std::move(MASTER_PRINTER.buffer).str();
214✔
73
  std::thread::id threadID = std::this_thread::get_id();
74
  que.threadId = reinterpret_cast<uint32 &>(threadID);
107✔
75
  que.type = MASTER_PRINTER.cType;
107✔
76

77
  for (auto &[func, useColor] : MASTER_PRINTER.functions) {
321✔
78
    if (useColor) {
107✔
79
      if (que.type == MPType::WRN) {
107✔
80
        func("\033[38;2;255;255;0m");
40✔
81
      } else if (que.type == MPType::ERR) {
67✔
82
        func("\033[38;2;255;0;0m");
44✔
83
      } else if (que.type == MPType::INF) {
23✔
84
        func("\033[38;2;0;180;255m");
1✔
85
      }
86
    }
87

88
    if (MASTER_PRINTER.printThreadID) {
107✔
89
      func("Thread[0x");
3✔
90
      char buffer[65];
91
      snprintf(buffer, 65, "%X", que.threadId);
3✔
92
      func(buffer);
3✔
93
      func("] ");
3✔
94
    }
95

96
    if (que.type == MPType::WRN) {
107✔
97
      func("WARNING: ");
40✔
98
    } else if (que.type == MPType::ERR) {
67✔
99
      func("ERROR: ");
44✔
100
    }
101

102
    func(que.payload.data());
107✔
103

104
    if (useColor && que.type != MPType::MSG) {
107✔
105
      func("\033[0m");
85✔
106
    }
107
  }
108

109
  for (auto &q : MASTER_PRINTER.queues) {
107✔
110
    q(que);
×
111
  }
112

113
  MASTER_PRINTER.cType = MPType::MSG;
107✔
114
  MASTER_PRINTER.mutex.unlock();
115
  MASTER_PRINTER.lockedThread = {};
107✔
116
}
107✔
117

118
void PrintThreadID(bool yn) { MASTER_PRINTER.printThreadID = yn; }
2✔
119
} // namespace es::print
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc