Skip to content

Commit 534728b

Browse files
committed
Add target binding tracing.
For platforms which do not support TraceSource, the tracing code is nop.
1 parent 9e7758c commit 534728b

File tree

8 files changed

+101
-8
lines changed

8 files changed

+101
-8
lines changed

Sync.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Preserve Path="Serialization\NonGenericMessagePackSerializer.cs" />
4545
<Preserve Path="Serialization\TypedMessagePackSerializerWrapper`1.cs" />
4646
<Preserve Path="Serialization\EnumTypedMessagePackSerializerWrapper`1.cs" />
47+
<Preserve Path="Serialization\Tracer.cs" />
4748
<Preserve Path="CorLibOnlyHelper*.tt" />
4849
<Preserve Path="CorLibOnlyHelper*.cs" />
4950
<Preserve Path="MPContract.cs" />

src/MsgPack.Unity.Full/MsgPack.Unity.Full.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'CodeAnalysis|AnyCPU'">
3939
<OutputPath>bin\CodeAnalysis\</OutputPath>
4040
<DefineConstants>TRACE;UNITY_IPHONE;MSGPACK_UNITY_FULL;AOT;CODE_ANALYSIS</DefineConstants>
41-
<DocumentationFile></DocumentationFile>
41+
<DocumentationFile>
42+
</DocumentationFile>
4243
<Optimize>true</Optimize>
4344
<NoWarn>3001,3002</NoWarn>
4445
<DebugType>pdbonly</DebugType>
@@ -709,6 +710,9 @@
709710
<Compile Include="..\MsgPack\Serialization\SerializingMember.cs">
710711
<Link>Serialization\SerializingMember.cs</Link>
711712
</Compile>
713+
<Compile Include="..\MsgPack\Serialization\Tracer.cs">
714+
<Link>Serialization\Tracer.cs</Link>
715+
</Compile>
712716
<Compile Include="..\MsgPack\Serialization\TypeKeyRepository.cs">
713717
<Link>Serialization\TypeKeyRepository.cs</Link>
714718
</Compile>

src/MsgPack.Unity/MsgPack.Unity.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@
719719
<Compile Include="..\MsgPack\Serialization\SerializingMember.cs">
720720
<Link>Serialization\SerializingMember.cs</Link>
721721
</Compile>
722+
<Compile Include="..\MsgPack\Serialization\Tracer.cs">
723+
<Link>Serialization\Tracer.cs</Link>
724+
</Compile>
722725
<Compile Include="..\MsgPack\Serialization\TypeKeyRepository.cs">
723726
<Link>Serialization\TypeKeyRepository.cs</Link>
724727
</Compile>

src/MsgPack/Serialization/SerializationTarget.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
using System;
3333
using System.Collections.Generic;
34+
using System.Diagnostics;
3435
#if CORE_CLR || UNITY || NETSTANDARD1_1
3536
using Contract = MsgPack.MPContract;
3637
#else
@@ -70,6 +71,7 @@ internal class SerializationTarget
7071

7172
private SerializationTarget( IList<SerializingMember> members, ConstructorInfo constructor, string[] correspondingMemberNames, bool canDeserialize )
7273
{
74+
Trace( "SerializationTarget::ctor(canDeserialize: {0})", canDeserialize );
7375
this.Members = members;
7476
this.DeserializationConstructor = constructor;
7577
this.IsConstructorDeserialization = constructor != null && constructor.GetParameters().Any();
@@ -286,21 +288,26 @@ private static bool DetermineCanDeserialize( ConstructorKind kind, Serialization
286288
{
287289
if ( HasUnpackableInterface( targetType, context ) )
288290
{
291+
Trace( "SerializationTarget::DetermineCanDeserialize({0}, {1}) -> true: HasUnpackableInterface", targetType, kind );
289292
return true;
290293
}
291294

292295
switch ( kind )
293296
{
294297
case ConstructorKind.Marked:
295298
{
299+
Trace( "SerializationTarget::DetermineCanDeserialize({0}, {1}) -> true: Marked", targetType, kind );
296300
return true;
297301
}
298302
case ConstructorKind.Parameterful:
299303
{
300-
return HasAnyCorrespondingMembers( correspondingMemberNames );
304+
var result = HasAnyCorrespondingMembers( correspondingMemberNames );
305+
Trace( "SerializationTarget::DetermineCanDeserialize({0}, {1}) -> {2}: HasAnyCorrespondingMembers", targetType, kind, result );
306+
return result;
301307
}
302308
case ConstructorKind.Default:
303309
{
310+
Trace( "SerializationTarget::DetermineCanDeserialize({0}, {1}) -> {2}: Default", targetType, kind, allowDefault );
304311
return allowDefault;
305312
}
306313
default:
@@ -317,7 +324,7 @@ private static MemberInfo[] GetDistinctMembers( Type type )
317324
var returningMemberNamesSet = new HashSet<string>();
318325
while ( type != typeof( object ) && type != null )
319326
{
320-
var members =
327+
var members =
321328
#if !NETSTANDARD1_1 && !NETSTANDARD1_3
322329
type.FindMembers(
323330
MemberTypes.Field | MemberTypes.Property,
@@ -507,6 +514,9 @@ private static ConstructorInfo FindDeserializationConstructor( SerializationCont
507514
// A constructor which has most parameters will be used.
508515
var mostRichConstructors =
509516
constructors.GroupBy( ctor => ctor.GetParameters().Length ).OrderByDescending( g => g.Key ).First().ToArray();
517+
#if DEBUG
518+
Trace( "SerializationTarget::FindDeserializationConstructor.MostRich({0}) -> {1}", targetType, String.Join( ";", mostRichConstructors.Select( x => x.ToString() ).ToArray() ) );
519+
#endif // DEBUG
510520
switch ( mostRichConstructors.Length )
511521
{
512522
case 1:
@@ -830,6 +840,14 @@ public override int GetHashCode( KeyValuePair<string, Type> obj )
830840
}
831841
}
832842

843+
[Conditional( "DEBUG" )]
844+
private static void Trace( string format, params object[] args )
845+
{
846+
#if !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
847+
Tracer.Binding.TraceEvent( Tracer.EventType.Trace, Tracer.EventId.Trace, format, args );
848+
#endif // !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
849+
}
850+
833851
private enum ConstructorKind
834852
{
835853
None = 0,

src/MsgPack/Serialization/Tracer.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#region -- License Terms --
1+
#region -- License Terms --
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2016 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2017 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -18,17 +18,22 @@
1818
//
1919
#endregion -- License Terms --
2020

21+
#if UNITY_5 || UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WII || UNITY_IPHONE || UNITY_ANDROID || UNITY_PS3 || UNITY_XBOX360 || UNITY_FLASH || UNITY_BKACKBERRY || UNITY_WINRT
22+
#define UNITY
23+
#endif
24+
2125
using System;
2226
using System.Diagnostics;
23-
#if NETSTANDARD1_1 || NETSTANDARD1_3
27+
#if NETSTANDARD1_1 || NETSTANDARD1_3 || ( UNITY && !MSGPACK_UNITY_FULL )
2428
using System.Globalization;
25-
#endif // NETSTANDARD1_1 || NETSTANDARD1_3
29+
#endif // NETSTANDARD1_1 || NETSTANDARD1_3 || ( UNITY && !MSGPACK_UNITY_FULL )
2630

2731
namespace MsgPack.Serialization
2832
{
2933
internal static class Tracer
3034
{
3135
public static readonly TraceSource Emit = new TraceSource( "MsgPack.Serialization.Emit" );
36+
public static readonly TraceSource Binding = new TraceSource( "MsgPack.Serialization.Binding" );
3237
public static readonly TraceSource Tracing = new TraceSource( "MsgPack.Serialization.Tracing" );
3338

3439
public static class EventId
@@ -56,7 +61,7 @@ public static class EventType
5661
}
5762
}
5863

59-
#if NETSTANDARD1_1 || NETSTANDARD1_3
64+
#if NETSTANDARD1_1 || NETSTANDARD1_3 || ( UNITY && !MSGPACK_UNITY_FULL )
6065
internal enum TraceEventType
6166
{
6267
Critical = 1,
@@ -78,13 +83,17 @@ public TraceSource( string name )
7883
[Conditional( "TRACE" )]
7984
public void TraceEvent( TraceEventType eventType, int id, string format, params object[] args )
8085
{
86+
#if !UNITY
8187
Debug.WriteLine( String.Format( CultureInfo.InvariantCulture, "{0} {1}: {2} : {3}", this._name, eventType, id, String.Format( CultureInfo.InvariantCulture, format, args ) ) );
88+
#endif // !UNITY
8289
}
8390

8491
[Conditional( "TRACE" )]
8592
public void TraceData( TraceEventType eventType, int id, object data )
8693
{
94+
#if !UNITY
8795
Debug.WriteLine( String.Format( CultureInfo.InvariantCulture, "{0} {1}: {2} : {3}", this._name, eventType, id, data ) );
96+
#endif // !UNITY
8897
}
8998
}
9099
#endif // NETSTANDARD1_1 || NETSTANDARD1_3

test/MsgPack.UnitTest.Unity.Il2cpp.Full.Desktop/MsgPack.UnitTest.Unity.Il2cpp.Full.Desktop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@
755755
<Link>UnpackingTest.Scalar.cs</Link>
756756
</Compile>
757757
<Compile Include="Properties\AssemblyInfo.cs" />
758+
<Compile Include="UnityTestHelper.cs" />
758759
</ItemGroup>
759760
<ItemGroup>
760761
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#region -- License Terms --
2+
//
3+
// MessagePack for CLI
4+
//
5+
// Copyright (C) 2010-2017 FUJIWARA, Yusuke
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
#endregion -- License Terms --
20+
21+
using System;
22+
using System.Diagnostics;
23+
24+
using MsgPack.Serialization;
25+
26+
namespace MsgPack
27+
{
28+
public static class UnityTestHelper
29+
{
30+
public static void AddBindingTraceLogger( Action<string> logger )
31+
{
32+
Tracer.Binding.Switch.Level = SourceLevels.All;
33+
Tracer.Binding.Listeners.Add( new DelegateTraceListener( logger ) );
34+
}
35+
36+
private sealed class DelegateTraceListener : TraceListener
37+
{
38+
private readonly Action<string> _logger;
39+
40+
public DelegateTraceListener( Action<string> logger )
41+
{
42+
this._logger = logger;
43+
}
44+
45+
public override void Write( string message )
46+
{
47+
this._logger( message );
48+
}
49+
50+
public override void WriteLine( string message )
51+
{
52+
this._logger( message );
53+
}
54+
}
55+
}
56+
}

test/MsgPack.UnitTest.Unity.Il2cpp.Full/Assets/UnitTests/TestDrivers/UnitTestDriver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private static void InitializeTestEngine()
9494
TestExecutionContext context = new TestExecutionContext();
9595
context.WorkDirectory = Environment.CurrentDirectory;
9696
CallContext.SetData( "NUnit.Framework.TestContext", context );
97+
//UnityTestHelper.AddBindingTraceLogger( m => UnityEngine.Debug.Log( m ) );
9798
}
9899

99100
/// <summary>

0 commit comments

Comments
 (0)