commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1197197 - in /commons/proper/daemon/trunk/src/native/windows: apps/prunsrv/prunsrv.c include/log.h src/log.c
Date Thu, 03 Nov 2011 16:08:57 GMT
Author: mturk
Date: Thu Nov  3 16:08:56 2011
New Revision: 1197197

URL: http://svn.apache.org/viewvc?rev=1197197&view=rev
Log:
Step 1 in log rotation support. We still need to check if JVM will allow changing stdout/stderr
descriptors

Modified:
    commons/proper/daemon/trunk/src/native/windows/apps/prunsrv/prunsrv.c
    commons/proper/daemon/trunk/src/native/windows/include/log.h
    commons/proper/daemon/trunk/src/native/windows/src/log.c

Modified: commons/proper/daemon/trunk/src/native/windows/apps/prunsrv/prunsrv.c
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/native/windows/apps/prunsrv/prunsrv.c?rev=1197197&r1=1197196&r2=1197197&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/native/windows/apps/prunsrv/prunsrv.c (original)
+++ commons/proper/daemon/trunk/src/native/windows/apps/prunsrv/prunsrv.c Thu Nov  3 16:08:56
2011
@@ -131,6 +131,7 @@ static APXCMDLINEOPT _options[] = {
 /* 36 */    { L"StdOutput",         L"StdOutput",       L"Log",         APXCMDOPT_STE | APXCMDOPT_REG,
NULL, 0},
 /* 37 */    { L"LogJniMessages",    L"LogJniMessages",  L"Log",         APXCMDOPT_INT | APXCMDOPT_REG,
NULL, 1},
 /* 38 */    { L"PidFile",           L"PidFile",         L"Log",         APXCMDOPT_STR | APXCMDOPT_REG,
NULL, 0},
+/* 39 */    { L"Rotate",            L"Rotate",          L"Log",         APXCMDOPT_INT | APXCMDOPT_REG,
NULL, 0},
             /* NULL terminate the array */
             { NULL }
 };
@@ -193,6 +194,7 @@ static APXCMDLINEOPT _options[] = {
 #define SO_STDOUTPUT        GET_OPT_V(36)
 #define SO_JNIVFPRINTF      GET_OPT_I(37)
 #define SO_PIDFILE          GET_OPT_V(38)
+#define SO_LOGROTATE        GET_OPT_I(39)
 
 /* Main service table entry
  * filled at run-time
@@ -239,18 +241,28 @@ static BOOL   gSignalValid   = TRUE;
 static APXJAVA_THREADARGS gRargs;
 static APXJAVA_THREADARGS gSargs;
 
-
 DWORD WINAPI eventThread(LPVOID lpParam)
 {
+    DWORD dwRotateCnt = SO_LOGROTATE;
+
     for (;;) {
-        DWORD dw = WaitForSingleObject(gSignalEvent, INFINITE);
+        DWORD dw = WaitForSingleObject(gSignalEvent, 1000);
+        if (dw == WAIT_TIMEOUT) {
+            /* Do process maintenance */
+            if (SO_LOGROTATE != 0 && --dwRotateCnt == 0) {
+                /* Perform log rotation. */
+
+                 dwRotateCnt = SO_LOGROTATE;
+            }
+            continue;
+        }
         if (dw == WAIT_OBJECT_0 && gSignalValid) {
             if (!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0))
                 apxLogWrite(APXLOG_MARK_SYSERR);
             ResetEvent(gSignalEvent);
+            continue;
         }
-        else
-            break;
+        break;
     }
     ExitThread(0);
     return 0;
@@ -266,8 +278,16 @@ static BOOL redirectStdStreams(APX_STDWR
     BOOL aErr = FALSE;
     BOOL aOut = FALSE;
 
-    if (lpWrapper->szStdOutFilename || lpWrapper->szStdErrFilename)
-        AllocConsole();
+    if (lpWrapper->szStdOutFilename || lpWrapper->szStdErrFilename) {
+        /* Alloc console if it doesn't exists. */
+        if (!AttachConsole(ATTACH_PARENT_PROCESS) &&
+             GetLastError() == ERROR_INVALID_HANDLE) {
+            HWND hc;
+            AllocConsole();
+            if ((hc = GetConsoleWindow()) != NULL)
+                ShowWindow(hc, SW_HIDE);
+        }
+    }
     /* redirect to file or console */
     if (lpWrapper->szStdOutFilename) {
         if (lstrcmpiW(lpWrapper->szStdOutFilename, PRSRV_AUTO) == 0) {
@@ -279,7 +299,8 @@ static BOOL redirectStdStreams(APX_STDWR
             lpWrapper->szStdOutFilename = apxLogFile(gPool,
                                                      lpWrapper->szLogPath,
                                                      lsn,
-                                                     NULL, TRUE);
+                                                     NULL, TRUE,
+                                                     SO_LOGROTATE);
         }
         /* Delete the file if not in append mode
          * XXX: See if we can use the params instead of that.
@@ -305,7 +326,8 @@ static BOOL redirectStdStreams(APX_STDWR
             lpWrapper->szStdErrFilename = apxLogFile(gPool,
                                                      lpWrapper->szLogPath,
                                                      lsn,
-                                                     NULL, TRUE);
+                                                     NULL, TRUE,
+                                                     SO_LOGROTATE);
         }
         if (!aErr)
             DeleteFileW(lpWrapper->szStdErrFilename);
@@ -1016,7 +1038,7 @@ static DWORD serviceStart()
         return TRUE;    /* Nothing to do */
     }
     if (IS_VALID_STRING(SO_PIDFILE)) {
-        gPidfileName = apxLogFile(gPool, SO_LOGPATH, SO_PIDFILE, NULL, FALSE);
+        gPidfileName = apxLogFile(gPool, SO_LOGPATH, SO_PIDFILE, NULL, FALSE, 0);
         if (GetFileAttributesW(gPidfileName) !=  INVALID_FILE_ATTRIBUTES) {
             /* Pid file exists */
             if (!DeleteFileW(gPidfileName)) {
@@ -1384,7 +1406,13 @@ void WINAPI serviceMain(DWORD argc, LPTS
             goto cleanup;
         }
         /* Allocate console so that events gets processed */
-        AllocConsole();
+        if (!AttachConsole(ATTACH_PARENT_PROCESS) &&
+             GetLastError() == ERROR_INVALID_HANDLE) {
+            HWND hc;
+            AllocConsole();
+            if ((hc = GetConsoleWindow()) != NULL)
+                ShowWindow(hc, SW_HIDE);
+        }
     }
     reportServiceStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
     if ((rc = serviceStart()) == 0) {
@@ -1514,9 +1542,12 @@ void __cdecl main(int argc, char **argv)
         }
     }
 
-    apxLogOpen(gPool, SO_LOGPATH, SO_LOGPREFIX);
+    apxLogOpen(gPool, SO_LOGPATH, SO_LOGPREFIX, SO_LOGROTATE);
     apxLogLevelSetW(NULL, SO_LOGLEVEL);
     apxLogWrite(APXLOG_MARK_DEBUG "Commons Daemon procrun log initialized");
+    if (SO_LOGROTATE)
+        apxLogWrite(APXLOG_MARK_DEBUG "Log will rotate each %d seconds.", SO_LOGROTATE);
+
     apxLogWrite(APXLOG_MARK_INFO "Commons Daemon procrun (%s %d-bit) started",
                 PRG_VERSION, PRG_BITS);
 

Modified: commons/proper/daemon/trunk/src/native/windows/include/log.h
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/native/windows/include/log.h?rev=1197197&r1=1197196&r2=1197197&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/native/windows/include/log.h (original)
+++ commons/proper/daemon/trunk/src/native/windows/include/log.h Thu Nov  3 16:08:56 2011
@@ -43,8 +43,8 @@ __APXBEGIN_DECLS
 #define APXLOG_LEVEL_WARN   2
 #define APXLOG_LEVEL_ERROR  3
 
-#define APXLOG_MARK_INFO    NULL, APXLOG_LEVEL_INFO,  TRUE,  NULL, 0, ""
-#define APXLOG_MARK_WARN    NULL, APXLOG_LEVEL_WARN,  TRUE,  NULL, 0, ""
+#define APXLOG_MARK_INFO    NULL, APXLOG_LEVEL_INFO,  TRUE,  __FILE__, __LINE__, ""
+#define APXLOG_MARK_WARN    NULL, APXLOG_LEVEL_WARN,  TRUE,  __FILE__, __LINE__, ""
 #define APXLOG_MARK_ERROR   NULL, APXLOG_LEVEL_ERROR, TRUE,  __FILE__, __LINE__, ""
 #define APXLOG_MARK_DEBUG   NULL, APXLOG_LEVEL_DEBUG, TRUE,  __FILE__, __LINE__, ""
 #define APXLOG_MARK_RAW     NULL, APXLOG_LEVEL_INFO,  FALSE, NULL, 0,
@@ -55,13 +55,15 @@ LPWSTR apxLogFile(
     LPCWSTR szPath,
     LPCWSTR szPrefix,
     LPCWSTR szName,
-    BOOL bTimeStamp
+    BOOL bTimeStamp,
+    DWORD dwRotate
 );
 
 HANDLE apxLogOpen(
     APXHANDLE hPool,
     LPCWSTR szPath,
-    LPCWSTR szPrefix
+    LPCWSTR szPrefix,
+    DWORD dwRotate
 );
 
 void apxLogClose(

Modified: commons/proper/daemon/trunk/src/native/windows/src/log.c
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/native/windows/src/log.c?rev=1197197&r1=1197196&r2=1197197&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/native/windows/src/log.c (original)
+++ commons/proper/daemon/trunk/src/native/windows/src/log.c Thu Nov  3 16:08:56 2011
@@ -20,6 +20,7 @@
 
 #define LINE_SEP    "\r\n"
 #define LOGF_EXT    L".%04d-%02d-%02d.log"
+#define LOGF_EXR    L".%04d-%02d-%02d.%02d%02d%02d.log"
 
 static LPCSTR _log_level[] = {
     "[debug] ",
@@ -32,9 +33,11 @@ static LPCSTR _log_level[] = {
 typedef struct apx_logfile_st {
     HANDLE      hFile;
     DWORD       dwLogLevel;
+    DWORD       dwRotate;
     SYSTEMTIME  sysTime;
     WCHAR       szPath[SIZ_PATHLEN];
     WCHAR       szPrefix[MAX_PATH];
+    WCHAR       szFile[MAX_PATH];
 } apx_logfile_st;
 
 /* Per-application master log file */
@@ -42,13 +45,60 @@ static apx_logfile_st *_st_sys_loghandle
 
 static apx_logfile_st  _st_sys_errhandle = { NULL, APXLOG_LEVEL_WARN, FALSE};
 
+static void logRotate(apx_logfile_st *lf, LPSYSTEMTIME t)
+{
+    WCHAR sName[SIZ_PATHLEN];
+    ULARGE_INTEGER cft;
+    ULARGE_INTEGER lft;
+    HANDLE h;
+
+    if (lf->dwRotate == 0)
+        return;
+
+    SystemTimeToFileTime(&lf->sysTime, (LPFILETIME)&lft);
+    SystemTimeToFileTime(t, (LPFILETIME)&cft);
+    if (cft.QuadPart < (lft.QuadPart + lf->dwRotate * 10000000ULL))
+        return;
+    /* Rotate time */
+    lf->sysTime = *t;
+    if (lf->dwRotate < 86400)
+        wsprintfW(sName, L"\\%s"  LOGF_EXR,
+                  lf->szPrefix,
+                  lf->sysTime.wYear,
+                  lf->sysTime.wMonth,
+                  lf->sysTime.wDay,
+                  lf->sysTime.wHour,
+                  lf->sysTime.wMinute,
+                  lf->sysTime.wSecond);
+    else
+        wsprintfW(sName, L"\\%s"  LOGF_EXT,
+                  lf->szPrefix,
+                  lf->sysTime.wYear,
+                  lf->sysTime.wMonth,
+                  lf->sysTime.wDay);
+    lstrlcpyW(lf->szFile, MAX_PATH, lf->szPath);
+    lstrlcatW(lf->szFile, MAX_PATH, sName);
+    h =  CreateFileW(lf->szFile,
+                     GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
+                     NULL,
+                     OPEN_ALWAYS,
+                     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_SEQUENTIAL_SCAN,
+                     NULL);
+    if (h == INVALID_HANDLE_VALUE) {
+        /* TODO: Log something */
+        return;
+    }
+    CloseHandle(lf->hFile);
+    lf->hFile = h;
+}
 
 LPWSTR apxLogFile(
     APXHANDLE hPool,
     LPCWSTR szPath,
     LPCWSTR szPrefix,
     LPCWSTR szName,
-    BOOL bTimeStamp)
+    BOOL bTimeStamp,
+    DWORD dwRotate)
 {
     LPWSTR sRet;
     WCHAR sPath[SIZ_PATHLEN];
@@ -68,20 +118,33 @@ LPWSTR apxLogFile(
         szPrefix = L"";
     if (!szName)
         szName   = L"";
-    if (bTimeStamp)
-        wsprintfW(sName,
-                  L"\\%s%s" LOGF_EXT,
+    if (bTimeStamp) {
+        if (dwRotate != 0 && dwRotate < 86400)
+            wsprintfW(sName,
+                  L"\\%s%s" LOGF_EXR,
                   szPrefix,
                   szName,
                   sysTime.wYear,
                   sysTime.wMonth,
-                  sysTime.wDay);
-    else
+                  sysTime.wDay,
+                  0,
+                  0,
+                  0);
+        else
+            wsprintfW(sName,
+                      L"\\%s%s" LOGF_EXT,
+                      szPrefix,
+                      szName,
+                      sysTime.wYear,
+                      sysTime.wMonth,
+                      sysTime.wDay);
+    }
+    else {
         wsprintfW(sName,
                   L"\\%s%s",
                   szPrefix,
                   szName);
-
+    }
     sRet = apxPoolAlloc(hPool, (SIZ_PATHLEN) * sizeof(WCHAR));
     /* Set default level to info */
     CreateDirectoryW(sPath, NULL);
@@ -99,7 +162,8 @@ LPWSTR apxLogFile(
 HANDLE apxLogOpen(
     APXHANDLE hPool,
     LPCWSTR szPath,
-    LPCWSTR szPrefix)
+    LPCWSTR szPrefix,
+    DWORD dwRotate)
 {
 
     WCHAR sPath[SIZ_PATHLEN];
@@ -127,11 +191,21 @@ HANDLE apxLogOpen(
     }
     if (!szPrefix)
         szPrefix = L"commons-daemon";
-    wsprintfW(sName, L"\\%s"  LOGF_EXT,
-              szPrefix,
-              sysTime.wYear,
-              sysTime.wMonth,
-              sysTime.wDay);
+    if (dwRotate != 0 && dwRotate < 86400)
+        wsprintfW(sName, L"\\%s"  LOGF_EXR,
+                  szPrefix,
+                  sysTime.wYear,
+                  sysTime.wMonth,
+                  sysTime.wDay,
+                  0,
+                  0,
+                  0);
+    else
+        wsprintfW(sName, L"\\%s"  LOGF_EXT,
+                  szPrefix,
+                  sysTime.wYear,
+                  sysTime.wMonth,
+                  sysTime.wDay);
     if (!(h = (apx_logfile_st *)apxPoolCalloc(hPool, sizeof(apx_logfile_st))))
         return INVALID_HANDLE_VALUE;
     /* Set default level to info */
@@ -140,10 +214,11 @@ HANDLE apxLogOpen(
 
     h->sysTime = sysTime;
     lstrlcpyW(h->szPath, MAX_PATH, sPath);
-    lstrlcatW(sPath, MAX_PATH, sName);
+    lstrlcpyW(h->szFile, MAX_PATH, sPath);
+    lstrlcatW(h->szFile, MAX_PATH, sName);
     lstrlcpyW(h->szPrefix, MAX_PATH, szPrefix);
 
-    h->hFile =  CreateFileW(sPath,
+    h->hFile =  CreateFileW(h->szFile,
                       GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                       NULL,
                       OPEN_ALWAYS,
@@ -153,9 +228,12 @@ HANDLE apxLogOpen(
         /* Make sure we write somewhere */
         h = &_st_sys_errhandle;
         apxDisplayError(FALSE, NULL, 0,
-                        "Unable to create logger at '%S'\n", sPath);
+                        "Unable to create logger at '%S'\n", h->szFile);
         return (HANDLE)h;
     }
+    else {
+        h->dwRotate = dwRotate;
+    }
     /* Set this file as system log file */
     if (!_st_sys_loghandle)
         _st_sys_loghandle = h;
@@ -226,7 +304,7 @@ apxLogWrite(
     }
     if (dwLevel < lf->dwLogLevel)
         return 0;
-    if (f) {
+    if (f && (lf->dwLogLevel == APXLOG_LEVEL_DEBUG || dwLevel == APXLOG_LEVEL_ERROR))
{
         f = (szFile + lstrlenA(szFile) - 1);
         while(f != szFile && '\\' != *f && '/' != *f)
             f--;
@@ -234,7 +312,7 @@ apxLogWrite(
             f++;
     }
     else
-        f = "";
+        f = NULL;
     szBp = buffer;
     if (!szFormat) {
         if (err == 0) {
@@ -271,6 +349,7 @@ apxLogWrite(
             GetLocalTime(&t);
             if (dolock) {
                 APX_LOGLOCK(lf->hFile);
+                logRotate(lf, &t);
             }
             if (bTimeStamp) {
                 wsprintfA(sb, "[%d-%02d-%02d %02d:%02d:%02d] ",



Mime
View raw message