96db434ffab33a5bdf467c84471399a598e2e459
[public/netxms.git] / include / nxlpapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** Log Parsing Library
4 ** Copyright (C) 2008, 2009 Victor Kirhenshtein
5 **
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.
10 **
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.
15 **
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.
19 **
20 ** File: nxlpapi.h
21 **
22 **/
23
24 #ifndef _nxlpapi_h_
25 #define _nxlpapi_h_
26
27 #ifdef _WIN32
28 #ifdef LIBNXLP_EXPORTS
29 #define LIBNXLP_EXPORTABLE __declspec(dllexport)
30 #else
31 #define LIBNXLP_EXPORTABLE __declspec(dllimport)
32 #endif
33 #else /* _WIN32 */
34 #define LIBNXLP_EXPORTABLE
35 #endif
36
37
38 #ifdef _WIN32
39 #include <netxms-regex.h>
40 #else
41 #include <regex.h>
42 #endif
43
44
45 //
46 // Context actions
47 //
48
49 #define CONTEXT_SET_MANUAL 0
50 #define CONTEXT_SET_AUTOMATIC 1
51 #define CONTEXT_CLEAR 2
52
53
54 //
55 // Callback
56 // Parameters:
57 // event id, original text, number of parameters, list of parameters,
58 // object id, user arg
59 //
60
61 typedef void (* LogParserCallback)(DWORD, const char *, int, char **, DWORD, void *);
62
63
64 //
65 // Log parser rule
66 //
67
68 class LIBNXLP_EXPORTABLE LogParser;
69
70 class LIBNXLP_EXPORTABLE LogParserRule
71 {
72 private:
73 LogParser *m_parser;
74 regex_t m_preg;
75 DWORD m_event;
76 bool m_isValid;
77 int m_numParams;
78 regmatch_t *m_pmatch;
79 TCHAR *m_regexp;
80 TCHAR *m_source;
81 DWORD m_level;
82 DWORD m_idStart;
83 DWORD m_idEnd;
84 TCHAR *m_context;
85 int m_contextAction;
86 TCHAR *m_contextToChange;
87 bool m_isInverted;
88 bool m_breakOnMatch;
89 TCHAR *m_description;
90
91 void expandMacros(const char *regexp, String &out);
92
93 public:
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);
98 ~LogParserRule();
99
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);
104
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; }
108
109 void setInverted(bool flag) { m_isInverted = flag; }
110 BOOL isInverted() { return m_isInverted; }
111
112 void setBreakFlag(bool flag) { m_breakOnMatch = flag; }
113 BOOL getBreakFlag() { return m_breakOnMatch; }
114
115 const TCHAR *getContext() { return m_context; }
116 const TCHAR *getContextToChange() { return m_contextToChange; }
117 int getContextAction() { return m_contextAction; }
118
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); }
121
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); }
124
125 void setLevel(DWORD level) { m_level = level; }
126 DWORD getLevel() { return m_level; }
127
128 void setIdRange(DWORD start, DWORD end) { m_idStart = start; m_idEnd = end; }
129 QWORD getIdRange() { return ((QWORD)m_idStart << 32) | (QWORD)m_idEnd; }
130
131 const TCHAR *getRegexpSource() { return CHECK_NULL(m_regexp); }
132 };
133
134
135 //
136 // Log parser class
137 //
138
139 class LIBNXLP_EXPORTABLE LogParser
140 {
141 friend bool LogParserRule::match(const char *, LogParserCallback, DWORD, void *);
142 friend bool LogParserRule::matchEx(const TCHAR *, DWORD, DWORD, const char *, LogParserCallback, DWORD, void *);
143
144 private:
145 int m_numRules;
146 LogParserRule **m_rules;
147 StringMap m_contexts;
148 StringMap m_macros;
149 LogParserCallback m_cb;
150 void *m_userArg;
151 TCHAR *m_fileName;
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;
158 int m_traceLevel;
159 void (*m_traceCallback)(const TCHAR *, va_list);
160
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);
164
165 public:
166 LogParser();
167 ~LogParser();
168
169 bool createFromXml(const char *xml, int xmlLen = -1, char *errorText = NULL, int errBufSize = 0);
170
171 void setFileName(const TCHAR *name);
172 const TCHAR *getFileName() { return m_fileName; }
173
174 void setThread(THREAD th) { m_thread = th; }
175 THREAD getThread() { return m_thread; }
176
177 void setProcessAllFlag(bool flag) { m_processAllRules = flag; }
178 bool getProcessAllFlag() { return m_processAllRules; }
179
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);
187
188 void addMacro(const TCHAR *name, const TCHAR *value);
189 const TCHAR *getMacro(const TCHAR *name);
190
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);
193
194 int getProcessedRecordsCount() { return m_recordsProcessed; }
195 int getMatchedRecordsCount() { return m_recordsMatched; }
196
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; }
200
201 #ifdef _WIN32
202 bool monitorFile(HANDLE stopEvent, void (*logger)(int, const TCHAR *, ...), bool readFromCurrPos = true);
203 #else
204 bool monitorFile(CONDITION stopCondition, bool *stopFlag, void (*logger)(int, const TCHAR *, ...), bool readFromCurrPos = true);
205 #endif
206 };
207
208 #endif