-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainConsole.cpp
More file actions
57 lines (51 loc) · 1.71 KB
/
MainConsole.cpp
File metadata and controls
57 lines (51 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Logger.h"
#include "MainCommon.h"
#include "ctrl-c.h"
#include <memory>
namespace
{
/// Alias CtrlCLibrary's handle ID type here, to reduce maintenance burden in
/// the case that it ever changes
using CtrlCHandleT = unsigned int;
/// Lambda for use as unique_ptr custom deleter to clean up CtrlCLibrary handler
auto DeleteCtrlCHandle = [](CtrlCHandleT* handlePtr)
{
if (!handlePtr) return;
CtrlCLibrary::ResetCtrlCHandler(*handlePtr);
delete handlePtr; // NOSONAR
};
/// Return a unique_ptr to a CtrlCLibrary handler ID, where the unique_ptr is
/// also an RAII wrapper for the handler (i.e. the handler is automatically
/// cleaned up when the unique_ptr goes out of scope)
std::unique_ptr<CtrlCHandleT, decltype(DeleteCtrlCHandle)> MakeCtrlCHandle()
{
return
{ // NOSONAR
new CtrlCHandleT{CtrlCLibrary::SetCtrlCHandler(
[](CtrlCLibrary::CtrlSignal s)
{
if (s != CtrlCLibrary::kCtrlCSignal) return false;
rustLaunchSite::Stop();
return true;
})},
DeleteCtrlCHandle
};
}
}
/// Main entry point for console flavor
int main(int argc, char *argv[])
{
// allocate logger on the stack so that it auto-destructs at end of main()
// also, use stdout / std::cout logging sink since this is the console flavor
rustLaunchSite::Logger logger{
std::make_shared<rustLaunchSite::LogSinkStdout>()};
// attempt to install Ctrl+C handler
auto ctrlCHandle{MakeCtrlCHandle()}; // NOSONAR this needs to live
if (CtrlCLibrary::kErrorID == *ctrlCHandle)
{
LOGERR(logger, "Failed to install Ctrl+C handler");
return rustLaunchSite::RLS_EXIT_HANDLER;
}
// start RLS common main, and return result on exit
return rustLaunchSite::Start(logger, argc, argv);
}