@@ -1,34 +1,34 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.Linq; |
|
3 | using System.Linq; | |
4 | using System.Text; |
|
4 | using System.Text; | |
5 |
|
5 | |||
6 | namespace Implab.Diagnostics { |
|
6 | namespace Implab.Diagnostics { | |
7 | public class ConsoleTraceListener: TextListenerBase { |
|
7 | public class ConsoleTraceListener: TextListenerBase { | |
8 |
|
8 | |||
9 | static readonly object _consoleLock = new object(); |
|
9 | static readonly object _consoleLock = new object(); | |
10 |
|
10 | |||
11 | public ConsoleTraceListener() |
|
11 | public ConsoleTraceListener() | |
12 | : base(true) { |
|
12 | : base(true) { | |
13 |
|
13 | |||
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | public ConsoleTraceListener(bool global) |
|
16 | public ConsoleTraceListener(bool global) | |
17 | : base(global) { |
|
17 | : base(global) { | |
18 |
|
18 | |||
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { |
|
21 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
22 | var msg = new StringBuilder(); |
|
22 | var msg = new StringBuilder(); | |
23 |
|
23 | |||
24 | for (int i = 0; i < text.indent; i++) |
|
24 | for (int i = 0; i < text.indent; i++) | |
25 | msg.Append(" "); |
|
25 | msg.Append(" "); | |
26 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); |
|
26 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); | |
27 |
|
27 | |||
28 | lock (_consoleLock) { |
|
28 | lock (_consoleLock) { | |
29 | Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1); |
|
29 | //Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1); | |
30 | Console.WriteLine(msg); |
|
30 | Console.WriteLine(msg); | |
31 | } |
|
31 | } | |
32 | } |
|
32 | } | |
33 | } |
|
33 | } | |
34 | } |
|
34 | } |
@@ -1,26 +1,43 | |||||
1 | namespace Implab.Diagnostics { |
|
1 | namespace Implab.Diagnostics { | |
2 | public static class Extensions { |
|
2 | public static class Extensions { | |
3 | public static IPromise<T> EndLogicalOperation<T>(this IPromise<T> promise) { |
|
3 | public static IPromise<T> EndLogicalOperation<T>(this IPromise<T> promise) { | |
4 | Safe.ArgumentNotNull(promise, "promise"); |
|
4 | Safe.ArgumentNotNull(promise, "promise"); | |
5 | var op = TraceContext.Instance.DetachLogicalOperation(); |
|
5 | var op = TraceContext.Instance.DetachLogicalOperation(); | |
6 |
|
6 | |||
7 |
return promise. |
|
7 | return promise.Then<T>( | |
|
8 | x => { | |||
8 | TraceContext.Instance.EnterLogicalOperation(op,true); |
|
9 | TraceContext.Instance.EnterLogicalOperation(op,true); | |
|
10 | TraceLog.TraceInformation("promise = {0}", x); | |||
9 | TraceLog.EndLogicalOperation(); |
|
11 | TraceLog.EndLogicalOperation(); | |
10 | TraceContext.Instance.Leave(); |
|
12 | TraceContext.Instance.Leave(); | |
11 | }); |
|
13 | return x; | |
|
14 | }, | |||
|
15 | err =>{ | |||
|
16 | TraceContext.Instance.EnterLogicalOperation(op,true); | |||
|
17 | TraceLog.TraceError("promise died {0}", err); | |||
|
18 | TraceLog.EndLogicalOperation(); | |||
|
19 | TraceContext.Instance.Leave(); | |||
|
20 | throw new TransientPromiseException(err); | |||
|
21 | }, | |||
|
22 | () => { | |||
|
23 | TraceContext.Instance.EnterLogicalOperation(op,true); | |||
|
24 | TraceLog.TraceInformation("promise cancelled"); | |||
|
25 | TraceLog.EndLogicalOperation(); | |||
|
26 | TraceContext.Instance.Leave(); | |||
|
27 | } | |||
|
28 | ); | |||
12 | } |
|
29 | } | |
13 |
|
30 | |||
14 | public static IPromise EndLogicalOperation(this IPromise promise) { |
|
31 | public static IPromise EndLogicalOperation(this IPromise promise) { | |
15 | Safe.ArgumentNotNull(promise, "promise"); |
|
32 | Safe.ArgumentNotNull(promise, "promise"); | |
16 | var op = TraceContext.Instance.DetachLogicalOperation(); |
|
33 | var op = TraceContext.Instance.DetachLogicalOperation(); | |
17 |
|
34 | |||
18 | return promise.Anyway(() => { |
|
35 | return promise.Anyway(() => { | |
19 | TraceContext.Instance.EnterLogicalOperation(op,true); |
|
36 | TraceContext.Instance.EnterLogicalOperation(op,true); | |
20 | TraceLog.EndLogicalOperation(); |
|
37 | TraceLog.EndLogicalOperation(); | |
21 | TraceContext.Instance.Leave(); |
|
38 | TraceContext.Instance.Leave(); | |
22 | }); |
|
39 | }); | |
23 | } |
|
40 | } | |
24 | } |
|
41 | } | |
25 | } |
|
42 | } | |
26 |
|
43 |
@@ -1,83 +1,90 | |||||
1 | using System.Threading; |
|
1 | using System.Threading; | |
2 | using System; |
|
2 | using System; | |
|
3 | using Implab.Diagnostics; | |||
|
4 | ||||
|
5 | ||||
3 | #if NET_4_5 |
|
6 | #if NET_4_5 | |
4 | using System.Threading.Tasks; |
|
7 | using System.Threading.Tasks; | |
5 | #endif |
|
8 | #endif | |
6 |
|
9 | |||
7 | namespace Implab { |
|
10 | namespace Implab { | |
8 | public static class PromiseExtensions { |
|
11 | public static class PromiseExtensions { | |
9 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { |
|
12 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { | |
10 | Safe.ArgumentNotNull(that, "that"); |
|
13 | Safe.ArgumentNotNull(that, "that"); | |
11 | var context = SynchronizationContext.Current; |
|
14 | var context = SynchronizationContext.Current; | |
12 | if (context == null) |
|
15 | if (context == null) | |
13 | return that; |
|
16 | return that; | |
14 |
|
17 | |||
15 | var p = new SyncContextPromise<T>(context, that); |
|
18 | var p = new SyncContextPromise<T>(context, that); | |
16 |
|
19 | |||
17 | that.On( |
|
20 | that.On( | |
18 | p.Resolve, |
|
21 | p.Resolve, | |
19 | p.Reject, |
|
22 | p.Reject, | |
20 | p.Cancel |
|
23 | p.Cancel | |
21 | ); |
|
24 | ); | |
22 | return p; |
|
25 | return p; | |
23 | } |
|
26 | } | |
24 |
|
27 | |||
25 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { |
|
28 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | |
26 | Safe.ArgumentNotNull(that, "that"); |
|
29 | Safe.ArgumentNotNull(that, "that"); | |
27 | Safe.ArgumentNotNull(context, "context"); |
|
30 | Safe.ArgumentNotNull(context, "context"); | |
28 |
|
31 | |||
29 | var p = new SyncContextPromise<T>(context, that); |
|
32 | var p = new SyncContextPromise<T>(context, that); | |
30 |
|
33 | |||
31 | that.On( |
|
34 | that.On( | |
32 | p.Resolve, |
|
35 | p.Resolve, | |
33 | p.Reject, |
|
36 | p.Reject, | |
34 | p.Cancel |
|
37 | p.Cancel | |
35 | ); |
|
38 | ); | |
36 | return p; |
|
39 | return p; | |
37 | } |
|
40 | } | |
38 |
|
41 | |||
39 | /// <summary> |
|
42 | /// <summary> | |
40 | /// Ensures the dispatched. |
|
43 | /// Ensures the dispatched. | |
41 | /// </summary> |
|
44 | /// </summary> | |
42 | /// <returns>The dispatched.</returns> |
|
45 | /// <returns>The dispatched.</returns> | |
43 | /// <param name="that">That.</param> |
|
46 | /// <param name="that">That.</param> | |
44 | /// <param name="head">Head.</param> |
|
47 | /// <param name="head">Head.</param> | |
45 | /// <param name="cleanup">Cleanup.</param> |
|
48 | /// <param name="cleanup">Cleanup.</param> | |
46 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> |
|
49 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | |
47 | /// <typeparam name="T">The 2nd type parameter.</typeparam> |
|
50 | /// <typeparam name="T">The 2nd type parameter.</typeparam> | |
48 | public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ |
|
51 | public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ | |
49 | Safe.ArgumentNotNull(that, "that"); |
|
52 | Safe.ArgumentNotNull(that, "that"); | |
50 | Safe.ArgumentNotNull(head, "head"); |
|
53 | Safe.ArgumentNotNull(head, "head"); | |
51 |
|
54 | |||
52 | that.On(null,null,() => head.On(cleanup)); |
|
55 | that.On(null,null,() => head.On(cleanup)); | |
53 |
|
56 | |||
54 | return that; |
|
57 | return that; | |
55 | } |
|
58 | } | |
56 |
|
59 | |||
57 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { |
|
60 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { | |
58 | Safe.ArgumentNotNull(that, "that"); |
|
61 | Safe.ArgumentNotNull(that, "that"); | |
59 | Safe.ArgumentNotNull(callback, "callback"); |
|
62 | Safe.ArgumentNotNull(callback, "callback"); | |
|
63 | var op = TraceContext.Instance.CurrentOperation; | |||
60 | return ar => { |
|
64 | return ar => { | |
|
65 | TraceContext.Instance.EnterLogicalOperation(op,false); | |||
61 | try { |
|
66 | try { | |
62 | that.Resolve(callback(ar)); |
|
67 | that.Resolve(callback(ar)); | |
63 | } catch (Exception err) { |
|
68 | } catch (Exception err) { | |
64 | that.Reject(err); |
|
69 | that.Reject(err); | |
|
70 | } finally { | |||
|
71 | TraceContext.Instance.Leave(); | |||
65 | } |
|
72 | } | |
66 | }; |
|
73 | }; | |
67 | } |
|
74 | } | |
68 |
|
75 | |||
69 | #if NET_4_5 |
|
76 | #if NET_4_5 | |
70 |
|
77 | |||
71 | public static Task<T> GetTask<T>(this IPromise<T> that) { |
|
78 | public static Task<T> GetTask<T>(this IPromise<T> that) { | |
72 | Safe.ArgumentNotNull(that, "that"); |
|
79 | Safe.ArgumentNotNull(that, "that"); | |
73 | var tcs = new TaskCompletionSource<T>(); |
|
80 | var tcs = new TaskCompletionSource<T>(); | |
74 |
|
81 | |||
75 | that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); |
|
82 | that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); | |
76 |
|
83 | |||
77 | return tcs.Task; |
|
84 | return tcs.Task; | |
78 | } |
|
85 | } | |
79 |
|
86 | |||
80 | #endif |
|
87 | #endif | |
81 | } |
|
88 | } | |
82 | } |
|
89 | } | |
83 |
|
90 |
General Comments 0
You need to be logged in to leave comments.
Login now