2 ** NetXMS - Network Management System
4 ** Copyright (C) 2008, 2009 Victor Kirhenshtein
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #ifdef LIBNXLP_EXPORTS
29 #define LIBNXLP_EXPORTABLE __declspec(dllexport)
31 #define LIBNXLP_EXPORTABLE __declspec(dllimport)
34 #define LIBNXLP_EXPORTABLE
39 #include <netxms-regex.h>
49 #define CONTEXT_SET_MANUAL 0
50 #define CONTEXT_SET_AUTOMATIC 1
51 #define CONTEXT_CLEAR 2
57 // event id, original text, number of parameters, list of parameters,
58 // object id, user arg
61 typedef void (* LogParserCallback
)(DWORD
, const char *, int, char **, DWORD
, void *);
68 class LIBNXLP_EXPORTABLE LogParser
;
70 class LIBNXLP_EXPORTABLE LogParserRule
86 TCHAR
*m_contextToChange
;
91 void expandMacros(const char *regexp
, String
&out
);
94 LogParserRule(LogParser
*parser
,
95 const char *regexp
, DWORD event
= 0, int numParams
= 0,
96 const TCHAR
*source
= NULL
, DWORD level
= 0xFFFFFFFF,
97 DWORD idStart
= 0, DWORD idEnd
= 0xFFFFFFFF);
100 bool isValid() { return m_isValid
; }
101 bool match(const char *line
, LogParserCallback cb
, DWORD objectId
, void *userArg
);
102 bool matchEx(const TCHAR
*source
, DWORD eventId
, DWORD level
,
103 const char *line
, LogParserCallback cb
, DWORD objectId
, void *userArg
);
105 void setContext(const TCHAR
*context
) { safe_free(m_context
); m_context
= (context
!= NULL
) ?
_tcsdup(context
) : NULL
; }
106 void setContextToChange(const TCHAR
*context
) { safe_free(m_contextToChange
); m_contextToChange
= (context
!= NULL
) ?
_tcsdup(context
) : NULL
; }
107 void setContextAction(int action
) { m_contextAction
= action
; }
109 void setInverted(bool flag
) { m_isInverted
= flag
; }
110 BOOL
isInverted() { return m_isInverted
; }
112 void setBreakFlag(bool flag
) { m_breakOnMatch
= flag
; }
113 BOOL
getBreakFlag() { return m_breakOnMatch
; }
115 const TCHAR
*getContext() { return m_context
; }
116 const TCHAR
*getContextToChange() { return m_contextToChange
; }
117 int getContextAction() { return m_contextAction
; }
119 void setDescription(const TCHAR
*descr
) { safe_free(m_description
); m_description
= (descr
!= NULL
) ?
_tcsdup(descr
) : NULL
; }
120 const TCHAR
*getDescription() { return CHECK_NULL_EX(m_description
); }
122 void setSource(const TCHAR
*source
) { safe_free(m_source
); m_source
= (source
!= NULL
) ?
_tcsdup(source
) : NULL
; }
123 const TCHAR
*getSource() { return CHECK_NULL_EX(m_source
); }
125 void setLevel(DWORD level
) { m_level
= level
; }
126 DWORD
getLevel() { return m_level
; }
128 void setIdRange(DWORD start
, DWORD end
) { m_idStart
= start
; m_idEnd
= end
; }
129 QWORD
getIdRange() { return ((QWORD
)m_idStart
<< 32) | (QWORD
)m_idEnd
; }
131 const TCHAR
*getRegexpSource() { return CHECK_NULL(m_regexp
); }
139 class LIBNXLP_EXPORTABLE LogParser
141 friend bool LogParserRule
::match(const char *, LogParserCallback
, DWORD
, void *);
142 friend bool LogParserRule
::matchEx(const TCHAR
*, DWORD
, DWORD
, const char *, LogParserCallback
, DWORD
, void *);
146 LogParserRule
**m_rules
;
147 StringMap m_contexts
;
149 LogParserCallback m_cb
;
152 CODE_TO_TEXT
*m_eventNameList
;
153 bool (*m_eventResolver
)(const TCHAR
*, DWORD
*);
154 THREAD m_thread
; // Associated thread
155 int m_recordsProcessed
;
156 int m_recordsMatched
;
157 bool m_processAllRules
;
159 void (*m_traceCallback
)(const TCHAR
*, va_list);
161 const TCHAR
*checkContext(LogParserRule
*rule
);
162 void trace(int level
, const TCHAR
*format
, ...);
163 bool matchLogRecord(bool hasAttributes
, const char *source
, DWORD eventId
, DWORD level
, const char *line
, DWORD objectId
);
169 bool createFromXml(const char *xml
, int xmlLen
= -1, char *errorText
= NULL
, int errBufSize
= 0);
171 void setFileName(const TCHAR
*name
);
172 const TCHAR
*getFileName() { return m_fileName
; }
174 void setThread(THREAD th
) { m_thread
= th
; }
175 THREAD
getThread() { return m_thread
; }
177 void setProcessAllFlag(bool flag
) { m_processAllRules
= flag
; }
178 bool getProcessAllFlag() { return m_processAllRules
; }
180 bool addRule(const char *regexp
, DWORD event
= 0, int numParams
= 0);
181 bool addRule(LogParserRule
*rule
);
182 void setCallback(LogParserCallback cb
) { m_cb
= cb
; }
183 void setUserArg(void *arg
) { m_userArg
= arg
; }
184 void setEventNameList(CODE_TO_TEXT
*ctt
) { m_eventNameList
= ctt
; }
185 void setEventNameResolver(bool (*cb
)(const TCHAR
*, DWORD
*)) { m_eventResolver
= cb
; }
186 DWORD
resolveEventName(const TCHAR
*name
, DWORD defVal
= 0);
188 void addMacro(const TCHAR
*name
, const TCHAR
*value
);
189 const TCHAR
*getMacro(const TCHAR
*name
);
191 bool matchLine(const char *line
, DWORD objectId
= 0);
192 bool matchEvent(const char *source
, DWORD eventId
, DWORD level
, const char *line
, DWORD objectId
= 0);
194 int getProcessedRecordsCount() { return m_recordsProcessed
; }
195 int getMatchedRecordsCount() { return m_recordsMatched
; }
197 int getTraceLevel() { return m_traceLevel
; }
198 void setTraceLevel(int level
) { m_traceLevel
= level
; }
199 void setTraceCallback(void (*cb
)(const TCHAR
*, va_list)) { m_traceCallback
= cb
; }
202 bool monitorFile(HANDLE stopEvent
, void (*logger
)(int, const TCHAR
*, ...), bool readFromCurrPos
= true);
204 bool monitorFile(CONDITION stopCondition
, bool *stopFlag
, void (*logger
)(int, const TCHAR
*, ...), bool readFromCurrPos
= true);