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"
61 if (not e.As<
String> ().empty ()) {
62 cerr <<
"Error: " << e.As<
String> () << endl;
65 cerr <<
"Usage: " << m.GetServiceDescription ().fRegistrationName <<
" [options] where options can be :\n ";
67 cerr <<
"\t--" <<
String{Main::CommandNames::kInstall}
68 <<
" /* Install service (only when debugging - should use real installer like WIX) */" << endl;
69 cerr <<
"\t--" <<
String{Main::CommandNames::kUnInstall}
70 <<
" /* UnInstall service (only when debugging - should use real installer like WIX) */" << endl;
72 cerr <<
"\t--" <<
String{Main::CommandNames::kRunAsService}
73 <<
" /* Run this process as a service (doesn't exit until the service is done ...) */" << endl;
74 cerr <<
"\t--" <<
String{Main::CommandNames::kRunDirectly} <<
" /* Run this process as a directly (doesn't exit until the service is done or ARGUMENT TIMEOUT seconds elapsed ...) but not using service infrastructure */"
76 cerr <<
"\t--" <<
String{Main::CommandNames::kStart} <<
" /* Service/Control Function: Start the service */" << endl;
77 cerr <<
"\t--" <<
String{Main::CommandNames::kStop} <<
" /* Service/Control Function: Stop the service */" << endl;
78 cerr <<
"\t--" <<
String{Main::CommandNames::kForcedStop}
79 <<
" /* Service/Control Function: Forced stop the service (after trying to normally stop) */" << endl;
80 cerr <<
"\t--" <<
String{Main::CommandNames::kRestart}
81 <<
" /* Service/Control Function: Stop and then re-start the service (ok if already stopped) */" << endl;
82 cerr <<
"\t--" <<
String{Main::CommandNames::kForcedRestart}
83 <<
" /* Service/Control Function: Stop (force if needed) and then re-start the service (ok if already stopped) */" << endl;
84 cerr <<
"\t--" <<
String{Main::CommandNames::kReloadConfiguration} <<
" /* Reload service configuration */" << endl;
85 cerr <<
"\t--" <<
String{Main::CommandNames::kPause} <<
" /* Service/Control Function: Pause the service */" << endl;
86 cerr <<
"\t--" <<
String{Main::CommandNames::kContinue} <<
" /* Service/Control Function: Continue the paused service */" << endl;
87 cerr <<
"\t--Status /* Service/Control Function: Print status of running service */ " << endl;
88 cerr <<
"\t--Version /* print this application version */ " << endl;
89 cerr <<
"\t--help /* Print this help. */ " << endl;
91 <<
"\tExtra unrecognized parameters for start/restart, and forced restart operations will be passed along to the actual "
98int main (
int argc,
const char* argv[])
103#if qStroika_Foundation_Execution_Thread_SupportThreadStatistics
104 [[maybe_unused]]
auto&& cleanupReport =
105 Finally ([] () {
DbgTrace (
"Exiting main with thread {} running"_f, Thread::GetStatistics ().fRunningThreads); });
116#if qStroika_Foundation_Common_Platform_Windows
130#if qStroika_Foundation_Common_Platform_POSIX
143 .fLogBufferingEnabled =
true,
144 .fSuppressDuplicatesThreshold = 15s,
146 bool dockerContainerFlag =
false;
147 if (dockerContainerFlag) {
148 using namespace IO::FileSystem;
149 Logger::sThe.AddAppender (
150 make_shared<Logger::StreamAppender> (FileOutputStream::New (STDOUT_FILENO, FileStream::AdoptFDPolicy::eDisconnectOnDestruction)));
153#if qStroika_HasComponent_syslog
154 Logger::sThe.SetAppenders (make_shared<Logger::SysLogAppender> (
"Stroika-Sample-Service"sv));
155#elif qStroika_Foundation_Common_Platform_Windows
156 Logger::sThe.SetAppenders (make_shared<Logger::WindowsEventLogAppender> (
"Stroika-Sample-Service"sv));
166 serviceIntegrationRep = make_shared<Main::LoggerServiceWrapper> (serviceIntegrationRep);
172 Main m{make_shared<Samples::Service::SampleAppServiceRep> (), serviceIntegrationRep};
178 using StandardCommandLineOptions::kHelp;
179 using StandardCommandLineOptions::kVersion;
183 cmdLine.Validate (allMyOptions);
185 if (cmdLine.Has (Main::CommandOptions::kStatus)) {
189 else if (cmdLine.Has (kHelp)) {
193 else if (cmdLine.Has (kVersion)) {
194 cout << m.GetServiceDescription ().fPrettyName <<
": "sv << Characters::ToString (AppVersion::kVersion) << endl;
208 String exceptMsg = Characters::ToString (current_exception ());
210 Logger::sThe.Log (Logger::eError,
"{}"_f, exceptMsg);
212 cerr <<
"FAILED: " << exceptMsg << endl;
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual T AsUTF8() const
A generalization of a vector: a container whose elements are keyed by the natural numbers.
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
void DefaultLoggingFatalErrorHandler(const Characters::SDKChar *msg) noexcept
auto Finally(FUNCTION &&f) -> Private_::FinallySentry< FUNCTION >