77#include " config_reader.h"
88#include " utils.h"
99
10+ #include " detail/syslog.h" // for syslog_dispatch
11+
1012#include " xrt/detail/version-git.h"
1113
1214#include < algorithm>
1921#include < mutex>
2022#include < thread>
2123#ifdef __linux__
22- # include < syslog.h>
2324# include < linux/limits.h>
2425# include < sys/stat.h>
2526# include < sys/types.h>
2627#endif
27- #ifdef _WIN32
28- # include < winsock.h>
29- // Windows Event Log API - required for syslog_dispatch
30- # include < windows.h>
31- #endif
3228
3329namespace {
3430
@@ -56,17 +52,7 @@ get_exe_path()
5652
5753
5854using severity_level = xrt_core::message::severity_level;
59-
60- // --
61- class message_dispatch
62- {
63- public:
64- message_dispatch () {}
65- virtual ~message_dispatch () {}
66- static message_dispatch* make_dispatcher (const std::string& choice);
67- public:
68- virtual void send (severity_level l, const char * tag, const char * msg) = 0;
69- };
55+ using message_dispatch = xrt_core::message::message_dispatch;
7056
7157// --
7258class null_dispatch : public message_dispatch
@@ -98,122 +84,6 @@ class console_dispatch : public message_dispatch
9884 };
9985};
10086
101- // syslog_dispatch: routes to the OS-level centralized log on each platform.
102- // Linux -> POSIX syslog
103- //
104- // Windows -> Windows Application Event Log under source "AMD_XRT"
105- // Filter in Event Viewer:
106- // Windows Logs -> Application -> Source: AMD_XRT
107- // Filter via PowerShell:
108- // Get-EventLog -LogName Application -Source "AMD_XRT" -- all
109- // Get-EventLog -LogName Application -Source "AMD_XRT" -EntryType Error -- errors only
110- // Get-EventLog -LogName Application -Source "AMD_XRT" -EntryType Error,Warning
111- // Filter via cmd line (Level: 2=Error, 3=Warning, 4=Information):
112- // wevtutil qe Application /q:"*[System[Provider[@Name='AMD_XRT']]]" /f:text
113- // wevtutil qe Application /q:"*[System[Provider[@Name='AMD_XRT'] and Level=2]]" /f:text
114- // wevtutil qe Application /q:"*[System[Provider[@Name='AMD_XRT'] and (Level=2 or Level=3)]]" /f:text
115- #ifdef _WIN32
116- class syslog_dispatch : public message_dispatch
117- {
118- public:
119- syslog_dispatch ()
120- {
121- // RegisterEventSourceA opens a handle to the Application event log and
122- // associates it with the source name "AMD_XRT". This handle is used in
123- // every subsequent ReportEventA call to stamp events with that source name.
124- // First arg (nullptr) means the local machine; a UNC server name can be
125- // passed to write to a remote machine's event log.
126- // This call does NOT require admin rights and does NOT touch the registry —
127- // it only opens a write channel. Registry registration of "AMD_XRT" as a
128- // known source is done separately at driver install time via the INF AddReg
129- // directive
130- m_handle = RegisterEventSourceA (nullptr , " AMD_XRT" );
131- // Do not throw on failure — syslog_dispatch is constructed lazily on the
132- // first call to xrt_core::message::send(), so a throw here would crash the
133- // application during normal logging if the Windows Event Log service is
134- // unavailable.
135- if (!m_handle)
136- std::cerr << " XRT: Failed to open Windows Event Log source 'AMD_XRT' "
137- << " (error " << GetLastError () << " ). Logging disabled.\n " ;
138- }
139-
140- virtual ~syslog_dispatch ()
141- {
142- if (m_handle)
143- DeregisterEventSource (m_handle);
144- }
145-
146- void
147- send (severity_level l, const char * tag, const char * msg) override
148- {
149- if (!m_handle)
150- return ; // Logging is unavailable if we failed to open the event source
151-
152- // Combine tag and message so Event Viewer shows "[xrt_elf] : some message"
153- std::string full_msg = std::string (" [" ) + tag + " ] : " + msg;
154- LPCSTR strings[] = {full_msg.c_str ()};
155- // Event ID 1 matches the pass-through entry in EventCreate.exe's message table
156- // (%1 format string), so Event Viewer displays our message text directly
157- // without the warning.
158- // Severity filtering in Event Viewer uses wType (Level column), not event ID.
159- static constexpr DWORD event_id = 1 ;
160- ReportEventA (m_handle, to_event_type (l), 0 , event_id,
161- nullptr , 1 , 0 , strings, nullptr );
162- }
163-
164- private:
165- HANDLE m_handle = nullptr ;
166-
167- // Maps XRT severity to Windows event type (controls icon in Event Viewer):
168- // EVENTLOG_ERROR_TYPE -> red X
169- // EVENTLOG_WARNING_TYPE -> yellow triangle
170- // EVENTLOG_INFORMATION_TYPE -> blue i
171- static WORD
172- to_event_type (severity_level l)
173- {
174- switch (l) {
175- case severity_level::emergency:
176- case severity_level::alert:
177- case severity_level::critical:
178- case severity_level::error:
179- return EVENTLOG_ERROR_TYPE;
180- case severity_level::warning:
181- return EVENTLOG_WARNING_TYPE;
182- default :
183- return EVENTLOG_INFORMATION_TYPE;
184- }
185- }
186-
187- };
188- #else
189- class syslog_dispatch : public message_dispatch
190- {
191- public:
192- syslog_dispatch ()
193- { openlog (" sdaccel" , LOG_PID|LOG_CONS, LOG_USER); }
194-
195- virtual ~syslog_dispatch ()
196- { closelog (); }
197-
198- void
199- send (severity_level l, const char * tag, const char * msg) override
200- { syslog (severityMap[l], " %s" , msg); }
201-
202- private:
203- // Maps XRT severity to POSIX syslog priority
204- std::map<severity_level, int > severityMap = {
205- { severity_level::emergency, LOG_EMERG},
206- { severity_level::alert, LOG_ALERT},
207- { severity_level::critical, LOG_CRIT},
208- { severity_level::error, LOG_ERR},
209- { severity_level::warning, LOG_WARNING},
210- { severity_level::notice, LOG_NOTICE},
211- { severity_level::info, LOG_INFO},
212- { severity_level::debug, LOG_DEBUG}
213- };
214- };
215- #endif
216-
21787// --
21888class file_dispatch : public message_dispatch
21989{
@@ -236,29 +106,6 @@ class file_dispatch : public message_dispatch
236106 };
237107};
238108
239- // -------
240- message_dispatch*
241- message_dispatch::
242- make_dispatcher (const std::string& choice)
243- {
244- if ( (choice == " null" ) || (choice == " " ))
245- return new null_dispatch;
246- else if (choice == " console" )
247- return new console_dispatch;
248- else if (choice == " syslog" )
249- return new syslog_dispatch;
250- else {
251- if (choice.front () == ' "' ) {
252- std::string file = choice;
253- file.erase (0 , 1 );
254- file.erase (file.size ()-1 );
255- return new file_dispatch (file);
256- }
257- else
258- return new file_dispatch (choice);
259- }
260- }
261-
262109// file ops
263110file_dispatch::
264111file_dispatch (const std::string &file)
@@ -319,6 +166,27 @@ send(severity_level l, const char* tag, const char* msg)
319166
320167namespace xrt_core { namespace message {
321168
169+ message_dispatch*
170+ message_dispatch::
171+ make_dispatcher (const std::string& choice)
172+ {
173+ if ((choice == " null" ) || (choice == " " ))
174+ return new null_dispatch;
175+ else if (choice == " console" )
176+ return new console_dispatch;
177+ else if (choice == " syslog" )
178+ return new syslog_dispatch;
179+ else {
180+ if (choice.front () == ' "' ) {
181+ std::string file = choice;
182+ file.erase (0 , 1 );
183+ file.erase (file.size ()-1 );
184+ return new file_dispatch (file);
185+ }
186+ return new file_dispatch (choice);
187+ }
188+ }
189+
322190void
323191send (severity_level l, const char * tag, const char * msg)
324192{
0 commit comments