2
// MonoMac.CFNetwork.Test.Views.LogViewerController
5
// Martin Baulig (martin.baulig@gmail.com)
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
using System.Diagnostics;
33
using System.Collections.Generic;
35
using MonoMac.Foundation;
38
namespace MonoMac.CFNetwork.Test.Views {
40
public partial class LogViewerController : MonoMac.AppKit.NSWindowController {
43
// Called when created from unmanaged code
44
public LogViewerController (IntPtr handle) : base (handle)
49
// Called when created directly from a XIB file
50
[Export ("initWithCoder:")]
51
public LogViewerController (NSCoder coder) : base (coder)
56
// Call to load from the XIB/NIB file
57
public LogViewerController () : base ("LogViewer")
62
// Shared initialization code
65
writer = new OutputTextWriter (this);
66
listener = new Listener (this);
72
TraceListener listener;
73
OutputTextWriter writer;
75
public override void AwakeFromNib ()
78
font = NSFont.SystemFontOfSize (16.0f);
81
//strongly typed window accessor
82
public new LogViewer Window {
84
return (LogViewer)base.Window;
88
public NSTextStorage Storage {
90
return Text.TextStorage;
94
public TextWriter TextWriter {
100
public override void ShowWindow (NSObject sender)
102
base.ShowWindow (sender);
103
Debug.Listeners.Add (listener);
106
partial void Clear (NSObject sender)
108
Storage.DeleteRange (new NSRange (0, Storage.Length));
111
partial void Quit (NSObject sender)
113
Debug.Listeners.Remove (listener);
117
#region Appending Text
119
public void Append (string text)
121
InvokeOnMainThread (() => DoAppend (text));
124
public void AppendLine (string message)
126
InvokeOnMainThread (() => DoAppend (message + Environment.NewLine));
129
public void AppendLine (NSColor color, string text)
131
InvokeOnMainThread (() => DoAppend (color, text + Environment.NewLine));
134
void DoAppend (string text)
136
var pos = Storage.Length;
137
Storage.Append (new NSAttributedString (text));
138
Text.SetFont (font, new NSRange (pos, text.Length));
141
void DoAppend (NSColor color, string text)
143
var pos = Storage.Length;
144
Storage.Append (new NSAttributedString (text));
145
var range = new NSRange (pos, text.Length);
146
Text.SetFont (font, range);
147
Text.SetTextColor (color, range);
154
class OutputTextWriter : TextWriter {
155
LogViewerController controller;
157
public OutputTextWriter (LogViewerController controller)
159
this.controller = controller;
162
public override Encoding Encoding {
163
get { return Encoding.Default; }
166
public override void Write (char value)
168
controller.Append (value.ToString ());
171
public override void Write (string value)
173
controller.Append (value);
177
class Listener : TraceListener {
178
LogViewerController controller;
180
public Listener (LogViewerController controller)
182
this.controller = controller;
185
#region implemented abstract members of TraceListener
186
public override void Write (string message)
188
controller.Append (message);
191
public override void WriteLine (string message)
193
controller.AppendLine (message);
196
public override void Fail (string message)
201
public override void Fail (string message, string detailMessage)
203
controller.AppendLine ("---- DEBUG ASSERTION FAILED ----");
204
controller.AppendLine ("---- Assert Short Message ----");
205
controller.AppendLine (NSColor.Red, message);
206
controller.AppendLine ("---- Assert Long Message ----");
207
controller.AppendLine (NSColor.Orange, detailMessage);
208
controller.AppendLine ("");