log parser status kept as integer; minor refactoing
authorVictor Kirhenshtein <victor@netxms.org>
Fri, 29 Sep 2017 16:48:48 +0000 (19:48 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Fri, 29 Sep 2017 16:55:34 +0000 (19:55 +0300)
include/nxlpapi.h
src/agent/subagents/logwatch/logwatch.cpp
src/libnxlp/eventlog.cpp
src/libnxlp/file.cpp
src/libnxlp/parser.cpp
src/libnxlp/wevt.cpp

index 5f0a034..e6f2bea 100644 (file)
 #include <netxms-regex.h>
 #include <nms_util.h>
 
+/**
+ * Parser status
+ */
+enum LogParserStatus
+{
+   LPS_INIT                = 0,
+   LPS_RUNNING             = 1,
+   LPS_NO_FILE             = 2,
+   LPS_OPEN_ERROR          = 3,
+   LPS_SUSPENDED           = 4,
+   LPS_EVT_SUBSCRIBE_ERROR = 5,
+   LPS_EVT_READ_ERROR      = 6,
+   LPS_EVT_OPEN_ERROR      = 7
+};
 
-//
-// Parser status
-//
-
-#define MAX_PARSER_STATUS_LEN  64
-
-#define LPS_INIT              _T("INIT")
-#define LPS_RUNNING           _T("RUNNING")
-#define LPS_NO_FILE           _T("FILE MISSING")
-#define LPS_OPEN_ERROR        _T("FILE OPEN ERROR")
-
-
-//
-// Context actions
-//
-
-#define CONTEXT_SET_MANUAL    0
-#define CONTEXT_SET_AUTOMATIC 1
-#define CONTEXT_CLEAR         2
-
-
-//
-// File encoding
-//
+/**
+ * Context actions
+ */
+enum LogParserContextAction
+{
+   CONTEXT_SET_MANUAL    = 0,
+   CONTEXT_SET_AUTOMATIC = 1,
+   CONTEXT_CLEAR         = 2
+};
 
-#define LP_FCP_AUTO    -1
-#define LP_FCP_ACP      0
-#define LP_FCP_UTF8     1
-#define LP_FCP_UCS2     2
-#define LP_FCP_UCS2_LE  3
-#define LP_FCP_UCS2_BE  4
-#define LP_FCP_UCS4     5
-#define LP_FCP_UCS4_LE  6
-#define LP_FCP_UCS4_BE  7
+/**
+ * File encoding
+ */
+enum LogParserFileEncoding
+{
+   LP_FCP_AUTO    = -1,
+   LP_FCP_ACP     = 0,
+   LP_FCP_UTF8    = 1,
+   LP_FCP_UCS2    = 2,
+   LP_FCP_UCS2_LE = 3,
+   LP_FCP_UCS2_BE = 4,
+   LP_FCP_UCS4    = 5,
+   LP_FCP_UCS4_LE = 6,
+   LP_FCP_UCS4_BE = 7
+};
 
 /**
  * Log parser callback
@@ -222,7 +227,7 @@ private:
    bool m_suspended;
        int m_traceLevel;
        void (*m_traceCallback)(int, const TCHAR *, va_list);
-       TCHAR m_status[MAX_PARSER_STATUS_LEN];
+       LogParserStatus m_status;
 #ifdef _WIN32
    TCHAR *m_marker;
 #endif
@@ -237,7 +242,7 @@ private:
 
    int getCharSize() const;
 
-   void setStatus(const TCHAR *status) { nx_strncpy(m_status, status, MAX_PARSER_STATUS_LEN); }
+   void setStatus(LogParserStatus status) { m_status = status; }
 
 #ifdef _WIN32
    void parseEvent(EVENTLOGRECORD *rec);
@@ -259,7 +264,8 @@ public:
    const TCHAR *getName() const { return m_name; }
        const TCHAR *getFileName() const { return m_fileName; }
        int getFileEncoding() const { return m_fileEncoding; }
-   const TCHAR *getStatus() const { return m_status; }
+   LogParserStatus getStatus() const { return m_status; }
+   const TCHAR *getStatusText() const;
    bool isFilePreallocated() const { return m_preallocatedFile; }
 
        void setName(const TCHAR *name);
index dfa1464..25daadf 100644 (file)
@@ -89,7 +89,7 @@ static LONG H_ParserStats(const TCHAR *cmd, const TCHAR *arg, TCHAR *value, Abst
        switch(*arg)
        {
                case 'S':       // Status
-                       ret_string(value, parser->getStatus());
+                       ret_string(value, parser->getStatusText());
                        break;
                case 'M':       // Matched records
                        ret_int(value, parser->getMatchedRecordsCount());
index d0fa4fe..4b4b1bf 100644 (file)
@@ -356,7 +356,7 @@ retry_read:
                                {
                                        LogParserTrace(0, _T("LogWatch: Unable to read event log \"%s\": %s"),
                                                                                &m_fileName[1], GetSystemErrorText(GetLastError(), (TCHAR *)buffer, bufferSize / sizeof(TCHAR)));
-                                       setStatus(_T("EVENT LOG READ ERROR"));
+                                       setStatus(LPS_EVT_READ_ERROR);
                                }
          }
 
@@ -387,7 +387,7 @@ retry_read:
    {
                LogParserTrace(0, _T("LogWatch: Unable to open event log \"%s\": %s"),
                               &m_fileName[1], GetSystemErrorText(GetLastError(), (TCHAR *)buffer, bufferSize / sizeof(TCHAR)));
-               setStatus(_T("EVENT LOG OPEN ERROR"));
+               setStatus(LPS_EVT_OPEN_ERROR);
       result = false;
    }
 
index 701a322..9c1eb76 100644 (file)
@@ -453,6 +453,7 @@ bool LogParser::monitorFile(CONDITION stopCondition, bool readFromCurrPos)
              {
                 exclusionPeriod = true;
             LogParserTrace(6, _T("LogParser: will not open file \"%s\" because of exclusion period"), getFileName());
+            setStatus(LPS_SUSPENDED);
              }
          if (ConditionWait(stopCondition, 30000))
             break;
@@ -514,21 +515,12 @@ bool LogParser::monitorFile(CONDITION stopCondition, bool readFromCurrPos)
                                                break;
                                        }
 
-#ifdef _NETWARE
-                                       if (fgetstat(fh, &st, ST_SIZE_BIT | ST_NAME_BIT) < 0)
-                                       {
-                                               LogParserTrace(1, _T("LogParser: fgetstat(%d) failed, errno=%d"), fh, errno);
-                                               readFromStart = true;
-                                               break;
-                                       }
-#else
                                        if (NX_FSTAT(fh, &st) < 0)
                                        {
                                                LogParserTrace(1, _T("LogParser: fstat(%d) failed, errno=%d"), fh, errno);
                                                readFromStart = true;
                                                break;
                                        }
-#endif
 
                                        if (CALL_STAT(fname, &stn) < 0)
                                        {
@@ -600,6 +592,7 @@ bool LogParser::monitorFile(CONDITION stopCondition, bool readFromCurrPos)
                                        {
                   LogParserTrace(6, _T("LogParser: closing file \"%s\" because of exclusion period"), fname);
                   exclusionPeriod = true;
+                  setStatus(LPS_SUSPENDED);
                                           break;
                                        }
                                }
index 175b41d..be1518c 100644 (file)
@@ -121,7 +121,7 @@ LogParser::LogParser()
    m_suspended = false;
        m_traceLevel = 0;
        m_traceCallback = NULL;
-       _tcscpy(m_status, LPS_INIT);
+       m_status = LPS_INIT;
 #ifdef _WIN32
    m_marker = NULL;
 #endif
@@ -167,7 +167,7 @@ LogParser::LogParser(const LogParser *src)
    m_suspended = src->m_suspended;
        m_traceLevel = src->m_traceLevel;
        m_traceCallback = src->m_traceCallback;
-       _tcscpy(m_status, LPS_INIT);
+       m_status = LPS_INIT;
 #ifdef _WIN32
    m_marker = _tcsdup_ex(src->m_marker);
 #endif
@@ -935,3 +935,21 @@ void LogParser::resume()
 {
    m_suspended = false;
 }
+
+/**
+ * Get status text
+ */
+const TCHAR *LogParser::getStatusText() const
+{
+   static const TCHAR *texts[] = {
+      _T("INIT"),
+      _T("RUNNING"),
+      _T("FILE MISSING"),
+      _T("FILE OPEN ERROR"),
+      _T("SUSPENDED"),
+      _T("EVENT LOG SUBSCRIBE FAILED"),
+      _T("EVENT LOG READ ERROR"),
+      _T("EVENT LOG OPEN ERROR")
+   };
+   return texts[m_status];
+}
index e190a08..69aed5a 100644 (file)
@@ -272,7 +272,7 @@ bool LogParser::monitorEventLogV6(CONDITION stopCondition)
                TCHAR buffer[1024];
                LogParserTrace(0, _T("LogWatch: Unable to open event log \"%s\" with EvtSubscribe(): %s"),
                               &m_fileName[1], GetSystemErrorText(GetLastError(), buffer, 1024));
-               setStatus(_T("EVENT LOG SUBSCRIBE FAILED"));
+               setStatus(LPS_EVT_SUBSCRIBE_ERROR);
       success = false;
        }
    return success;