Motion Master
Loading...
Searching...
No Matches
global.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef WINDOWS_BUILD
4#include <windows.h>
5#endif
6#include <atomic>
7#include <csignal>
8#include <iostream>
9#include <map>
10#include <mutex>
11#include <queue>
12#include <string>
13#include <thread>
14#include <unordered_map>
15#include <vector>
16
17#include "ethercat.h"
18#include "loguru.h"
20
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
26
27extern uint8 g_pIOmap[4096];
28
29// SIGINT or SIGTERM caught
30extern volatile sig_atomic_t g_sig_caught;
31
34
35extern uint16_t g_req_res_port;
36extern uint16_t g_pub_sub_port;
37
38// Using SOEM
39extern std::string g_soem_mac_address;
40extern std::string g_soem_ifname;
41extern std::vector<std::mutex> g_soem_mailbox_mutexes;
42extern std::atomic<int> g_soem_slavecount;
43extern std::atomic<bool> g_pdo_mapping_in_progress;
44
46
47// Number of mocked devices
48extern size_t g_mock_devices;
49
50// Path to a directory holding the templates for mock devices
51extern std::string g_templates_path;
52
58 public:
59 private:
60 static std::map<std::string, std::unique_ptr<InternalLogger>> self_map;
62 constexpr static std::uint32_t internal_log_size = 0x200000;
63 std::mutex internal_log_mutex;
65 uint32_t type;
66 std::string name;
67
68 public:
69 std::string g_internal_log;
72 std::vector<std::string> logPreVec;
74 std::vector<std::string> logMsgVec;
76 std::vector<std::string> logIndVec;
79 std::deque<std::string> assertEqStrQue;
80 std::deque<void*> assertEqUserDataQue;
82 bool m_pop;
84 void (*assertEqCallback)(const std::string& pre, const std::string& str1,
85 const std::string& str2, void* userData,
86 int& retval);
87
89 static InternalLogger* getInstance(const std::string& name) {
90 if (InternalLogger::self_map.find(name) == InternalLogger::self_map.end()) {
91 InternalLogger::self_map.insert(
92 {name, std::make_unique<InternalLogger>()});
93 InternalLogger::self_map.at(name).get()->name = name;
94 InternalLogger::self_map.at(name).get()->type = 0;
95 InternalLogger::self_map.at(name).get()->assertEqCallback = nullptr;
96 InternalLogger::self_map.at(name).get()->m_pop = true;
97 }
98 return InternalLogger::self_map.at(name).get();
99 }
100
101 static void setType(const std::string& name, uint32_t type) {
102 InternalLogger::self_map.at(name).get()->type = type;
103 }
104
115 void log(const loguru::Message& message) {
116 std::unique_lock<std::mutex> lk(internal_log_mutex);
117 if (type == 0) {
118 g_internal_log += message.preamble;
119 g_internal_log += message.indentation;
120 g_internal_log += message.message;
121 g_internal_log += "\n";
122 if (g_internal_log.size() > internal_log_size) {
123 // Erase the first 12.5% of the internal log
124 auto pos = g_internal_log.find_first_of('\n', internal_log_size >> 3u);
125 g_internal_log.erase(0, pos + 1);
126 }
127 } else if (type == 1) {
128 logPreVec.push_back(message.preamble);
129 logMsgVec.push_back(message.message);
130 logIndVec.push_back(message.indentation);
131 if (!assertEqStrQue.empty()) {
132 if (assertEqCallback != nullptr) {
133 size_t loopSize = assertEqStrQue.size();
134 for (size_t i = 0; i < loopSize; i++) {
135 int retval = 0;
136 (*assertEqCallback)(logPreVec.back(), logMsgVec.back(),
138 retval);
139 if (retval != 0) {
140 assertEqStrQue.clear();
141 assertEqUserDataQue.clear();
142 break;
143 }
144 if (m_pop) {
145 assertEqStrQue.pop_front();
146 assertEqUserDataQue.pop_front();
147 }
148 }
149 }
150 }
151 }
152 }
153
156 static std::string& get_log(const std::string& name) {
158 std::unique_lock<std::mutex> lk(pil->internal_log_mutex);
160 return pil->g_internal_log_copy;
161 }
162
164 static void log_to_internal_log(void* user_data,
165 const loguru::Message& message) {
166 reinterpret_cast<InternalLogger*>(user_data)->log(message);
167 }
168
170 void setAssertEqCallback(void (*func)(const std::string& pre,
171 const std::string& str1,
172 const std::string& str2, void* userData,
173 int& retval)) {
174 assertEqCallback = func;
175 }
176
178 void pushAssertEq(const std::string& str, void* userData = nullptr) {
179 assertEqStrQue.push_front(str);
180 assertEqUserDataQue.push_front(userData);
181 }
182
183 void setPop() { m_pop = true; }
184 void clearPop() { m_pop = false; }
185
186 void resetState() {
187 g_internal_log.clear();
188 g_internal_log_copy.clear();
189 logPreVec.clear();
190 logMsgVec.clear();
191 logIndVec.clear();
192 assertEqStrQue.clear();
193 assertEqUserDataQue.clear();
194 }
195};
196
197// Motion Master running in end of the line testing mode
198extern bool g_eol_mode;
Manages communication and control for an external autotuning process.
Definition: standalone_autotuning.h:239
uint8_t type
Definition: co_dictionary.h:1
const char ** name
Definition: co_dictionary.h:7
bool g_eol_mode
Definition: global.cc:34
MasterType
Definition: global.h:32
std::string g_soem_mac_address
Definition: global.cc:25
uint16_t g_pub_sub_port
Definition: global.cc:23
uint8 g_pIOmap[4096]
Definition: global.cc:16
size_t g_mock_devices
Definition: global.cc:31
std::vector< std::mutex > g_soem_mailbox_mutexes
Definition: global.cc:27
void g_soem_update_slavecount()
Definition: global.cc:38
std::string g_templates_path
Definition: global.cc:32
volatile sig_atomic_t g_sig_caught
Definition: global.cc:18
std::atomic< bool > g_pdo_mapping_in_progress
Definition: global.cc:29
std::atomic< int > g_soem_slavecount
Definition: global.cc:28
MasterType g_master_type
Definition: global.cc:20
uint16_t g_req_res_port
Definition: global.cc:22
std::string g_soem_ifname
Definition: global.cc:26
StandaloneAutotuning g_standalone_autotuning
Logging class.
Definition: global.h:57
void setAssertEqCallback(void(*func)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval))
Definition: global.h:170
static void log_to_internal_log(void *user_data, const loguru::Message &message)
Definition: global.h:164
bool m_pop
Definition: global.h:82
void log(const loguru::Message &message)
Callback from loguru.
Definition: global.h:115
void clearPop()
Definition: global.h:184
std::deque< void * > assertEqUserDataQue
Definition: global.h:80
std::vector< std::string > logPreVec
Definition: global.h:72
std::deque< std::string > assertEqStrQue
Definition: global.h:79
void resetState()
Definition: global.h:186
std::string g_internal_log_copy
Definition: global.h:70
static std::string & get_log(const std::string &name)
Definition: global.h:156
static void setType(const std::string &name, uint32_t type)
Definition: global.h:101
void(* assertEqCallback)(const std::string &pre, const std::string &str1, const std::string &str2, void *userData, int &retval)
Definition: global.h:84
std::vector< std::string > logIndVec
Definition: global.h:76
static InternalLogger * getInstance(const std::string &name)
Definition: global.h:89
std::vector< std::string > logMsgVec
Definition: global.h:74
std::string g_internal_log
Definition: global.h:69
void setPop()
Definition: global.h:183
void pushAssertEq(const std::string &str, void *userData=nullptr)
Definition: global.h:178