Skip to content

Commit ed499ce

Browse files
dantraMSFTdaxian-dbw
authored andcommitted
Use native os_log APIs on OSX for PowerShell logging (PowerShell#5310)
1 parent 819f9a3 commit ed499ce

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

src/libpsl-native/src/nativesyslog.cpp

100644100755
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,65 @@
22
//! @brief Provides wrappers around the syslog apis to support exporting
33
//! for PInvoke calls by powershell.
44
//! These functions are intended only for PowerShell internal use.
5+
//! To view log output in real time
6+
//! On Linux
7+
//! tail -f /var/log/syslog | grep powershell
8+
//! On OSX
9+
//! sudo log stream
10+
//! NOTE: replace powershell with the LogIdentity value when overriding in configuration
511
#include <syslog.h>
612
#include <nativesyslog.h>
713

14+
#if defined(__APPLE__) && defined(__MACH__)
15+
16+
#include <os/log.h>
17+
#include <stdio.h>
18+
static os_log_t _log = NULL;
19+
20+
// This format string ensures the message string for the log statements
21+
// are visible. Using just %s marks them as private and the do not show
22+
// up with log stream.
23+
#define MESSAGE_FORMAT "%{public}s"
24+
25+
// The submodule name to pass to os_log_create.
26+
// The passed in ident value will be used for the category.
27+
// see man os_log_create
28+
#define SUBMODULE_NAME "com.microsoft.powershell"
29+
30+
#endif
31+
832
//! @brief Native_SysLog is a wrapper around the syslog api.
933
//! It explicitly passes the message as a parameter to a %s format
10-
//! string since the message may have arbitray characters that can
34+
//! string since the message may have arbitrary characters that can
1135
//! be misinterpreted as format specifiers.
1236
//!
1337
//! @retval none.
1438
extern "C" void Native_SysLog(int32_t priority, const char* message)
1539
{
40+
#if defined(__APPLE__) && defined(__MACH__)
41+
switch (priority)
42+
{
43+
case LOG_EMERG:
44+
case LOG_ALERT:
45+
case LOG_CRIT:
46+
os_log_fault(_log, MESSAGE_FORMAT, message);
47+
break;
48+
49+
case LOG_ERR:
50+
os_log_error(_log, MESSAGE_FORMAT, message);
51+
break;
52+
53+
case LOG_DEBUG:
54+
os_log_debug(_log, MESSAGE_FORMAT, message);
55+
break;
56+
57+
default:
58+
os_log(_log, MESSAGE_FORMAT, message);
59+
break;
60+
}
61+
#else
1662
syslog(priority, "%s", message);
63+
#endif
1764
}
1865

1966
//! @brief Native_OpenLog is a wrapper around the openlog, syslog api.
@@ -23,13 +70,21 @@ extern "C" void Native_SysLog(int32_t priority, const char* message)
2370
//! @retval none.
2471
extern "C" void Native_OpenLog(const char* ident, int facility)
2572
{
73+
#if defined(__APPLE__) && defined(__MACH__)
74+
_log = os_log_create(SUBMODULE_NAME, ident);
75+
#else
2676
openlog(ident, LOG_NDELAY | LOG_PID, facility);
77+
#endif
2778
}
2879

2980
//! @brief Native_OpenLog is a wrapper around the closelog, syslog api.
3081
//!
3182
//! @retval none.
3283
extern "C" void Native_CloseLog()
3384
{
85+
#if defined(__APPLE__) && defined(__MACH__)
86+
// Nothing to do here now, for now.
87+
#else
3488
closelog();
89+
#endif
3590
}

0 commit comments

Comments
 (0)