@@ -939,6 +939,18 @@ Stub::LogServiceActive()
939939 return log_thread_;
940940}
941941
942+ std::unique_ptr<Logger> Logger::log_instance_;
943+
944+ std::unique_ptr<Logger>&
945+ Logger::GetOrCreateInstance ()
946+ {
947+ if (Logger::log_instance_.get () == nullptr ) {
948+ Logger::log_instance_ = std::make_unique<Logger>();
949+ }
950+
951+ return Logger::log_instance_;
952+ }
953+
942954// Bound function, called from the python client
943955void
944956Logger::Log (const std::string& message, LogLevel level)
@@ -954,7 +966,7 @@ Logger::Log(const std::string& message, LogLevel level)
954966 uint32_t line = lineno.cast <uint32_t >();
955967
956968 if (!stub->LogServiceActive ()) {
957- Logger::GetOrCreateInstance (). Log (filename, line, level, message);
969+ Logger::GetOrCreateInstance ()-> Log (filename, line, level, message);
958970 } else {
959971 std::unique_ptr<PbLog> log_msg (new PbLog (filename, line, message, level));
960972 stub->EnqueueLogRequest (log_msg);
@@ -1195,6 +1207,7 @@ main(int argc, char** argv)
11951207 std::string triton_install_path = argv[6 ];
11961208 std::string name = argv[8 ];
11971209
1210+ std::unique_ptr<Logger>& logger = Logger::GetOrCreateInstance ();
11981211 std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance ();
11991212 try {
12001213 stub->Instantiate (
@@ -1213,7 +1226,7 @@ main(int argc, char** argv)
12131226
12141227 std::atomic<bool > background_thread_running = {true };
12151228 std::thread background_thread =
1216- std::thread ([&parent_pid, &background_thread_running, &stub] {
1229+ std::thread ([&parent_pid, &background_thread_running, &stub, &logger ] {
12171230 while (background_thread_running) {
12181231 // Every 300ms set the health variable to true. This variable is in
12191232 // shared memory and will be set to false by the parent process.
@@ -1235,6 +1248,7 @@ main(int argc, char** argv)
12351248 non_graceful_exit = true ;
12361249
12371250 // Destroy stub and exit.
1251+ logger.reset ();
12381252 stub.reset ();
12391253 exit (1 );
12401254 }
@@ -1264,6 +1278,7 @@ main(int argc, char** argv)
12641278 // objects. If the scoped_interpreter is destroyed before the stub object,
12651279 // this process will no longer hold the GIL lock and destruction of the stub
12661280 // will result in segfault.
1281+ logger.reset ();
12671282 stub.reset ();
12681283
12691284 return 0 ;
0 commit comments