Skip to content

Commit e617963

Browse files
Add separate headers for platform specific code
Signed-off-by: Rahul Bramandlapalli <rbramand@amd.com>
1 parent ebbbd44 commit e617963

5 files changed

Lines changed: 176 additions & 171 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
3+
4+
// POSIX syslog implementation of syslog_dispatch.
5+
6+
#include "core/common/message.h"
7+
8+
#include <map>
9+
#include <syslog.h>
10+
11+
namespace xrt_core { namespace message {
12+
13+
class syslog_dispatch : public message_dispatch
14+
{
15+
public:
16+
syslog_dispatch()
17+
{ openlog("sdaccel", LOG_PID|LOG_CONS, LOG_USER); }
18+
19+
~syslog_dispatch()
20+
{ closelog(); }
21+
22+
void
23+
send(severity_level l, const char* tag, const char* msg) override
24+
{ syslog(m_severity_map[l], "%s", msg); }
25+
26+
private:
27+
std::map<severity_level, int> m_severity_map = {
28+
{ severity_level::emergency, LOG_EMERG },
29+
{ severity_level::alert, LOG_ALERT },
30+
{ severity_level::critical, LOG_CRIT },
31+
{ severity_level::error, LOG_ERR },
32+
{ severity_level::warning, LOG_WARNING },
33+
{ severity_level::notice, LOG_NOTICE },
34+
{ severity_level::info, LOG_INFO },
35+
{ severity_level::debug, LOG_DEBUG }
36+
};
37+
};
38+
39+
}} // xrt_core::message
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
3+
#ifndef core_common_detail_syslog_h
4+
#define core_common_detail_syslog_h
5+
6+
#ifdef _WIN32
7+
# include "core/common/detail/windows/syslog.h"
8+
#else
9+
# include "core/common/detail/linux/syslog.h"
10+
#endif
11+
12+
#endif
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
3+
4+
// Windows Event Log implementation of syslog_dispatch.
5+
// Routes XRT messages to the Application event log under source "AMD_XRT".
6+
//
7+
// Filter in Event Viewer:
8+
// Windows Logs -> Application -> Source: AMD_XRT
9+
// Filter via PowerShell:
10+
// Get-EventLog -LogName Application -Source "AMD_XRT"
11+
// Get-EventLog -LogName Application -Source "AMD_XRT" -EntryType Error
12+
// Get-EventLog -LogName Application -Source "AMD_XRT" -EntryType Error,Warning
13+
// Filter via cmd (Level: 2=Error, 3=Warning, 4=Information):
14+
// wevtutil qe Application /q:"*[System[Provider[@Name='AMD_XRT']]]" /f:text
15+
// wevtutil qe Application /q:"*[System[Provider[@Name='AMD_XRT'] and Level=2]]" /f:text
16+
// wevtutil qe Application /q:"*[System[Provider[@Name='AMD_XRT'] and (Level=2 or Level=3)]]" /f:text
17+
18+
#include "core/common/message.h"
19+
20+
#include <iostream>
21+
#include <string>
22+
#include <windows.h>
23+
24+
namespace xrt_core { namespace message {
25+
26+
class syslog_dispatch : public message_dispatch
27+
{
28+
public:
29+
syslog_dispatch()
30+
{
31+
// RegisterEventSourceA opens a handle to the Application event log and
32+
// associates it with the source name "AMD_XRT". Registry registration of
33+
// "AMD_XRT" as a known source is done at driver install time via the INF
34+
// AddReg directive. This call does not require admin rights.
35+
m_handle = RegisterEventSourceA(nullptr, "AMD_XRT");
36+
// Do not throw on failure: this is constructed lazily on first
37+
// xrt_core::message::send(), so a throw here would crash the application
38+
// if the Windows Event Log service is unavailable.
39+
if (!m_handle)
40+
std::cerr << "XRT: Failed to open Windows Event Log source 'AMD_XRT' "
41+
<< "(error " << GetLastError() << "). Logging disabled.\n";
42+
}
43+
44+
~syslog_dispatch()
45+
{
46+
if (m_handle)
47+
DeregisterEventSource(m_handle);
48+
}
49+
50+
void
51+
send(severity_level l, const char* tag, const char* msg) override
52+
{
53+
if (!m_handle)
54+
return;
55+
56+
std::string full_msg = std::string("[") + tag + "] : " + msg;
57+
LPCSTR strings[] = {full_msg.c_str()};
58+
// Event ID 1 matches the pass-through entry in EventCreate.exe's message
59+
// table (%1), so Event Viewer displays our text directly without a warning.
60+
// Severity filtering uses wType (Level column), not event ID.
61+
static constexpr DWORD event_id = 1;
62+
ReportEventA(m_handle, to_event_type(l), 0, event_id,
63+
nullptr, 1, 0, strings, nullptr);
64+
}
65+
66+
private:
67+
HANDLE m_handle = nullptr;
68+
69+
// Maps XRT severity to Windows event type (controls icon in Event Viewer):
70+
// EVENTLOG_ERROR_TYPE -> red X
71+
// EVENTLOG_WARNING_TYPE -> yellow triangle
72+
// EVENTLOG_INFORMATION_TYPE -> blue i
73+
static WORD
74+
to_event_type(severity_level l)
75+
{
76+
switch (l) {
77+
case severity_level::emergency:
78+
case severity_level::alert:
79+
case severity_level::critical:
80+
case severity_level::error:
81+
return EVENTLOG_ERROR_TYPE;
82+
case severity_level::warning:
83+
return EVENTLOG_WARNING_TYPE;
84+
default:
85+
return EVENTLOG_INFORMATION_TYPE;
86+
}
87+
}
88+
};
89+
90+
}} // xrt_core::message

src/runtime_src/core/common/message.cpp

Lines changed: 24 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
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>
@@ -19,16 +21,10 @@
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

3329
namespace {
3430

@@ -56,17 +52,7 @@ get_exe_path()
5652

5753

5854
using 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
//--
7258
class 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
//--
21888
class 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
263110
file_dispatch::
264111
file_dispatch(const std::string &file)
@@ -319,6 +166,27 @@ send(severity_level l, const char* tag, const char* msg)
319166

320167
namespace 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+
322190
void
323191
send(severity_level l, const char* tag, const char* msg)
324192
{

0 commit comments

Comments
 (0)