GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use os_log APIs to log PowerShell events on OSX by dantraMSFT · Pull Request #5310 · PowerShell/PowerShell

Use os_log APIs to log PowerShell events on OSX #5310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Dongbo Wang (daxian-dbw) merged 1 commit into PowerShell:master from dantraMSFT:libpsl-native/syslog
Nov 3, 2017

Uh oh!

There was an error while loading. Please reload this page.

Merged

Use os_log APIs to log PowerShell events on OSX #5310

Changes from all commits
Commits
Show all changes
1 commit
Select commit
411c521
Use native os_log APIs on OSX
Nov 2, 2017
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Use native os_log APIs on OSX
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

dantraMSFT @dantraMSFT
dantraMSFT authored and dantraMSFT committed Nov 2, 2017
commit 411c5211d950682c94e223339ef661a110415788

There are no files selected for viewing

57 changes: 56 additions & 1 deletion 57 src/libpsl-native/src/nativesyslog.cpp
100644 → 100755
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,65 @@
//! @brief Provides wrappers around the syslog apis to support exporting
//! for PInvoke calls by powershell.
//! These functions are intended only for PowerShell internal use.
//! To view log output in real time
//! On Linux
//! tail -f /var/log/syslog | grep powershell
//! On OSX
//! sudo log stream
//! NOTE: replace powershell with the LogIdentity value when overriding in configuration
#include <syslog.h>
#include <nativesyslog.h>

#if defined(__APPLE__) && defined(__MACH__)

#include <os/log.h>
#include <stdio.h>
static os_log_t _log = NULL;

// This format string ensures the message string for the log statements
// are visible. Using just %s marks them as private and the do not show
// up with log stream.
#define MESSAGE_FORMAT "%{public}s"

// The submodule name to pass to os_log_create.
// The passed in ident value will be used for the category.
// see man os_log_create
#define SUBMODULE_NAME "com.microsoft.powershell"

#endif

//! @brief Native_SysLog is a wrapper around the syslog api.
//! It explicitly passes the message as a parameter to a %s format
//! string since the message may have arbitray characters that can
//! string since the message may have arbitrary characters that can
//! be misinterpreted as format specifiers.
//!
//! @retval none.
extern "C" void Native_SysLog(int32_t priority, const char* message)
{
#if defined(__APPLE__) && defined(__MACH__)
switch (priority)
{
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
os_log_fault(_log, MESSAGE_FORMAT, message);
break;

case LOG_ERR:
os_log_error(_log, MESSAGE_FORMAT, message);
break;

case LOG_DEBUG:
os_log_debug(_log, MESSAGE_FORMAT, message);
Comment thread

@daxian-dbw Dongbo Wang (daxian-dbw) Nov 2, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For those different log types, do we need to check whether the type is enabled before calling the log method? What would happen in case the target type is not enabled?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dantraMSFT Dan Travison (dantraMSFT) Nov 2, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check occurs in SysLogProvider.cs. The filters are passed into its constructor by PSSysLogProvider. Both Linux and Mac use the PowerShellProperties.json to define the filters (see JsonConfigFileAccessor in PropertyAccessor.cs)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daxian-dbw Dongbo Wang (daxian-dbw) Nov 2, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the API os_log_type_enabled exposed in this .cpp file, how does SysLogProvider.cs check if a log type is enabled? Does it enable all log types when creating the log object when calling os_log_create?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dantraMSFT Dan Travison (dantraMSFT) Nov 3, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed. Operational events are logged at the default level; which is enabled by default so we'll see the same output we would see in the event log.

https://developer.apple.com/documentation/os/os_log

I'd prefer to filter without interop calls at the risk that someone will change the configuration for powershell at the native layer and we make unneeded interop calls. We can address this later, if it becomes an issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daxian-dbw Dongbo Wang (daxian-dbw) Nov 3, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. #Closed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
break;

default:
os_log(_log, MESSAGE_FORMAT, message);
break;
}
#else
syslog(priority, "%s", message);
#endif
}

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

//! @brief Native_OpenLog is a wrapper around the closelog, syslog api.
//!
//! @retval none.
extern "C" void Native_CloseLog()
{
#if defined(__APPLE__) && defined(__MACH__)
// Nothing to do here now, for now.
#else
closelog();
#endif
}
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.