Skip to content
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.

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

[corefoundation] Fix TypeInitializationException in OSLog #7962

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/CoreFoundation/OSLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ namespace CoreFoundation {
[Introduced (PlatformName.MacCatalyst, 13, 0)]
public sealed class OSLog : NativeObject {

// initialized only once (see tests/cecil-tests/)
public static OSLog Default { get; } = new OSLog (IntPtr.Zero, false);
static OSLog _default;

public static OSLog Default {
get {
if (_default == null) {
var h = Dlfcn.dlsym (Libraries.System.Handle, "_os_log_default");
if (h == IntPtr.Zero)
throw new NotSupportedException ("Feature not available on this OS version");
_default = new OSLog (h, false);
}
return _default;
}
}

protected override void Retain ()
{
Expand Down
40 changes: 40 additions & 0 deletions tests/monotouch-test/CoreFoundation/OSLogTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.IO;
using System.Runtime.InteropServices;

using CoreFoundation;
using Foundation;
using ObjCRuntime;

using NUnit.Framework;

namespace MonoTouchFixtures.CoreFoundation {

[TestFixture]
[Preserve (AllMembers = true)]
public class OSLogTest {

[TestFixtureSetUp]
public void SetUp ()
{
TestRuntime.AssertXcodeVersion (8,0);
}

[Test]
public void Default ()
{
OSLog.Default.Log (OSLogLevel.Default, "monotouch-test / Default / Default");
Copy link
Member

Choose a reason for hiding this comment

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

You need a version check to avoid this test on older OSes.

// this will show in the application output (e.g. inside VSfM)
}

[Test]
public void Custom ()
{
using (var log = new OSLog ("subsystem", "category")) {
log.Log (OSLogLevel.Error, "monotouch-test / custom / Debug");
// this will show in the application output (e.g. inside VSfM)
// and also inside Console.app under the simulator/device
Copy link
Member

Choose a reason for hiding this comment

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

No Asserts?

Copy link
Member

@dalexsoto dalexsoto Feb 24, 2020

Choose a reason for hiding this comment

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

As long as it does to crash or throw here :P I think this is fine #YOLO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mandel-macaque not quite sure on what to assert (if it does not crash, like it would without the fix). Reading console.app is a quite troublesome from simulator and would not work on devices. Got any ideas ?

Copy link
Member

Choose a reason for hiding this comment

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

I think there is a way with http://www.cplusplus.com/reference/cstdio/freopen/ freopen stderr to a file, read wha was logged in the file. Maybe too much work :/

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with "not crashing is good enough". I could see redirects being brittle maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I won't stop you PR'ing that... but, for myself, messing with stderr, which is already used used by the test harness, seems more a problem than a solution :)

Copy link
Member

Choose a reason for hiding this comment

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

}
}
}
}