You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. // \file service.cpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. #ifdef WIN32
  23. #include <windows.h>
  24. #include <strsafe.h>
  25. #else
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #endif
  32. #ifdef DEBUG_LOG_FILE
  33. #include <fstream>
  34. #endif
  35. #include <cstdio>
  36. #include <cstdlib>
  37. #include <thread>
  38. #include "service.hpp"
  39. #ifdef WIN32
  40. // Application main entry point for Windows.
  41. int main(int argc, char *argv[]) {
  42. #ifdef DEBUG_LOG_FILE
  43. std::ofstream logStream;
  44. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  45. logStream << "main. argc: " << argc << std::endl;
  46. for (int i = 0; i < argc; i++) {
  47. logStream << "arg " << i << ": '" << argv[i] << "'" << std::endl;
  48. }
  49. logStream.close();
  50. #endif
  51. CodeDweller::Service &service = CodeDweller::Service::getInstance();
  52. return service.main(argc, (char **) argv);
  53. }
  54. // Service entry point for Windows.
  55. VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv) {
  56. #ifdef DEBUG_LOG_FILE
  57. std::ofstream logStream;
  58. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  59. logStream << "ServiceMain. argc: " << argc << std::endl;
  60. for (unsigned int i = 0; i < argc; i++) {
  61. logStream << "arg " << i << ": '" << argv[i] << "'" << std::endl;
  62. }
  63. logStream.close();
  64. #endif
  65. CodeDweller::Service &service = CodeDweller::Service::getInstance();
  66. return service.serviceMain(argc, argv);
  67. }
  68. /// Control message handler for Windows.
  69. VOID WINAPI ServiceCtrlHandler(DWORD message) {
  70. #ifdef DEBUG_LOG_FILE
  71. std::ofstream logStream;
  72. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  73. logStream << "ServiceCtrlHandler. message: " << message
  74. << ". Calling processCtrlMessage" << std::endl;
  75. logStream.close();
  76. #endif
  77. CodeDweller::Service &service = CodeDweller::Service::getInstance();
  78. service.processCtrlMessage(message);
  79. #ifdef DEBUG_LOG_FILE
  80. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  81. logStream << "ServiceCtrlHandler. Done." << std::endl;
  82. logStream.close();
  83. #endif
  84. }
  85. #else
  86. // Main program for *nix daemon.
  87. int main(int argc, char *argv[]) {
  88. CodeDweller::Service &service = CodeDweller::Service::getInstance();
  89. return service.main(argc, argv);
  90. }
  91. #endif
  92. namespace CodeDweller {
  93. std::mutex Service::objectMutex;
  94. std::vector<std::string> Service::cmdLineArgs;
  95. bool Service::pauseReceived = false;
  96. bool Service::resumeReceived = false;
  97. bool Service::stopReceived = false;
  98. std::vector<Service::Callback *> Service::pauseCallbacks;
  99. std::vector<Service::Callback *> Service::resumeCallbacks;
  100. std::vector<Service::Callback *> Service::stopCallbacks;
  101. int Service::callbackTimeout_ms = 30 * 1000;
  102. Service::Service() :
  103. callbacksActive(true) {
  104. }
  105. Service &Service::getInstance() {
  106. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  107. static Service service;
  108. return service;
  109. }
  110. int Service::main(int argc, char *argv[]) {
  111. // Under Windows, only arg 0 is passed. Under *nix, all arguments
  112. // are passed.
  113. loadArguments(argc, (char **) argv);
  114. #ifdef DEBUG_LOG_FILE
  115. std::ofstream logStream;
  116. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  117. logStream << "Service::main. argc: " << argc << std::endl;
  118. for (int i = 0; i < argc; i++) {
  119. logStream << "arg " << i << ": '" << argv[i] << "'" << std::endl;
  120. }
  121. logStream.close();
  122. #endif
  123. #ifdef WIN32
  124. SERVICE_TABLE_ENTRY ServiceTable[] = {
  125. {(LPTSTR) "", ServiceMain},
  126. {NULL, NULL}
  127. };
  128. if (StartServiceCtrlDispatcher(ServiceTable) == FALSE) {
  129. return GetLastError();
  130. }
  131. return 0;
  132. #else
  133. pid_t pid;
  134. // Fork the process.
  135. pid = fork();
  136. if (pid < 0) {
  137. perror("Error from first fork");
  138. return(EXIT_FAILURE);
  139. }
  140. // Terminate the parent.
  141. if (pid > 0) {
  142. return(EXIT_SUCCESS);
  143. }
  144. // This is the child. Become the session leader.
  145. if (setsid() < 0) {
  146. perror("Error from setsid");
  147. return(EXIT_FAILURE);
  148. }
  149. // Fork again.
  150. pid = fork();
  151. if (pid < 0) {
  152. perror("Error from second fork");
  153. return(EXIT_FAILURE);
  154. }
  155. // Terminate the parent.
  156. if (pid > 0) {
  157. return(EXIT_SUCCESS);
  158. }
  159. // Set default file permissions. This call always succeeds.
  160. umask(0);
  161. // Disassociate the process from the default directory of the
  162. // grandparent.
  163. if (chdir("/") != 0) {
  164. perror("Error from chdir");
  165. return(EXIT_FAILURE);
  166. }
  167. // Initialize the set of signals to wait for.
  168. if (sigemptyset(&signalSet) != 0) {
  169. perror("Error from sigemptyset");
  170. return(EXIT_FAILURE);
  171. }
  172. if ((sigaddset(&signalSet, SIGTSTP) != 0) ||
  173. (sigaddset(&signalSet, SIGCONT) != 0) ||
  174. (sigaddset(&signalSet, SIGHUP) != 0) ||
  175. (sigaddset(&signalSet, SIGTERM) != 0)) {
  176. perror("Error from sigaddset");
  177. return(EXIT_FAILURE);
  178. }
  179. // Block the signals.
  180. if (sigprocmask(SIG_BLOCK, &signalSet, NULL) != 0) {
  181. perror("Error from sigprocmask");
  182. return(EXIT_FAILURE);
  183. }
  184. // Close all open file descriptors.
  185. for (int fd = 2; fd >= 0; fd--) {
  186. if (close(fd) != 0) {
  187. // There is no controlling terminal to send any message to.
  188. return(EXIT_FAILURE);
  189. }
  190. }
  191. // Connect standard I/O to the null device.
  192. int fd;
  193. // stdin.
  194. fd = open("/dev/null", O_RDONLY);
  195. if (fd < 0) {
  196. return(EXIT_FAILURE);
  197. }
  198. // stdout.
  199. fd = open("/dev/null", O_WRONLY);
  200. if (fd < 0) {
  201. return(EXIT_FAILURE);
  202. }
  203. // stderr.
  204. fd = open("/dev/null", O_WRONLY);
  205. if (fd < 0) {
  206. return(EXIT_FAILURE);
  207. }
  208. // Start the thread to process messages.
  209. std::thread messageThread(&Service::processMessages, this);
  210. // Run the service.
  211. int status;
  212. status = run();
  213. // Send a Stop message so that messageThread exits.
  214. pthread_kill(messageThread.native_handle(), SIGTERM);
  215. messageThread.join();
  216. return(status);
  217. #endif
  218. }
  219. #ifdef WIN32
  220. void Service::serviceMain(DWORD argc, LPTSTR *argv) {
  221. // Arg 0 contains the service name; skip it. The remaining
  222. // arguments contain the command-line arguments, excluding the
  223. // executable name; append them to the object's argument list.
  224. loadArguments(argc - 1, (char **) argv + 1);
  225. // Register the service control handler with the SCM.
  226. serviceStatusHandle =
  227. RegisterServiceCtrlHandler("", ServiceCtrlHandler);
  228. #ifdef DEBUG_LOG_FILE
  229. std::ofstream logStream;
  230. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  231. logStream << "ServiceMain. argc: " << argc << std::endl;
  232. for (DWORD i = 0; i < argc; i++) {
  233. logStream << "arg " << i << ": '" << argv[i] << "'" << std::endl;
  234. }
  235. logStream << " serviceStatusHandle == NULL: " <<
  236. (NULL == serviceStatusHandle) << std::endl;
  237. logStream.close();
  238. #endif
  239. if (serviceStatusHandle == NULL) {
  240. return;
  241. }
  242. ZeroMemory(&serviceStatus, sizeof(serviceStatus));
  243. serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  244. serviceStatus.dwControlsAccepted = 0;
  245. serviceStatus.dwCurrentState = SERVICE_START_PENDING;
  246. serviceStatus.dwWin32ExitCode = NO_ERROR;
  247. serviceStatus.dwServiceSpecificExitCode = 0;
  248. serviceStatus.dwCheckPoint = 0;
  249. serviceStatus.dwWaitHint = 5000;
  250. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  251. // Tell the service controller that the service has started.
  252. serviceStatus.dwControlsAccepted =
  253. SERVICE_ACCEPT_STOP |
  254. SERVICE_ACCEPT_PAUSE_CONTINUE;
  255. serviceStatus.dwCurrentState = SERVICE_RUNNING;
  256. serviceStatus.dwWin32ExitCode = 0;
  257. serviceStatus.dwCheckPoint = 0;
  258. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  259. #ifdef DEBUG_LOG_FILE
  260. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  261. logStream << "Starting application thread." << std::endl;
  262. logStream.close();
  263. #endif
  264. // Start the application thread.
  265. int status;
  266. status = run();
  267. // Change status to stopped.
  268. serviceStatus.dwControlsAccepted = 0;
  269. serviceStatus.dwCurrentState = SERVICE_STOPPED;
  270. serviceStatus.dwWin32ExitCode = status;
  271. serviceStatus.dwCheckPoint = 0;
  272. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  273. #ifdef DEBUG_LOG_FILE
  274. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  275. logStream << "Exiting." << std::endl;
  276. logStream.close();
  277. #endif
  278. return;
  279. }
  280. #endif
  281. void Service::loadArguments(int argc, char *argv[]) {
  282. for (int i = 0; i < argc; i++) {
  283. cmdLineArgs.push_back(argv[i]);
  284. }
  285. }
  286. const std::vector<std::string> &Service::arguments() {
  287. return cmdLineArgs;
  288. }
  289. void Service::setCallbackTimeout_ms(int timeout_ms) {
  290. callbackTimeout_ms = (timeout_ms < 1 ? 1 : timeout_ms);
  291. }
  292. void Service::onPauseCall(Callback &pauseFunctor) {
  293. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  294. pauseCallbacks.push_back(&pauseFunctor);
  295. }
  296. void Service::onResumeCall(Callback &resumeFunctor) {
  297. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  298. resumeCallbacks.push_back(&resumeFunctor);
  299. }
  300. void Service::onStopCall(Callback &stopFunctor) {
  301. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  302. stopCallbacks.push_back(&stopFunctor);
  303. }
  304. bool Service::receivedPause() {
  305. return (pauseReceived);
  306. }
  307. bool Service::receivedResume() {
  308. return (resumeReceived);
  309. }
  310. bool Service::receivedStop() {
  311. return (stopReceived);
  312. }
  313. void Service::clearReceivedPause() {
  314. pauseReceived = false;
  315. }
  316. void Service::clearReceivedResume() {
  317. resumeReceived = false;
  318. }
  319. void Service::clearReceivedStop() {
  320. stopReceived = false;
  321. }
  322. #ifdef WIN32
  323. void Service::processCtrlMessage(DWORD message) {
  324. #ifdef DEBUG_LOG_FILE
  325. std::ofstream logStream;
  326. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  327. logStream << "processCtrlMessage. message: " << message << std::endl;
  328. logStream.close();
  329. #endif
  330. switch (message) {
  331. case SERVICE_CONTROL_PAUSE:
  332. #ifdef DEBUG_LOG_FILE
  333. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  334. logStream << "processCtrlMessage. Received Pause" << std::endl;
  335. logStream.close();
  336. #endif
  337. serviceStatus.dwControlsAccepted = 0;
  338. serviceStatus.dwCurrentState = SERVICE_PAUSE_PENDING;
  339. serviceStatus.dwWin32ExitCode = NO_ERROR;
  340. serviceStatus.dwCheckPoint = 1;
  341. serviceStatus.dwWaitHint = callbackTimeout_ms;
  342. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  343. pauseReceived = true;
  344. for (auto callback : pauseCallbacks) {
  345. (*callback)();
  346. serviceStatus.dwCheckPoint++;
  347. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  348. }
  349. serviceStatus.dwControlsAccepted =
  350. SERVICE_ACCEPT_SHUTDOWN |
  351. SERVICE_ACCEPT_STOP |
  352. SERVICE_ACCEPT_PAUSE_CONTINUE;
  353. serviceStatus.dwCurrentState = SERVICE_PAUSED;
  354. serviceStatus.dwWin32ExitCode = NO_ERROR;
  355. serviceStatus.dwCheckPoint = 0;
  356. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  357. break;
  358. case SERVICE_CONTROL_CONTINUE:
  359. #ifdef DEBUG_LOG_FILE
  360. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  361. logStream << "processCtrlMessage. Received Resume" << std::endl;
  362. logStream.close();
  363. #endif
  364. serviceStatus.dwControlsAccepted = 0;
  365. serviceStatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
  366. serviceStatus.dwWin32ExitCode = NO_ERROR;
  367. serviceStatus.dwCheckPoint = 1;
  368. serviceStatus.dwWaitHint = callbackTimeout_ms;
  369. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  370. resumeReceived = true;
  371. for (auto callback : resumeCallbacks) {
  372. (*callback)();
  373. serviceStatus.dwCheckPoint++;
  374. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  375. }
  376. serviceStatus.dwControlsAccepted =
  377. SERVICE_ACCEPT_SHUTDOWN |
  378. SERVICE_ACCEPT_STOP |
  379. SERVICE_ACCEPT_PAUSE_CONTINUE;
  380. serviceStatus.dwCurrentState = SERVICE_RUNNING;
  381. serviceStatus.dwWin32ExitCode = NO_ERROR;
  382. serviceStatus.dwCheckPoint = 0;
  383. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  384. break;
  385. case SERVICE_CONTROL_STOP:
  386. case SERVICE_CONTROL_SHUTDOWN:
  387. #ifdef DEBUG_LOG_FILE
  388. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  389. logStream << "processCtrlMessage. Received "
  390. << (message == SERVICE_CONTROL_STOP ? "STOP" : "SHUTDOWN")
  391. << std::endl;
  392. logStream.close();
  393. #endif
  394. serviceStatus.dwControlsAccepted = 0;
  395. serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  396. serviceStatus.dwWin32ExitCode = NO_ERROR;
  397. serviceStatus.dwCheckPoint = 1;
  398. serviceStatus.dwWaitHint = callbackTimeout_ms;
  399. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  400. stopReceived = true;
  401. for (auto callback : stopCallbacks) {
  402. (*callback)();
  403. serviceStatus.dwCheckPoint++;
  404. (void) SetServiceStatus(serviceStatusHandle, &serviceStatus);
  405. }
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. #else
  412. void Service::processMessages() {
  413. int sigNum;
  414. int status;
  415. while (true) {
  416. // Wait for a signal.
  417. status = sigwait(&signalSet, &sigNum);
  418. if (0 != status) {
  419. perror("Error from sigwait");
  420. // TODO: Implement recovery.
  421. ;
  422. }
  423. // Process the message.
  424. switch (sigNum) {
  425. case SIGTSTP:
  426. {
  427. pauseReceived = true;
  428. callbacksActive = true;
  429. // Get the timeout time.
  430. timeoutTime =
  431. std::chrono::steady_clock::now() +
  432. std::chrono::milliseconds(callbackTimeout_ms);
  433. // Start watchdog thread.
  434. std::thread watchdogThread(&Service::watchdog, this);
  435. #ifdef DEBUG_LOG_FILE
  436. std::ofstream logStream;
  437. auto startTime = std::chrono::steady_clock::now();
  438. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  439. logStream << "Service::processMessages. Calling Pause callbacks--"
  440. << std::endl;
  441. #endif
  442. for (auto callback : pauseCallbacks) {
  443. (*callback)();
  444. timeoutTime =
  445. std::chrono::steady_clock::now() +
  446. std::chrono::milliseconds(callbackTimeout_ms);
  447. #ifdef DEBUG_LOG_FILE
  448. logStream << "Service::processMessages. "
  449. << "Cumulative time after callback (ms): "
  450. << std::chrono::duration_cast<std::chrono::milliseconds>
  451. (std::chrono::steady_clock::now() - startTime).count()
  452. << std::endl;
  453. #endif
  454. }
  455. callbacksActive = false;
  456. #ifdef DEBUG_LOG_FILE
  457. logStream << "Service::processMessages. "
  458. << "Cumulative time after all callback (ms): "
  459. << std::chrono::duration_cast<std::chrono::milliseconds>
  460. (std::chrono::steady_clock::now() - startTime).count()
  461. << std::endl;
  462. #endif
  463. watchdogThread.join();
  464. #ifdef DEBUG_LOG_FILE
  465. logStream << "Service::processMessages. "
  466. <<" Cumulative time after joining watchdog (ms): "
  467. << std::chrono::duration_cast<std::chrono::milliseconds>
  468. (std::chrono::steady_clock::now() - startTime).count()
  469. << std::endl;
  470. logStream.close();
  471. #endif
  472. }
  473. break;
  474. case SIGCONT:
  475. {
  476. resumeReceived = true;
  477. callbacksActive = true;
  478. // Get the timeout time.
  479. timeoutTime =
  480. std::chrono::steady_clock::now() +
  481. std::chrono::milliseconds(callbackTimeout_ms);
  482. // Start watchdog thread.
  483. std::thread watchdogThread(&Service::watchdog, this);
  484. for (auto callback : resumeCallbacks) {
  485. (*callback)();
  486. timeoutTime =
  487. std::chrono::steady_clock::now() +
  488. std::chrono::milliseconds(callbackTimeout_ms);
  489. }
  490. callbacksActive = false;
  491. watchdogThread.join();
  492. }
  493. break;
  494. case SIGTERM:
  495. {
  496. stopReceived = true;
  497. callbacksActive = true;
  498. // Get the timeout time.
  499. timeoutTime =
  500. std::chrono::steady_clock::now() +
  501. std::chrono::milliseconds(callbackTimeout_ms);
  502. // Start watchdog thread.
  503. std::thread watchdogThread(&Service::watchdog, this);
  504. for (auto callback : stopCallbacks) {
  505. (*callback)();
  506. timeoutTime =
  507. std::chrono::steady_clock::now() +
  508. std::chrono::milliseconds(callbackTimeout_ms);
  509. }
  510. callbacksActive = false;
  511. watchdogThread.join();
  512. }
  513. // Exit.
  514. return;
  515. break;
  516. default:
  517. ;
  518. } // switch.
  519. } // while.
  520. }
  521. void Service::watchdog() {
  522. #ifdef DEBUG_LOG_FILE
  523. auto startTime = std::chrono::steady_clock::now();
  524. std::ofstream logStream;
  525. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  526. logStream << "**Service::watchdog. Thread starting." << std::endl;
  527. logStream.close();
  528. #endif
  529. // Sleep and check whether the process should exit.
  530. std::chrono::milliseconds sleepTime(100);
  531. while (std::chrono::steady_clock::now() < timeoutTime) {
  532. std::this_thread::sleep_for(sleepTime);
  533. if (!callbacksActive) {
  534. #ifdef DEBUG_LOG_FILE
  535. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  536. logStream << "**Service::watchdog. Exiting at "
  537. << std::chrono::duration_cast<std::chrono::milliseconds>
  538. (std::chrono::steady_clock::now() - startTime).count()
  539. << " ms after starting"
  540. << std::endl;
  541. logStream.close();
  542. #endif
  543. return;
  544. }
  545. #ifdef DEBUG_LOG_FILE
  546. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  547. logStream << "Service::watchdog. Cumulative time since starting (ms): "
  548. << std::chrono::duration_cast<std::chrono::milliseconds>
  549. (std::chrono::steady_clock::now() - startTime).count()
  550. << std::endl;
  551. logStream.close();
  552. #endif
  553. }
  554. // Timeout expired.
  555. #ifdef DEBUG_LOG_FILE
  556. logStream.open(DEBUG_LOG_FILE, std::fstream::app);
  557. logStream << "**Service::watchdog. Callback timeout expired. "
  558. << "Cumulative time (ms): "
  559. << std::chrono::duration_cast<std::chrono::milliseconds>
  560. (std::chrono::steady_clock::now() - startTime).count()
  561. << std::endl;
  562. logStream.close();
  563. #endif
  564. std::exit(EXIT_FAILURE);
  565. }
  566. #endif
  567. }