WSF
WsfConsole.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2020 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef WSF_CONSOLE_HPP
13#define WSF_CONSOLE_HPP
14
15#include "wsf_export.h"
16
17#include <fstream>
18#include <list>
19#include <map>
20
21#include "UtInput.hpp"
22#include "UtLogSubscriber.hpp"
23#include "UtOptional.hpp"
26
27namespace wsf
28{
29namespace console
30{
34{
36
38 ut::log::TypeList mTypes;
39
41 bool mSingleLine = false;
42
44 bool mForceFlushing = true;
45};
46
48class WSF_EXPORT SubscriberBase : public ut::log::SubscriberBase
49{
50public:
52 explicit SubscriberBase(const OutputSettings& aSettings) noexcept;
53
55 void UpdateSettings(const OutputSettings& aSettings);
57 OutputSettings GetSettings() const noexcept;
58
59private:
62 bool AddMessageP(const ut::log::Message& aMessage) noexcept final;
63
68 bool WriteSingleLine(const ut::log::Message& aMessage, std::ostream& aStream, int aDepth = 0) const noexcept;
69
71 virtual std::ostream& GetStream() = 0;
73 virtual void FlushStream(bool aFlush) = 0;
74
75 bool mSingleLine = false;
76 bool mForceFlushing = true;
77};
78
81{
82public:
84 explicit ConsoleSubscriber(const OutputSettings& aSettings) noexcept;
85
86 ~ConsoleSubscriber() noexcept override;
87
88private:
90 std::ostream& GetStream() override { return mStream; }
92 void FlushStream(bool aFlush) override;
93
94 std::ostringstream mStream;
97 ut::log::TypeList mPreviousConsoleSubscriptions;
98};
99
102{
103public:
105
106 ~FileSubscriber() noexcept override;
107
110 void OpenFile(const std::string& aFileName);
111
112private:
114 std::ostream& GetStream() override { return mStream; }
116 void FlushStream(bool aFlush) override;
117
118 std::string mFileName;
119 std::ofstream mStream;
120};
121
124{
125public:
126 ScenarioExtension() = default;
127
130
131 bool ProcessInput(UtInput& aInput) override;
132 void SimulationCreated(WsfSimulation& aSimulation) override;
133 bool Complete() override;
134
135private:
136 void ProcessInputFile(UtInput& aInput);
137 static void ProcessInputSettings(UtInput& aInput, OutputSettings& aSettings);
138 static void ProcessInputType(UtInput& aInput, ut::log::TypeList& aTypes);
139
140 OutputSettings mConsoleSettings;
141 std::map<std::string, OutputSettings> mFileSettings;
142
144 ut::optional<ConsoleSubscriber> mConsoleSubscriber;
145 std::list<FileSubscriber> mFileSubscribers;
146};
147} // namespace console
148} // namespace wsf
149
150#endif
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
WsfScenarioExtension()
Definition WsfScenarioExtension.cpp:21
The main controller for a simulation.
Definition WsfSimulation.hpp:109
ConsoleSubscriber(const OutputSettings &aSettings) noexcept
Disables default console output.
Definition WsfConsole.cpp:159
Writes received messages to file on disk.
Definition WsfConsole.hpp:102
void OpenFile(const std::string &aFileName)
Definition WsfConsole.cpp:188
SubscriberBase(const OutputSettings &aSettings) noexcept
Creates and registers the subscriber.
Definition WsfConsole.cpp:41
bool ProcessInput(UtInput &aInput) override
Definition WsfConsole.cpp:214
ScenarioExtension(const ScenarioExtension &)=delete
void SimulationCreated(WsfSimulation &aSimulation) override
Definition WsfConsole.cpp:332
ScenarioExtension & operator=(const ScenarioExtension &)=delete
bool Complete() override
Definition WsfConsole.cpp:343
void UpdateSettings(const OutputSettings &aSettings)
Sets the settings.
Definition WsfConsole.cpp:48
SubscriberBase(const OutputSettings &aSettings) noexcept
Creates and registers the subscriber.
Definition WsfConsole.cpp:41
OutputSettings GetSettings() const noexcept
Returns the current settings.
Definition WsfConsole.cpp:56
Definition WsfConsole.hpp:30
Function definitions for those who need run-time access to the version.
Definition WsfComm.cpp:32
Definition WsfConsole.hpp:34
ut::log::TypeList mTypes
Which types to subscribe to.
Definition WsfConsole.hpp:38
OutputSettings()
Definition WsfConsole.cpp:36
bool mForceFlushing
Perform a I/O flush at the end of every log entry.
Definition WsfConsole.hpp:44
bool mSingleLine
Format messages are enabled if Info messages are enabled.
Definition WsfConsole.hpp:41
Copyrights Multiple, All Rights Reserved