Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 0d50feb

Browse files
committed
Cleaned up C# style
1 parent 594d672 commit 0d50feb

File tree

9 files changed

+182
-109
lines changed

9 files changed

+182
-109
lines changed

src/csharp/Record/RecordLogger.cs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
using System;
1+
//------------------------------------------------------------------------------
2+
// <copyright file="RecordLogger.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// Licensed under the MIT License.
5+
// </copyright>
6+
//------------------------------------------------------------------------------
7+
using System;
28
using System.Collections.Generic;
39
using System.Diagnostics;
410
using System.Text;
@@ -12,39 +18,15 @@ public static class RecordLogger
1218
{
1319
private static readonly object SyncRoot = new object();
1420
private static readonly NativeMethods.k4a_logging_message_cb_t DebugMessageHandler = OnDebugMessage;
21+
private static readonly RecordLoggerProvider LoggerProvider = new RecordLoggerProvider();
1522
private static bool isInitialized;
1623

17-
private static event Action<LogMessage> LogMessageHandlers;
18-
19-
private class RecordLoggerProvider : ILoggingProvider
20-
{
21-
public event Action<LogMessage> LogMessage
22-
{
23-
add
24-
{
25-
RecordLogger.LogMessage += value;
26-
}
27-
remove
28-
{
29-
RecordLogger.LogMessage -= value;
30-
}
31-
}
32-
}
33-
34-
private readonly static RecordLoggerProvider loggerProvider = new RecordLoggerProvider();
35-
36-
public static ILoggingProvider LogProvider
37-
{
38-
get
39-
{
40-
return RecordLogger.loggerProvider;
41-
}
42-
}
43-
24+
#pragma warning disable CA1003 // Use generic event handler instances
4425
/// <summary>
4526
/// Occurs when the Azure Kinect Sensor Record and Playback SDK delivers a debug message.
4627
/// </summary>
4728
public static event Action<LogMessage> LogMessage
29+
#pragma warning restore CA1003 // Use generic event handler instances
4830
{
4931
add
5032
{
@@ -68,6 +50,19 @@ public static event Action<LogMessage> LogMessage
6850
}
6951
}
7052

53+
private static event Action<LogMessage> LogMessageHandlers;
54+
55+
/// <summary>
56+
/// Gets the interface for reading log messages.
57+
/// </summary>
58+
public static ILoggingProvider LogProvider
59+
{
60+
get
61+
{
62+
return RecordLogger.LoggerProvider;
63+
}
64+
}
65+
7166
/// <summary>
7267
/// Initializes the <see cref="RecordLogger"/> class to begin receiving messages from the Azure Kinect Sensor SDK.
7368
/// </summary>
@@ -138,5 +133,23 @@ private static void CurrentDomain_Exit(object sender, EventArgs e)
138133
Trace.WriteLine("Failed to close the debug message handler");
139134
}
140135
}
136+
137+
private class RecordLoggerProvider : ILoggingProvider
138+
{
139+
public event Action<LogMessage> LogMessage
140+
{
141+
add
142+
{
143+
RecordLogger.LogMessage += value;
144+
}
145+
146+
remove
147+
{
148+
RecordLogger.LogMessage -= value;
149+
}
150+
}
151+
152+
public string ProviderName => "Azure Kinect Recording SDK";
153+
}
141154
}
142155
}

src/csharp/SDK/Allocator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public bool UseManagedAllocator
9292
AzureKinectException.ThrowIfNotSuccess(() => NativeMethods.k4a_set_allocator(this.allocateDelegate, this.freeDelegate));
9393
this.hooked = true;
9494
}
95+
#pragma warning disable CA1031 // Do not catch general exception types
9596
catch (Exception)
97+
#pragma warning restore CA1031 // Do not catch general exception types
9698
{
9799
// Don't fail if we can't set the allocator since this code path is called during the global type
98100
// initialization. A failure to set the allocator is also not fatal, but will only cause a performance

src/csharp/SDK/Device.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ public HardwareVersion Version
166166
}
167167
}
168168
}
169-
170169
/// <summary>
171170
/// Gets the native handle.
172171
/// </summary>
@@ -193,27 +192,6 @@ public IntPtr Handle
193192
}
194193
}
195194

196-
/// <summary>
197-
/// Gets the native handle.
198-
/// </summary>
199-
/// <returns>The native handle that is wrapped by this device.</returns>
200-
/// <remarks>The function is dangerous because there is no guarantee that the
201-
/// handle will not be disposed once it is retrieved. This should only be called
202-
/// by code that can ensure that the Capture object will not be disposed on another
203-
/// thread.</remarks>
204-
internal NativeMethods.k4a_device_t DangerousGetHandle()
205-
{
206-
lock (this)
207-
{
208-
if (this.disposedValue)
209-
{
210-
throw new ObjectDisposedException(nameof(Device));
211-
}
212-
213-
return this.handle;
214-
}
215-
}
216-
217195
/// <summary>
218196
/// Gets the number of currently connected devices.
219197
/// </summary>
@@ -236,6 +214,27 @@ public static Device Open(int index = 0)
236214
return new Device(handle);
237215
}
238216

217+
/// <summary>
218+
/// Gets the native handle.
219+
/// </summary>
220+
/// <returns>The native handle that is wrapped by this device.</returns>
221+
/// <remarks>The function is dangerous because there is no guarantee that the
222+
/// handle will not be disposed once it is retrieved. This should only be called
223+
/// by code that can ensure that the Capture object will not be disposed on another
224+
/// thread.</remarks>
225+
internal NativeMethods.k4a_device_t DangerousGetHandle()
226+
{
227+
lock (this)
228+
{
229+
if (this.disposedValue)
230+
{
231+
throw new ObjectDisposedException(nameof(Device));
232+
}
233+
234+
return this.handle;
235+
}
236+
}
237+
239238
/// <summary>
240239
/// Gets the calibration of the device.
241240
/// </summary>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+

2+
// This file is used by Code Analysis to maintain SuppressMessage
3+
// attributes that are applied to this project.
4+
// Project-level suppressions either have no target or are given
5+
// a specific target and scoped to a namespace, type, member, etc.
6+
7+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "CA1508:Avoid dead conditional code", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.Azure.Kinect.Sensor.Device.GetCapture(System.TimeSpan)~Microsoft.Azure.Kinect.Sensor.Capture")]
8+

src/csharp/SDK/ILoggingProvider.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//------------------------------------------------------------------------------
2+
// <copyright file="ILoggingProvider.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// Licensed under the MIT License.
5+
// </copyright>
6+
//------------------------------------------------------------------------------
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Text;
10+
11+
namespace Microsoft.Azure.Kinect.Sensor
12+
{
13+
/// <summary>
14+
/// An interface for trace logging providers from the native SDK.
15+
/// </summary>
16+
public interface ILoggingProvider
17+
{
18+
#pragma warning disable CA1003 // Use generic event handler instances
19+
/// <summary>
20+
/// Occurs when the native SDK delivers a debug message.
21+
/// </summary>
22+
event Action<LogMessage> LogMessage;
23+
#pragma warning restore CA1003 // Use generic event handler instances
24+
25+
/// <summary>
26+
/// Gets the name of the layer providing the messages.
27+
/// </summary>
28+
string ProviderName { get; }
29+
}
30+
}

src/csharp/SDK/Logger.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,22 @@
99

1010
namespace Microsoft.Azure.Kinect.Sensor
1111
{
12-
13-
public interface ILoggingProvider
14-
{
15-
event Action<LogMessage> LogMessage;
16-
}
17-
1812
/// <summary>
1913
/// The Azure Kinect logging system. Enables access to the debug messages from the Azure Kinect device.
2014
/// </summary>
2115
public static class Logger
2216
{
2317
private static readonly object SyncRoot = new object();
2418
private static readonly NativeMethods.k4a_logging_message_cb_t DebugMessageHandler = OnDebugMessage;
19+
private static readonly LoggerProvider LoggerProviderValue = new LoggerProvider();
2520
private static bool isInitialized;
2621

27-
private static event Action<LogMessage> LogMessageHandlers;
28-
29-
private class LoggerProvider : ILoggingProvider
30-
{
31-
public event Action<LogMessage> LogMessage
32-
{
33-
add
34-
{
35-
Logger.LogMessage += value;
36-
}
37-
remove
38-
{
39-
Logger.LogMessage -= value;
40-
}
41-
}
42-
}
43-
44-
private readonly static LoggerProvider loggerProvider = new LoggerProvider();
45-
46-
public static ILoggingProvider LogProvider
47-
{
48-
get
49-
{
50-
return Logger.loggerProvider;
51-
}
52-
}
53-
22+
#pragma warning disable CA1003 // Use generic event handler instances
5423
/// <summary>
5524
/// Occurs when the Azure Kinect Sensor SDK delivers a debug message.
5625
/// </summary>
5726
public static event Action<LogMessage> LogMessage
27+
#pragma warning restore CA1003 // Use generic event handler instances
5828
{
5929
add
6030
{
@@ -78,6 +48,19 @@ public static event Action<LogMessage> LogMessage
7848
}
7949
}
8050

51+
private static event Action<LogMessage> LogMessageHandlers;
52+
53+
/// <summary>
54+
/// Gets the interface for reading log messages.
55+
/// </summary>
56+
public static ILoggingProvider LogProvider
57+
{
58+
get
59+
{
60+
return Logger.LoggerProviderValue;
61+
}
62+
}
63+
8164
/// <summary>
8265
/// Initializes the <see cref="Logger"/> class to begin receiving messages from the Azure Kinect Sensor SDK.
8366
/// </summary>
@@ -148,5 +131,23 @@ private static void CurrentDomain_Exit(object sender, EventArgs e)
148131
Trace.WriteLine("Failed to close the debug message handler");
149132
}
150133
}
134+
135+
private class LoggerProvider : ILoggingProvider
136+
{
137+
public event Action<LogMessage> LogMessage
138+
{
139+
add
140+
{
141+
Logger.LogMessage += value;
142+
}
143+
144+
remove
145+
{
146+
Logger.LogMessage -= value;
147+
}
148+
}
149+
150+
public string ProviderName => "Azure Kinect SDK";
151+
}
151152
}
152153
}

src/csharp/SDK/Native/LoggingTracer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ namespace Microsoft.Azure.Kinect.Sensor
1717
public class LoggingTracer : IDisposable
1818
{
1919
private readonly int threadId;
20+
private readonly LogLevel minLevel;
2021

2122
private bool disposed;
2223
private List<LogMessage> messages;
2324

2425
private ILoggingProvider[] loggingProviders;
2526

26-
private LogLevel minLevel;
27-
2827
/// <summary>
2928
/// Initializes a new instance of the <see cref="LoggingTracer"/> class.
3029
/// </summary>
@@ -33,6 +32,11 @@ public LoggingTracer()
3332
{
3433
}
3534

35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="LoggingTracer"/> class.
37+
/// </summary>
38+
/// <param name="minLevel">Minimum level of messages to capture.</param>
39+
/// <param name="loggingProvider">Set of logging providers to capture from.</param>
3640
public LoggingTracer(LogLevel minLevel, params ILoggingProvider[] loggingProvider)
3741
{
3842
this.messages = new List<LogMessage>();

src/csharp/SDK/Native/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public static extern k4a_result_t k4a_set_debug_message_handler(
374374

375375
[DllImport("k4arecord", CallingConvention = k4aCallingConvention, CharSet = CharSet.Ansi)]
376376
[NativeReference]
377-
public static extern k4a_result_t k4a_record_create(string path, k4a_device_t device, k4a_device_configuration_t deviceConfiguration, out k4a_record_t handle);
377+
public static extern k4a_result_t k4a_record_create([MarshalAs(UnmanagedType.LPStr)] string path, k4a_device_t device, k4a_device_configuration_t deviceConfiguration, out k4a_record_t handle);
378378

379379
[DllImport("k4arecord", CallingConvention = k4aCallingConvention)]
380380
[NativeReference]

0 commit comments

Comments
 (0)