-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MvxTrace.cs
169 lines (134 loc) · 4.94 KB
/
MvxTrace.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// MvxTrace.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com
using System;
using Cirrious.CrossCore.Core;
using Cirrious.CrossCore;
using Cirrious.CrossCore.Exceptions;
namespace Cirrious.CrossCore.Platform
{
public class MvxTrace
: MvxSingleton<IMvxTrace>
, IMvxTrace
{
#region public static Interface
public static readonly DateTime WhenTraceStartedUtc = DateTime.UtcNow;
public static string DefaultTag { get; set; }
public static void Initialize()
{
if (Instance != null)
throw new MvxException("MvxTrace already initialized");
DefaultTag = "mvx";
// selfRegisteringSingleton
new MvxTrace();
}
public static void TaggedTrace(MvxTraceLevel level, string tag, string message, params object[] args)
{
if (Instance != null)
Instance.Trace(level, tag, PrependWithTime(message), args);
}
public static void TaggedTrace(MvxTraceLevel level, string tag, Func<string> message)
{
if (Instance != null)
Instance.Trace(level, tag, PrependWithTime(message));
}
public static void Trace(MvxTraceLevel level, string message, params object[] args)
{
if (Instance != null)
Instance.Trace(level, DefaultTag, PrependWithTime(message), args);
}
public static void Trace(MvxTraceLevel level, Func<string> message)
{
if (Instance != null)
Instance.Trace(level, DefaultTag, PrependWithTime(message));
}
public static void TaggedTrace(string tag, string message, params object[] args)
{
TaggedTrace(MvxTraceLevel.Diagnostic, tag, message, args);
}
public static void TaggedWarning(string tag, string message, params object[] args)
{
TaggedTrace(MvxTraceLevel.Warning, tag, message, args);
}
public static void TaggedError(string tag, string message, params object[] args)
{
TaggedTrace(MvxTraceLevel.Error, tag, message, args);
}
public static void TaggedTrace(string tag, Func<string> message)
{
TaggedTrace(MvxTraceLevel.Diagnostic, tag, message);
}
public static void TaggedWarning(string tag, Func<string> message)
{
TaggedTrace(MvxTraceLevel.Warning, tag, message);
}
public static void TaggedError(string tag, Func<string> message)
{
TaggedTrace(MvxTraceLevel.Error, tag, message);
}
public static void Trace(string message, params object[] args)
{
Trace(MvxTraceLevel.Diagnostic, message, args);
}
public static void Warning(string message, params object[] args)
{
Trace(MvxTraceLevel.Warning, message, args);
}
public static void Error(string message, params object[] args)
{
Trace(MvxTraceLevel.Error, message, args);
}
public static void Trace(Func<string> message)
{
Trace(MvxTraceLevel.Diagnostic, message);
}
public static void Warning(Func<string> message)
{
Trace(MvxTraceLevel.Warning, message);
}
public static void Error(Func<string> message)
{
Trace(MvxTraceLevel.Error, message);
}
#endregion Static Interface
private readonly IMvxTrace _realTrace;
public MvxTrace()
{
_realTrace = Mvx.Resolve<IMvxTrace>();
if (_realTrace == null)
throw new MvxException("No platform trace service available");
}
#region IMvxTrace Members
void IMvxTrace.Trace(MvxTraceLevel level, string tag, Func<string> message)
{
_realTrace.Trace(level, tag, message);
}
void IMvxTrace.Trace(MvxTraceLevel level, string tag, string message)
{
_realTrace.Trace(level, tag, message);
}
void IMvxTrace.Trace(MvxTraceLevel level, string tag, string message, params object[] args)
{
_realTrace.Trace(level, tag, message, args);
}
#endregion
#region private helpers
private static string PrependWithTime(string input)
{
var timeIntoApp = (DateTime.UtcNow - WhenTraceStartedUtc).TotalSeconds;
return string.Format("{0,6:0.00} {1}", timeIntoApp, input);
}
private static Func<string> PrependWithTime(Func<string> input)
{
return () =>
{
var result = input();
return PrependWithTime(result);
};
}
#endregion
}
}