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