22
22
23
23
#include " PowerPlant.hpp"
24
24
25
+ #include < exception>
26
+ #include < tuple>
27
+
28
+ #include " Reactor.hpp"
29
+ #include " dsl/store/DataStore.hpp"
30
+ #include " dsl/word/Shutdown.hpp"
31
+ #include " dsl/word/Startup.hpp"
32
+ #include " dsl/word/emit/Direct.hpp"
33
+ #include " message/CommandLineArguments.hpp"
34
+ #include " message/LogMessage.hpp"
35
+ #include " threading/ReactionTask.hpp"
36
+
25
37
namespace NUClear {
38
+ namespace util {
39
+ struct GroupDescriptor ;
40
+ struct ThreadPoolDescriptor ;
41
+ } // namespace util
26
42
27
43
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
28
44
PowerPlant* PowerPlant::powerplant = nullptr ;
29
45
46
+ // This is taking argc and argv as given by main so this should not take an array
47
+ // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
48
+ PowerPlant::PowerPlant (Configuration config, int argc, const char * argv[]) : scheduler(config.thread_count) {
49
+
50
+ // Stop people from making more then one powerplant
51
+ if (powerplant != nullptr ) {
52
+ throw std::runtime_error (" There is already a powerplant in existence (There should be a single PowerPlant)" );
53
+ }
54
+
55
+ // Store our static variable
56
+ powerplant = this ;
57
+
58
+ // Emit our arguments if any.
59
+ message::CommandLineArguments args;
60
+ for (int i = 0 ; i < argc; ++i) {
61
+ args.emplace_back (argv[i]);
62
+ }
63
+
64
+ // Emit our command line arguments
65
+ emit (std::make_unique<message::CommandLineArguments>(args));
66
+ }
67
+
30
68
PowerPlant::~PowerPlant () {
31
69
// Make sure reactors are destroyed before anything else
32
70
while (!reactors.empty ()) {
@@ -50,6 +88,16 @@ void PowerPlant::start() {
50
88
scheduler.start ();
51
89
}
52
90
91
+ void PowerPlant::add_idle_task (const NUClear::id_t & id,
92
+ const util::ThreadPoolDescriptor& pool_descriptor,
93
+ std::function<void ()>&& task) {
94
+ scheduler.add_idle_task (id, pool_descriptor, std::move (task));
95
+ }
96
+
97
+ void PowerPlant::remove_idle_task (const NUClear::id_t & id, const util::ThreadPoolDescriptor& pool_descriptor) {
98
+ scheduler.remove_idle_task (id, pool_descriptor);
99
+ }
100
+
53
101
void PowerPlant::submit (const NUClear::id_t & id,
54
102
const int & priority,
55
103
const util::GroupDescriptor& group,
@@ -75,14 +123,19 @@ void PowerPlant::submit(std::unique_ptr<threading::ReactionTask>&& task, const b
75
123
}
76
124
}
77
125
78
- void PowerPlant::add_idle_task (const NUClear::id_t & id,
79
- const util::ThreadPoolDescriptor& pool_descriptor,
80
- std::function<void ()>&& task) {
81
- scheduler.add_idle_task (id, pool_descriptor, std::move (task));
82
- }
126
+ void PowerPlant::log (const LogLevel& level, std::string message) {
127
+ // Get the current task
128
+ const auto * current_task = threading::ReactionTask::get_current_task ();
83
129
84
- void PowerPlant::remove_idle_task (const NUClear::id_t & id, const util::ThreadPoolDescriptor& pool_descriptor) {
85
- scheduler.remove_idle_task (id, pool_descriptor);
130
+ // Direct emit the log message so that any direct loggers can use it
131
+ emit<dsl::word::emit::Direct>(std::make_unique<message::LogMessage>(
132
+ level,
133
+ current_task != nullptr ? current_task->parent .reactor .log_level : LogLevel::UNKNOWN,
134
+ std::move (message),
135
+ current_task != nullptr ? current_task->stats : nullptr ));
136
+ }
137
+ void PowerPlant::log (const LogLevel& level, std::stringstream& message) {
138
+ log (level, message.str ());
86
139
}
87
140
88
141
void PowerPlant::shutdown () {
@@ -101,4 +154,5 @@ void PowerPlant::shutdown() {
101
154
bool PowerPlant::running () const {
102
155
return is_running.load ();
103
156
}
157
+
104
158
} // namespace NUClear
0 commit comments