4#include "Stroika/Frameworks/StroikaPreComp.h"
10#include "Stroika/Foundation/Debug/Fatal.h"
12#include "Stroika/Foundation/Execution/CommandLine.h"
14#include "Stroika/Foundation/Execution/SignalHandlers.h"
16#if qStroika_Foundation_Common_Platform_Windows
17#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
18#include "Stroika/Foundation/Execution/Platform/Windows/StructuredException.h"
21#include "Stroika/Frameworks/Service/Main.h"
23#include "AppVersion.h"
62 if (not e.fMessage.empty ()) {
63 cerr <<
"Error: " << e.fMessage.AsUTF8<
string> () << endl;
66 cerr <<
"Usage: " << m.GetServiceDescription ().fRegistrationName.AsNarrowSDKString () <<
" [options] where options can be :\n ";
69 <<
" /* Install service (only when debugging - should use real installer like WIX) */" << endl;
71 <<
" /* UnInstall service (only when debugging - should use real installer like WIX) */" << endl;
74 <<
" /* Run this process as a service (doesn't exit until the serivce is done ...) */" << endl;
75 cerr <<
"\t--" <<
String{Main::CommandNames::kRunDirectly}.
AsNarrowSDKString () <<
" /* Run this process as a directly (doesn't exit until the serivce is done or ARGUMENT TIMEOUT seconds elapsed ...) but not using service infrastructure */"
78 <<
" /* Service/Control Function: Start the service */" << endl;
80 <<
" /* Service/Control Function: Stop the service */" << endl;
82 <<
" /* Service/Control Function: Forced stop the service (after trying to normally stop) */" << endl;
84 <<
" /* Service/Control Function: Stop and then re-start the service (ok if already stopped) */" << endl;
86 <<
" /* Service/Control Function: Stop (force if needed) and then re-start the service (ok if already stopped) */" << endl;
87 cerr <<
"\t--" <<
String{Main::CommandNames::kReloadConfiguration}.
AsNarrowSDKString () <<
" /* Reload service configuration */" << endl;
89 <<
" /* Service/Control Function: Pause the service */" << endl;
91 <<
" /* Service/Control Function: Continue the paused service */" << endl;
92 cerr <<
"\t--Status /* Service/Control Function: Print status of running service */ " << endl;
93 cerr <<
"\t--Version /* print this application version */ " << endl;
94 cerr <<
"\t--help /* Print this help. */ " << endl;
96 <<
"\tExtra unrecognized parameters for start/restart, and forcedrestart operations will be passed along to the actual "
103int main (
int argc,
const char* argv[])
108#if qStroika_Foundation_Execution_Thread_SupportThreadStatistics
109 [[maybe_unused]]
auto&& cleanupReport = Execution::Finally (
110 [] () {
DbgTrace (
"Exiting main with thread {} running"_f, Execution::Thread::GetStatistics ().fRunningThreads); });
121#if qStroika_Foundation_Common_Platform_Windows
125 Debug::RegisterDefaultFatalErrorHandlers (Execution::DefaultLoggingFatalErrorHandler);
135#if qStroika_Foundation_Common_Platform_POSIX
148 .fLogBufferingEnabled =
true,
149 .fSuppressDuplicatesThreshold = 15s,
151 bool dockerContainerFlag =
false;
152 if (dockerContainerFlag) {
153 using namespace IO::FileSystem;
154 Logger::sThe.AddAppender (
155 make_shared<Logger::StreamAppender> (FileOutputStream::New (STDOUT_FILENO, FileStream::AdoptFDPolicy::eDisconnectOnDestruction)));
158#if qStroika_HasComponent_syslog
159 Logger::sThe.SetAppenders (make_shared<Logger::SysLogAppender> (
"Stroika-Sample-Service"sv));
160#elif qStroika_Foundation_Common_Platform_Windows
161 Logger::sThe.SetAppenders (make_shared<Logger::WindowsEventLogAppender> (
"Stroika-Sample-Service"sv));
171 serviceIntegrationRep = make_shared<Main::LoggerServiceWrapper> (serviceIntegrationRep);
177 Main m{make_shared<Samples::Service::SampleAppServiceRep> (), serviceIntegrationRep};
184 using Execution::StandardCommandLineOptions::kHelp;
185 using Execution::StandardCommandLineOptions::kVersion;
189 cmdLine.Validate (allMyOptions);
191 if (cmdLine.Has (Main::CommandOptions::kStatus)) {
195 else if (cmdLine.Has (kHelp)) {
199 else if (cmdLine.Has (kVersion)) {
200 cout << m.GetServiceDescription ().fPrettyName.AsNarrowSDKString () <<
": "sv
201 << Characters::ToString (AppVersion::kVersion).AsNarrowSDKString () << endl;
215 String exceptMsg = Characters::ToString (current_exception ());
217 Logger::sThe.Log (Logger::eError,
"{}"_f, exceptMsg);
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual T AsUTF8() const
nonvirtual string AsNarrowSDKString() const
A generalization of a vector: a container whose elements are keyed by the natural numbers.
A simple/portable wrapper on syslog/log4j/WindowsEventlog, with features like throttling,...
nonvirtual void SetSignalHandlers(SignalID signal)
static SignalHandlerRegistry & Get()
nonvirtual void SetStandardCrashHandlerSignals(SignalHandler handler=SignalHandler{DefaultCrashSignalHandler, SignalHandler::Type::eDirect}, const Containers::Set< SignalID > &forSignals=GetStandardCrashSignals())
static const SignalHandler kIGNORED
static shared_ptr< IServiceIntegrationRep > mkDefaultServiceIntegrationRep()
nonvirtual Containers::Set< ServiceIntegrationFeatures > GetServiceIntegrationFeatures() const
nonvirtual void Run(const CommandArgs &args, const Streams::OutputStream::Ptr< Characters::Character > &out=nullptr)
nonvirtual String GetServiceStatusMessage() const
void DefaultLoggingCrashSignalHandler(Execution::SignalID signal) noexcept