如何P/调用os_log? [英] How to P/Invoke os_log?
问题描述
在.NET Core控制台应用程序中从C#调用MacOS的os_log的语法应该是什么??
基于
https://developer.apple.com/documentation/os/os_log
和
如何在Xamarin中使用iOS OSLog?
和
https://opensource. apple.com/source/xnu/xnu-4903.221.2/libkern/os/log.h.auto.html
我期待这样的事情:
Based on
https://developer.apple.com/documentation/os/os_log
and
How to use iOS OSLog with Xamarin?
and
https://opensource.apple.com/source/xnu/xnu-4903.221.2/libkern/os/log.h.auto.html
I was expecting something like this:
using System.Runtime.InteropServices;
namespace Foo
{
class Program
{
[DllImport("__Internal", EntryPoint = "os_log_create")]
private static extern IntPtr os_log_create(string subsystem, string category);
[DllImport("__Internal", EntryPoint = "os_log")]
private static extern void os_log(IntPtr log, string format, string message);
static void Main(string[] args)
{
IntPtr log = os_log_create("some.bundle.id", "SomeCategory");
os_log(log, "%s", "Test!");
}
}
}
但是,当我尝试在Mac上运行此程序时,会得到一个System.DllNotFoundException
,其内容为Unable to load shared library '__Internal' or one of its dependencies...
.
However, when I try to run this on my Mac I get a System.DllNotFoundException
that says Unable to load shared library '__Internal' or one of its dependencies...
.
任何有关此问题或C#和MacOS之间的P/Invoke的帮助都将非常有帮助,谢谢!
Any help with this issue or P/Invoke between C# and MacOS would be very helpful, thanks!
推荐答案
宏os_log
与os_log_create函数相比,os_log是一个宏,如注释中所述.
In contrast to the os_log_create function, os_log is a macro, as already mentioned in the comments.
因此,如果您要用C编写:
So if you would write in C:
os_log(log, "%{public}s", "Test!");
它最终将调用一个名为_os_log_impl的函数,但该函数的第一个参数将是指针__dso_handle,我们无法从托管端访问该指针.
It would finally call a function named _os_log_impl, but the first parameter of that would be a pointer __dso_handle to which we don't have access from the managed side.
可能的解决方案
但是,如果没有Apple的新日志记录系统,您不必做任何事情.一种可能性是创建一个动态库,该库提供可从托管C#代码轻松调用的已定义API.
But you don't have to do without the new logging system from Apple. One possibility is to create a dynamic library that provides a defined API that can easily be called from the managed C# code.
如何在Xcode中创建动态库
在Xcode中创建动态库很容易:
It is easy to create a dynamic library in Xcode:
-
选择XCode< File/New Project>
choose in XCode <File/New Project>
在 macOS 部分
使用类型动态
最小示例
我们自己的 Logging 库的最小.c示例如下所示:
A minimal .c example for our own Logging library might look like this:
#include <os/log.h>
extern void Log(os_log_t log, char *message) {
os_log(log, "%{public}s", message);
}
从.Net拨打电话
我拿走了你的资料,只是对其做了些微修改:
I took your source and only slightly modified it:
using System;
using System.Runtime.InteropServices;
namespace Foo
{
class Program
{
[DllImport("System", EntryPoint = "os_log_create")]
private static extern IntPtr os_log_create(string subsystem, string category);
[DllImport("Logging", EntryPoint = "Log")]
private static extern void Log(IntPtr log, string msg);
static void Main(string[] args)
{
IntPtr log = os_log_create("some.bundle.id", "SomeCategory");
Log(log, "Test!");
}
}
}
使用Xcode创建的动态库的名称为 Logging .我们在C中创建的日志记录功能在此处名为 Log .
The dynamic library created with Xcode has the name Logging. Our in C created logging function is named Log here.
当然,您可以根据需要设计API,这应该是一个尽可能接近问题的最小示例.
Of course you can design the API as comfortable as you want, this should be a minimal example that is as close to the question as possible.
控制台实用程序中的输出
控制台实用程序中的输出(如果您过滤 some.bundle.id )将如下所示:
The output in the Console utility (if you filter for some.bundle.id) would look like this:
这篇关于如何P/调用os_log?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!