Rotate files while the service is running.
authorIain Patterson <me@iain.cx>
Sat, 11 Jan 2014 11:46:42 +0000 (11:46 +0000)
committerIain Patterson <me@iain.cx>
Sat, 11 Jan 2014 11:46:42 +0000 (11:46 +0000)
If AppRotateOnline is set, set up a pipe between the application's
stdout/stderr and a new thread which reads all input and writes to the
configured output file(s).

If a file breaches the configured AppRotateBytes threshold while the
service is running, close and rotate the file then resume logging to
a new file.

This behaviour is not the default due to the potential for things to go
wrong.  We have to juggle file handles and might hit a read or write
error which causes logging to fail thus losing all output until the
service is restarted, etc etc.

Thanks Doug Watson.

12 files changed:
README.txt
gui.cpp
io.cpp
io.h
messages.mc
nssm.rc
registry.cpp
registry.h
resource.h
service.cpp
service.h
settings.cpp

index 830ca67..3769d4c 100644 (file)
@@ -314,6 +314,15 @@ Rotation is independent of the CreateFile() parameters used to open the files.
 They will be rotated regardless of whether NSSM would otherwise have appended\r
 or replaced them.\r
 \r
+NSSM can also rotate files which hit the configured size threshold while the\r
+service is running.  To enable this feature, set AppRotateOnline to a non-zero\r
+value.\r
+\r
+Note that online rotation requires NSSM to intercept the application's I/O\r
+and create the output files on its behalf.  This is more complex and\r
+error-prone than simply redirecting the I/O streams before launching the\r
+application.  Therefore online rotation is not enabled by default.\r
+\r
 \r
 Environment variables\r
 ---------------------\r
diff --git a/gui.cpp b/gui.cpp
index 831aef2..c09b363 100644 (file)
--- a/gui.cpp
+++ b/gui.cpp
@@ -152,9 +152,11 @@ int nssm_gui(int resource, nssm_service_t *service) {
     if (service->stdout_disposition == CREATE_ALWAYS) SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_SETCHECK, BST_CHECKED, 0);\r
     if (service->rotate_files) {\r
       SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_SETCHECK, BST_CHECKED, 0);\r
+      EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE), 1);\r
       EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), 1);\r
       EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), 1);\r
     }\r
+    if (service->rotate_stdout_online || service->rotate_stderr_online) SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_SETCHECK, BST_CHECKED, 0);\r
     SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, service->rotate_seconds, 0);\r
     if (! service->rotate_bytes_high) SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, service->rotate_bytes_low, 0);\r
 \r
@@ -252,6 +254,7 @@ static inline void set_affinity_enabled(unsigned char enabled) {
 }\r
 \r
 static inline void set_rotation_enabled(unsigned char enabled) {\r
+  EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE), enabled);\r
   EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS), enabled);\r
   EnableWindow(GetDlgItem(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW), enabled);\r
 }\r
@@ -505,6 +508,7 @@ int configure(HWND window, nssm_service_t *service, nssm_service_t *orig_service
   /* Get rotation stuff. */\r
   if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {\r
     service->rotate_files = true;\r
+    if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_GETCHECK, 0, 0) & BST_CHECKED) service->rotate_stdout_online = service->rotate_stderr_online = true;
     check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, &service->rotate_seconds);\r
     check_number(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, &service->rotate_bytes_low);\r
   }\r
@@ -1027,6 +1031,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);\r
 \r
       /* Set defaults. */\r
+      SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_ONLINE, BM_SETCHECK, BST_UNCHECKED, 0);\r
       SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_SECONDS, 0, 0);\r
       SetDlgItemInt(tablist[NSSM_TAB_ROTATION], IDC_ROTATE_BYTES_LOW, 0, 0);\r
       set_rotation_enabled(0);\r
diff --git a/io.cpp b/io.cpp
index 9dd21bf..072061e 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -1,5 +1,19 @@
 #include "nssm.h"\r
 \r
+static HANDLE create_logging_thread(logger_t *logger) {\r
+  HANDLE thread_handle = CreateThread(NULL, 0, log_and_rotate, (void *) logger, 0, logger->tid_ptr);\r
+  if (! thread_handle) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETHREAD_FAILED, error_string(GetLastError()), 0);\r
+  return thread_handle;\r
+}\r
+\r
+static inline void write_bom(logger_t *logger) {\r
+  wchar_t bom = L'\ufeff';\r
+  unsigned long out;\r
+  if (! WriteFile(logger->write_handle, (void *) &bom, sizeof(bom), &out, 0)) {\r
+    log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SOMEBODY_SET_UP_US_THE_BOM, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+  }\r
+}\r
+\r
 /* Get path, share mode, creation disposition and flags for a stream. */\r
 int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned long *sharing, unsigned long default_sharing, unsigned long *disposition, unsigned long default_disposition, unsigned long *flags, unsigned long default_flags) {\r
   TCHAR value[NSSM_STDIO_LENGTH];\r
@@ -93,6 +107,22 @@ HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *a
   return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
 }\r
 \r
+static void rotated_filename(TCHAR *path, TCHAR *rotated, unsigned long rotated_len, SYSTEMTIME *st) {\r
+  if (! st) {\r
+    SYSTEMTIME now;\r
+    st = &now;\r
+    GetSystemTime(st);\r
+  }\r
+\r
+  TCHAR buffer[MAX_PATH];\r
+  memmove(buffer, path, sizeof(buffer));\r
+  TCHAR *ext = PathFindExtension(buffer);\r
+  TCHAR extension[MAX_PATH];\r
+  _sntprintf_s(extension, _countof(extension), _TRUNCATE, _T("-%04u%02u%02uT%02u%02u%02u.%03u%s"), st->wYear, st->wMonth, st->wDay, st->wHour, st->wMinute, st->wSecond, st->wMilliseconds, ext);\r
+  *ext = _T('\0');\r
+  _sntprintf_s(rotated, rotated_len, _TRUNCATE, _T("%s%s"), buffer, extension);\r
+}\r
+\r
 void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsigned long low, unsigned long high) {\r
   unsigned long error;\r
 \r
@@ -146,14 +176,8 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign
   /* Get new filename. */\r
   FileTimeToSystemTime(&info.ftLastWriteTime, &st);\r
 \r
-  TCHAR buffer[MAX_PATH];\r
-  memmove(buffer, path, sizeof(buffer));\r
-  TCHAR *ext = PathFindExtension(buffer);\r
-  TCHAR extension[MAX_PATH];\r
-  _sntprintf_s(extension, _countof(extension), _TRUNCATE, _T("-%04u%02u%02uT%02u%02u%02u.%03u%s"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, ext);\r
-  *ext = _T('\0');\r
   TCHAR rotated[MAX_PATH];\r
-  _sntprintf_s(rotated, _countof(rotated), _TRUNCATE, _T("%s%s"), buffer, extension);\r
+  rotated_filename(path, rotated, _countof(rotated), &st);\r
 \r
   /* Rotate. */\r
   if (MoveFile(path, rotated)) return;\r
@@ -186,6 +210,10 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
     set_flags = true;\r
   }\r
 \r
+  ULARGE_INTEGER size;\r
+  size.LowPart = service->rotate_bytes_low;\r
+  size.HighPart = service->rotate_bytes_high;\r
+\r
   /* stdout */\r
   if (get_createfile_parameters(key, NSSM_REG_STDOUT, service->stdout_path, &service->stdout_sharing, NSSM_STDOUT_SHARING, &service->stdout_disposition, NSSM_STDOUT_DISPOSITION, &service->stdout_flags, NSSM_STDOUT_FLAGS)) {\r
     service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0;\r
@@ -194,8 +222,58 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   }\r
   if (si && service->stdout_path[0]) {\r
     if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
-    si->hStdOutput = append_to_file(service->stdout_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);\r
-    if (! si->hStdOutput) return 4;\r
+    HANDLE stdout_handle = append_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
+    if (! stdout_handle) {\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdout_path, error_string(GetLastError()), 0);\r
+      return 4;\r
+    }\r
+\r
+    /* Try online rotation only if a size threshold is set. */\r
+    logger_t *stdout_logger = 0;\r
+    if (service->rotate_files && service->rotate_stdout_online && size.QuadPart) {\r
+      stdout_logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t));\r
+      if (stdout_logger) {\r
+        /* Pipe between application's stdout and our logging handle. */\r
+        if (CreatePipe(&service->stdout_pipe, &si->hStdOutput, &attributes, 0)) {\r
+          stdout_logger->service_name = service->name;\r
+          stdout_logger->path = service->stdout_path;\r
+          stdout_logger->sharing = service->stdout_sharing;\r
+          stdout_logger->disposition = service->stdout_disposition;\r
+          stdout_logger->flags = service->stdout_flags;\r
+          stdout_logger->read_handle = service->stdout_pipe;\r
+          stdout_logger->write_handle = stdout_handle;\r
+          stdout_logger->size = (__int64) size.QuadPart;\r
+          stdout_logger->tid_ptr = &service->stdout_tid;\r
+\r
+          /* Logging thread. */\r
+          service->stdout_thread = create_logging_thread(stdout_logger);\r
+          if (! service->stdout_thread) {\r
+            log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stdout_path, error_string(GetLastError()));\r
+            CloseHandle(service->stdout_pipe);\r
+            CloseHandle(si->hStdOutput);\r
+            service->stdout_tid = 0;\r
+          }\r
+        }\r
+        else service->stdout_tid = 0;\r
+      }\r
+      else {\r
+        log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stdout_logger"), _T("get_output_handles()"), 0);\r
+        service->stdout_tid = 0;\r
+      }\r
+\r
+      /* Fall through to try direct I/O. */\r
+      if (! service->stdout_tid) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stdout_path, error_string(GetLastError()));\r
+    }\r
+\r
+    if (! service->stdout_tid) {\r
+      if (stdout_logger) HeapFree(GetProcessHeap(), 0, stdout_logger);\r
+      if (! DuplicateHandle(GetCurrentProcess(), stdout_handle, GetCurrentProcess(), &si->hStdOutput, 0, true, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {\r
+        log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()), 0);\r
+        return 4;\r
+      }\r
+      service->rotate_stdout_online = false;\r
+    }\r
+\r
     set_flags = true;\r
   }\r
 \r
@@ -211,6 +289,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
       service->stderr_sharing = service->stdout_sharing;\r
       service->stderr_disposition = service->stdout_disposition;\r
       service->stderr_flags = service->stdout_flags;\r
+      service->rotate_stderr_online = false;\r
 \r
       if (si) {\r
         /* Two handles to the same file will create a race. */\r
@@ -222,13 +301,59 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
     }\r
     else if (si) {\r
       if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
-      si->hStdError = append_to_file(service->stderr_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);\r
-      if (! si->hStdError) {\r
+      HANDLE stderr_handle = append_to_file(service->stderr_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
+      if (! stderr_handle) {\r
         log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stderr_path, error_string(GetLastError()), 0);\r
         return 7;\r
       }\r
-      SetEndOfFile(si->hStdError);\r
+\r
+      /* Try online rotation only if a size threshold is set. */\r
+      logger_t *stderr_logger = 0;\r
+      if (service->rotate_files && service->rotate_stderr_online && size.QuadPart) {\r
+        stderr_logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t));\r
+        if (stderr_logger) {\r
+          /* Pipe between application's stderr and our logging handle. */\r
+          if (CreatePipe(&service->stderr_pipe, &si->hStdError, &attributes, 0)) {\r
+            stderr_logger->service_name = service->name;\r
+            stderr_logger->path = service->stderr_path;\r
+            stderr_logger->sharing = service->stderr_sharing;\r
+            stderr_logger->disposition = service->stderr_disposition;\r
+            stderr_logger->flags = service->stderr_flags;\r
+            stderr_logger->read_handle = service->stderr_pipe;\r
+            stderr_logger->write_handle = stderr_handle;\r
+            stderr_logger->size = (__int64) size.QuadPart;\r
+            stderr_logger->tid_ptr = &service->stderr_tid;\r
+\r
+            /* Logging thread. */\r
+            service->stderr_thread = create_logging_thread(stderr_logger);\r
+            if (! service->stderr_thread) {\r
+              log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stderr_path, error_string(GetLastError()));\r
+              CloseHandle(service->stderr_pipe);\r
+              CloseHandle(si->hStdError);\r
+              service->stderr_tid = 0;\r
+            }\r
+          }\r
+          else service->stderr_tid = 0;\r
+        }\r
+        else {\r
+          log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stderr_logger"), _T("get_output_handles()"), 0);\r
+          service->stderr_tid = 0;\r
+        }\r
+\r
+        /* Fall through to try direct I/O. */\r
+        if (! service->stderr_tid) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stderr_path, error_string(GetLastError()));\r
+      }\r
+\r
+      if (! service->stderr_tid) {\r
+        if (stderr_logger) HeapFree(GetProcessHeap(), 0, stderr_logger);\r
+        if (! DuplicateHandle(GetCurrentProcess(), stderr_handle, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {\r
+          log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDERR, error_string(GetLastError()), 0);\r
+          return 7;\r
+        }\r
+        service->rotate_stderr_online = false;\r
+      }\r
     }\r
+\r
     set_flags = true;\r
   }\r
 \r
@@ -243,8 +368,111 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   return 0;\r
 }\r
 \r
-void close_output_handles(STARTUPINFO *si) {\r
+void close_output_handles(STARTUPINFO *si, bool close_stdout, bool close_stderr) {\r
   if (si->hStdInput) CloseHandle(si->hStdInput);\r
-  if (si->hStdOutput) CloseHandle(si->hStdOutput);\r
-  if (si->hStdError) CloseHandle(si->hStdError);\r
+  if (si->hStdOutput && close_stdout) CloseHandle(si->hStdOutput);\r
+  if (si->hStdError && close_stderr) CloseHandle(si->hStdError);\r
+}\r
+\r
+void close_output_handles(STARTUPINFO *si) {\r
+  return close_output_handles(si, true, true);\r
+}\r
+\r
+/* Wrapper to be called in a new thread for logging. */\r
+unsigned long WINAPI log_and_rotate(void *arg) {\r
+  logger_t *logger = (logger_t *) arg;\r
+  if (! logger) return 1;\r
+\r
+  __int64 size;\r
+  BY_HANDLE_FILE_INFORMATION info;\r
+\r
+  /* Find initial file size. */\r
+  if (! GetFileInformationByHandle(logger->write_handle, &info)) logger->size = 0LL;\r
+  else {\r
+    ULARGE_INTEGER l;\r
+    l.HighPart = info.nFileSizeHigh;\r
+    l.LowPart = info.nFileSizeLow;\r
+    size = l.QuadPart;\r
+  }\r
+\r
+  char buffer[80];\r
+  void *address;\r
+  unsigned long in, out;\r
+  while (true) {\r
+    /* Read data from the pipe. */\r
+    address = &buffer;\r
+    if (! ReadFile(logger->read_handle, address, sizeof(buffer), &in, 0)) {\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_READFILE_FAILED, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+      return 2;\r
+    }\r
+\r
+    if (size + (__int64) in >= logger->size) {\r
+      /* Look for newline. */\r
+      unsigned long i;\r
+      for (i = 0; i < in; i++) {\r
+        if (buffer[i] == '\n') {\r
+          unsigned char unicode = IsTextUnicode(address, sizeof(buffer), 0);\r
+          if (unicode) i += sizeof(wchar_t);\r
+          else i += sizeof(char);\r
+\r
+          /* Write up to the newline. */\r
+          if (! WriteFile(logger->write_handle, address, i, &out, 0)) {\r
+            log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_WRITEFILE_FAILED, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+            return 3;\r
+          }\r
+\r
+          /* Rotate. */\r
+          TCHAR rotated[MAX_PATH];\r
+          rotated_filename(logger->path, rotated, _countof(rotated), 0);\r
+\r
+          /*\r
+            Ideally we'd try the rename first then close the handle but\r
+            MoveFile() will fail if the handle is still open so we must\r
+            risk losing everything.\r
+          */\r
+          CloseHandle(logger->write_handle);\r
+          if (! MoveFile(logger->path, rotated)) {\r
+            unsigned long error = GetLastError();\r
+            if (error != ERROR_FILE_NOT_FOUND) {\r
+              log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, logger->service_name, logger->path, _T("MoveFile()"), rotated, error_string(error), 0);\r
+              /* We can at least try to re-open the existing file. */\r
+              logger->disposition = OPEN_ALWAYS;\r
+            }\r
+          }\r
+\r
+          /* Reopen. */\r
+          logger->write_handle = append_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags);\r
+          if (! logger->write_handle) {\r
+            log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(GetLastError()), 0);\r
+            /* Oh dear.  Now we can't log anything further. */\r
+            return 4;\r
+          }\r
+\r
+          /* Unicode files need a new BOM. */\r
+          if (unicode) write_bom(logger);\r
+\r
+          /* Resume writing after the newline. */\r
+          size = 0LL;\r
+          address = (void *) ((char *) address + i);\r
+          in -= i;\r
+\r
+          break;\r
+        }\r
+      }\r
+    }\r
+    else if (! size) {\r
+      /* Write a BOM to the new file. */\r
+      if (IsTextUnicode(address, sizeof(buffer), 0)) write_bom(logger);\r
+    }\r
+\r
+    /* Write the data. */\r
+    if (! WriteFile(logger->write_handle, address, in, &out, 0)) {\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_WRITEFILE_FAILED, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+      return 3;\r
+    }\r
+\r
+    size += (__int64) out;\r
+  }\r
+\r
+  return 0;\r
 }\r
diff --git a/io.h b/io.h
index 2056868..23c8488 100644 (file)
--- a/io.h
+++ b/io.h
 #define NSSM_STDERR_DISPOSITION OPEN_ALWAYS\r
 #define NSSM_STDERR_FLAGS FILE_ATTRIBUTE_NORMAL\r
 \r
+typedef struct {\r
+  TCHAR *service_name;\r
+  TCHAR *path;\r
+  unsigned long sharing;\r
+  unsigned long disposition;\r
+  unsigned long flags;\r
+  HANDLE read_handle;\r
+  HANDLE write_handle;\r
+  __int64 size;\r
+  unsigned long *tid_ptr;\r
+} logger_t;\r
+\r
 int get_createfile_parameters(HKEY, TCHAR *, TCHAR *, unsigned long *, unsigned long, unsigned long *, unsigned long, unsigned long *, unsigned long);\r
 int set_createfile_parameter(HKEY, TCHAR *, TCHAR *, unsigned long);\r
 int delete_createfile_parameter(HKEY, TCHAR *, TCHAR *);\r
 HANDLE append_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long);\r
 void rotate_file(TCHAR *, TCHAR *, unsigned long, unsigned long, unsigned long);\r
 int get_output_handles(nssm_service_t *, HKEY, STARTUPINFO *);\r
+void close_output_handles(STARTUPINFO *, bool, bool);\r
 void close_output_handles(STARTUPINFO *);\r
+unsigned long WINAPI log_and_rotate(void *);\r
 \r
 #endif\r
index fdac57c..ca6bbcf 100644 (file)
Binary files a/messages.mc and b/messages.mc differ
diff --git a/nssm.rc b/nssm.rc
index f014b60..14244c6 100644 (file)
Binary files a/nssm.rc and b/nssm.rc differ
index 443d2e5..b526ee5 100644 (file)
@@ -111,6 +111,8 @@ int create_parameters(nssm_service_t *service, bool editing) {
   }\r
   if (service->rotate_files) set_number(key, NSSM_REG_ROTATE, 1);\r
   else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE);\r
+  if (service->rotate_stdout_online) set_number(key, NSSM_REG_ROTATE_ONLINE, 1);\r
+  else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_ONLINE);\r
   if (service->rotate_seconds) set_number(key, NSSM_REG_ROTATE_SECONDS, service->rotate_seconds);\r
   else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_SECONDS);\r
   if (service->rotate_bytes_low) set_number(key, NSSM_REG_ROTATE_BYTES_LOW, service->rotate_bytes_low);\r
@@ -573,6 +575,11 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) {
     else service->rotate_files = false;\r
   }\r
   else service->rotate_files = false;\r
+  if (get_number(key, NSSM_REG_ROTATE_ONLINE, &rotate_files, false) == 1) {\r
+    if (rotate_files) service->rotate_stdout_online = service->rotate_stderr_online = true;\r
+    else service->rotate_stdout_online = service->rotate_stderr_online = false;\r
+  }\r
+  else service->rotate_stdout_online = service->rotate_stderr_online = false;\r
   if (get_number(key, NSSM_REG_ROTATE_SECONDS, &service->rotate_seconds, false) != 1) service->rotate_seconds = 0;\r
   if (get_number(key, NSSM_REG_ROTATE_BYTES_LOW, &service->rotate_bytes_low, false) != 1) service->rotate_bytes_low = 0;\r
   if (get_number(key, NSSM_REG_ROTATE_BYTES_HIGH, &service->rotate_bytes_high, false) != 1) service->rotate_bytes_high = 0;\r
index 5552063..162b8b3 100644 (file)
@@ -21,6 +21,7 @@
 #define NSSM_REG_STDIO_DISPOSITION _T("CreationDisposition")\r
 #define NSSM_REG_STDIO_FLAGS _T("FlagsAndAttributes")\r
 #define NSSM_REG_ROTATE _T("AppRotateFiles")\r
+#define NSSM_REG_ROTATE_ONLINE _T("AppRotateOnline")\r
 #define NSSM_REG_ROTATE_SECONDS _T("AppRotateSeconds")\r
 #define NSSM_REG_ROTATE_BYTES_LOW _T("AppRotateBytes")\r
 #define NSSM_REG_ROTATE_BYTES_HIGH _T("AppRotateBytesHigh")\r
index fdab376..0fa1f5c 100644 (file)
 #define IDC_ENVIRONMENT_REPLACE         1027\r
 #define IDC_TRUNCATE                    1028\r
 #define IDC_ROTATE                      1029\r
-#define IDC_ROTATE_SECONDS              1030\r
-#define IDC_ROTATE_BYTES_LOW            1031\r
-#define IDC_DISPLAYNAME                 1032\r
-#define IDC_DESCRIPTION                 1033\r
-#define IDC_STARTUP                     1034\r
-#define IDC_LOCALSYSTEM                 1035\r
-#define IDC_INTERACT                    1036\r
-#define IDC_ACCOUNT                     1037\r
-#define IDC_USERNAME                    1038\r
-#define IDC_PASSWORD1                   1039\r
-#define IDC_PASSWORD2                   1040\r
-#define IDC_PRIORITY                    1041\r
-#define IDC_AFFINITY_ALL                1042\r
-#define IDC_AFFINITY                    1043\r
+#define IDC_ROTATE_ONLINE               1030\r
+#define IDC_ROTATE_SECONDS              1031\r
+#define IDC_ROTATE_BYTES_LOW            1032\r
+#define IDC_DISPLAYNAME                 1033\r
+#define IDC_DESCRIPTION                 1034\r
+#define IDC_STARTUP                     1035\r
+#define IDC_LOCALSYSTEM                 1036\r
+#define IDC_INTERACT                    1037\r
+#define IDC_ACCOUNT                     1038\r
+#define IDC_USERNAME                    1039\r
+#define IDC_PASSWORD1                   1040\r
+#define IDC_PASSWORD2                   1041\r
+#define IDC_PRIORITY                    1042\r
+#define IDC_AFFINITY_ALL                1043\r
+#define IDC_AFFINITY                    1044\r
 \r
 // Next default values for new objects\r
 // \r
@@ -67,7 +68,7 @@
 #ifndef APSTUDIO_READONLY_SYMBOLS\r
 #define _APS_NEXT_RESOURCE_VALUE        115\r
 #define _APS_NEXT_COMMAND_VALUE         40001\r
-#define _APS_NEXT_CONTROL_VALUE         1044\r
+#define _APS_NEXT_CONTROL_VALUE         1045\r
 #define _APS_NEXT_SYMED_VALUE           101\r
 #endif\r
 #endif\r
index c3a5008..d0141a1 100644 (file)
@@ -1431,7 +1431,7 @@ int start_service(nssm_service_t *service) {
 \r
   if (get_process_creation_time(service->process_handle, &service->creation_time)) ZeroMemory(&service->creation_time, sizeof(service->creation_time));\r
 \r
-  close_output_handles(&si);\r
+  close_output_handles(&si, ! service->rotate_stdout_online, ! service->rotate_stderr_online);\r
 \r
   if (service->affinity) {\r
     /*\r
index 155586a..8549fee 100644 (file)
--- a/service.h
+++ b/service.h
@@ -57,11 +57,19 @@ typedef struct {
   unsigned long stdout_sharing;\r
   unsigned long stdout_disposition;\r
   unsigned long stdout_flags;\r
+  HANDLE stdout_pipe;\r
+  HANDLE stdout_thread;\r
+  unsigned long stdout_tid;\r
   TCHAR stderr_path[MAX_PATH];\r
   unsigned long stderr_sharing;\r
   unsigned long stderr_disposition;\r
   unsigned long stderr_flags;\r
+  HANDLE stderr_pipe;\r
+  HANDLE stderr_thread;\r
+  unsigned long stderr_tid;\r
   bool rotate_files;\r
+  bool rotate_stdout_online;\r
+  bool rotate_stderr_online;\r
   unsigned long rotate_seconds;\r
   unsigned long rotate_bytes_low;\r
   unsigned long rotate_bytes_high;\r
index 3943220..3f01047 100644 (file)
@@ -830,6 +830,7 @@ settings_t settings[] = {
   { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
   { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number },
   { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
+  { NSSM_REG_ROTATE_ONLINE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
   { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
   { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
   { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },