- Changes in logging and debug output
[public/netxms.git] / src / server / core / evproc.cpp
1 /*
2 ** Project X - Network Management System
3 ** Copyright (C) 2003 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** $module: evproc.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25
26 //
27 // Handler for EnumerateSessions()
28 //
29
30 static void BroadcastEvent(ClientSession *pSession, void *pArg)
31 {
32 if (pSession->GetState() == STATE_AUTHENTICATED)
33 pSession->OnNewEvent((Event *)pArg);
34 }
35
36
37 //
38 // Event processing thread
39 //
40
41 THREAD_RESULT THREAD_CALL EventProcessor(void *arg)
42 {
43 Event *pEvent;
44
45 while(!ShutdownInProgress())
46 {
47 pEvent = (Event *)g_pEventQueue->GetOrBlock();
48 if (pEvent == INVALID_POINTER_VALUE)
49 break; // Shutdown indicator
50
51 // Write event to log if required
52 if (pEvent->Flags() & EF_LOG)
53 {
54 char *pszMsg, szQuery[1024];
55
56 pszMsg = EncodeSQLString(pEvent->Message());
57 sprintf(szQuery, "INSERT INTO event_log (event_id,event_timestamp,"
58 "event_source,event_severity,event_message) "
59 "VALUES (%d,%d,%d,%d,'%s')", pEvent->Id(), pEvent->TimeStamp(),
60 pEvent->SourceId(), pEvent->Severity(), pszMsg);
61 free(pszMsg);
62 DBQuery(g_hCoreDB, szQuery);
63 }
64
65 // Send event to all connected clients
66 EnumerateClientSessions(BroadcastEvent, pEvent);
67
68 // Write event information to screen if event debugging is on
69 if (IsStandalone() && (g_dwFlags & AF_DEBUG_EVENTS))
70 {
71 NetObj *pObject = FindObjectById(pEvent->SourceId());
72 if (pObject == NULL)
73 pObject = g_pEntireNet;
74 printf("EVENT %d (F:0x%04X S:%d) FROM %s: %s\n", pEvent->Id(),
75 pEvent->Flags(), pEvent->Severity(), pObject->Name(), pEvent->Message());
76 }
77
78 // Pass event through event processing policy
79 g_pEventPolicy->ProcessEvent(pEvent);
80
81 // Destroy event
82 delete pEvent;
83 }
84
85 DbgPrintf(AF_DEBUG_EVENTS, "Event processing thread #%d stopped", arg);
86 return THREAD_OK;
87 }