5875f6350672ced1baa536a896d57c46760a4497
[public/netxms.git] / src / nxlptest / nxlptest.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Log Parser Testing Utility
4 ** Copyright (C) 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: nxlptest.cpp
21 **
22 **/
23
24 #include "nxlptest.h"
25
26 #ifndef _WIN32
27 #include <signal.h>
28 #endif
29
30 //
31 // Static data
32 //
33
34 static CONDITION m_stopCondition = INVALID_CONDITION_HANDLE;
35
36
37 //
38 // Help text
39 //
40
41 static TCHAR m_helpText[] =
42 _T("NetXMS Log Parsing Tester Version ") NETXMS_VERSION_STRING _T("\n")
43 _T("Copyright (c) 2009-2012 Victor Kirhenshtein\n\n")
44 _T("Usage:\n")
45 _T(" nxlptest [options] parser\n\n")
46 _T("Where valid options are:\n")
47 _T(" -f file : Input file (overrides parser settings)\n")
48 _T(" -h : Show this help\n")
49 _T(" -i : Uses standard input instead of file defined in parser\n" )
50 _T(" -t level : Set trace level (overrides parser settings)\n")
51 _T(" -v : Show version and exit\n")
52 _T("\n");
53
54 /**
55 * Trace callback
56 */
57 static void TraceCallback(int level, const TCHAR *format, va_list args)
58 {
59 _vtprintf(format, args);
60 _puttc(_T('\n'), stdout);
61 }
62
63 /**
64 * Logger callback
65 */
66 static void LoggerCallback(int level, const TCHAR *format, va_list args)
67 {
68 TraceCallback(level, format, args);
69 }
70
71 /**
72 * File parsing thread
73 */
74 static THREAD_RESULT THREAD_CALL ParserThread(void *arg)
75 {
76 ((LogParser *)arg)->monitorFile(m_stopCondition);
77 return THREAD_OK;
78 }
79
80 #ifndef _WIN32
81
82 bool s_stop = false;
83
84 static void OnBreak(int sig)
85 {
86 s_stop = true;
87 }
88
89 #endif
90
91 /**
92 * main()
93 */
94 int main(int argc, char *argv[])
95 {
96 int rc = 0, ch, traceLevel = -1;
97 BYTE *xml;
98 UINT32 size;
99 TCHAR *inputFile = NULL;
100
101 InitNetXMSProcess(true);
102
103 // Parse command line
104 opterr = 1;
105 while((ch = getopt(argc, argv, "f:hit:v")) != -1)
106 {
107 switch(ch)
108 {
109 case 'h':
110 _tprintf(m_helpText);
111 return 0;
112 case 'v':
113 _tprintf(_T("NetXMS Log Parsing Tester Version ") NETXMS_VERSION_STRING _T("\n")
114 _T("Copyright (c) 2009-2017 Victor Kirhenshtein\n\n"));
115 return 0;
116 case 'f':
117 #ifdef UNICODE
118 inputFile = WideStringFromMBString(optarg);
119 #else
120 inputFile = optarg;
121 #endif
122 break;
123 case 't':
124 traceLevel = strtol(optarg, NULL, 0);
125 break;
126 case '?':
127 return 1;
128 default:
129 break;
130 }
131 }
132
133 if (argc - optind < 1)
134 {
135 _tprintf(_T("Required arguments missing\n"));
136 return 1;
137 }
138
139 InitLogParserLibrary();
140 SetLogParserTraceCallback(LoggerCallback);
141
142 #ifdef UNICODE
143 WCHAR *wname = WideStringFromMBString(argv[optind]);
144 xml = LoadFile(wname, &size);
145 free(wname);
146 #else
147 xml = LoadFile(argv[optind], &size);
148 #endif
149 if (xml != NULL)
150 {
151 TCHAR errorText[1024];
152 THREAD thread;
153
154 ObjectArray<LogParser> *parsers = LogParser::createFromXml((const char *)xml, size, errorText, 1024);
155 if ((parsers != NULL) && (parsers->size() > 0))
156 {
157 LogParser *parser = parsers->get(0);
158 parser->setTraceCallback(TraceCallback);
159 if (traceLevel != -1)
160 parser->setTraceLevel(traceLevel);
161 if (inputFile != NULL)
162 parser->setFileName(inputFile);
163
164 m_stopCondition = ConditionCreate(TRUE);
165 thread = ThreadCreateEx(ParserThread, 0, parser);
166 #ifdef _WIN32
167 _tprintf(_T("Parser started. Press ESC to stop.\nFile: %s\nTrace level: %d\n\n"),
168 parser->getFileName(), parser->getTraceLevel());
169 while(1)
170 {
171 ch = _getch();
172 if (ch == 27)
173 break;
174 }
175 #else
176 _tprintf(_T("Parser started. Press Ctrl+C to stop.\nFile: %s\nTrace level: %d\n\n"),
177 parser->getFileName(), parser->getTraceLevel());
178
179 signal(SIGINT, OnBreak);
180
181 sigset_t signals;
182 sigemptyset(&signals);
183 sigaddset(&signals, SIGINT);
184 pthread_sigmask(SIG_UNBLOCK, &signals, NULL);
185
186 while(!s_stop)
187 ThreadSleepMs(500);
188 #endif
189 ConditionSet(m_stopCondition);
190 ThreadJoin(thread);
191 ConditionDestroy(m_stopCondition);
192 }
193 else
194 {
195 _tprintf(_T("ERROR: invalid parser definition file (%s)\n"), errorText);
196 rc = 1;
197 }
198 free(xml);
199 }
200 else
201 {
202 _tprintf(_T("ERROR: unable to load parser definition file (%s)\n"), _tcserror(errno));
203 rc = 2;
204 }
205
206 CleanupLogParserLibrary();
207 return rc;
208 }