|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.IO; |
|
21 using System.Windows.Forms; |
|
22 using SymbianUtils; |
|
23 using SymbianUtils.Tracer; |
|
24 |
|
25 namespace SymbianUtils.FileSystem |
|
26 { |
|
27 public sealed class FSLog : DisposableObject, ITracer |
|
28 { |
|
29 #region Constructors |
|
30 public FSLog() |
|
31 : this( false ) |
|
32 { |
|
33 } |
|
34 |
|
35 public FSLog( bool aIncludeTimeStamp ) |
|
36 { |
|
37 iIncludeTimeStamp = aIncludeTimeStamp; |
|
38 // |
|
39 try |
|
40 { |
|
41 string path = Application.ExecutablePath; |
|
42 string exe = Path.GetFileName( path ); |
|
43 path = Path.GetDirectoryName( path ); |
|
44 // |
|
45 string file = Path.Combine( path, exe + ".debug.txt" ); |
|
46 iStream = new StreamWriter( new FileStream( file, FileMode.Create ) ); |
|
47 iStream.AutoFlush = true; |
|
48 } |
|
49 catch ( Exception ) |
|
50 { |
|
51 } |
|
52 } |
|
53 #endregion |
|
54 |
|
55 #region API |
|
56 public void TraceAlways( string aMessage ) |
|
57 { |
|
58 DoTrace( aMessage, true ); |
|
59 } |
|
60 #endregion |
|
61 |
|
62 #region Properties |
|
63 public bool Verbose |
|
64 { |
|
65 get { return iVerbose; } |
|
66 set { iVerbose = value; } |
|
67 } |
|
68 #endregion |
|
69 |
|
70 #region Internal methods |
|
71 private void DoTrace( string aLine, bool aDiagnostics ) |
|
72 { |
|
73 if ( aDiagnostics ) |
|
74 { |
|
75 System.Diagnostics.Debug.WriteLine( aLine ); |
|
76 } |
|
77 |
|
78 // Try to output to file |
|
79 if ( iStream != null ) |
|
80 { |
|
81 try |
|
82 { |
|
83 iStream.WriteLine( aLine ); |
|
84 } |
|
85 catch ( Exception ) |
|
86 { |
|
87 iStream.Close(); |
|
88 iStream = null; |
|
89 } |
|
90 } |
|
91 } |
|
92 #endregion |
|
93 |
|
94 #region DisposableObject |
|
95 protected override void CleanupManagedResources() |
|
96 { |
|
97 try |
|
98 { |
|
99 base.CleanupManagedResources(); |
|
100 } |
|
101 finally |
|
102 { |
|
103 if ( iStream != null ) |
|
104 { |
|
105 iStream.Close(); |
|
106 } |
|
107 iStream = null; |
|
108 } |
|
109 } |
|
110 #endregion |
|
111 |
|
112 #region From ITracer |
|
113 public void Trace( string aMessage ) |
|
114 { |
|
115 StringBuilder text = new StringBuilder( aMessage ); |
|
116 if ( iIncludeTimeStamp ) |
|
117 { |
|
118 DateTime now = DateTime.Now; |
|
119 text.Insert( 0, now.ToLongTimeString() + " - " ); |
|
120 } |
|
121 |
|
122 string msg = text.ToString(); |
|
123 System.Diagnostics.Debug.WriteLine( msg ); |
|
124 |
|
125 if ( iVerbose ) |
|
126 { |
|
127 System.Console.WriteLine( msg ); |
|
128 DoTrace( msg, false ); |
|
129 } |
|
130 } |
|
131 |
|
132 public void Trace( string aFormat, params object[] aParams ) |
|
133 { |
|
134 string text = string.Format( aFormat, aParams ); |
|
135 Trace( text ); |
|
136 } |
|
137 #endregion |
|
138 |
|
139 #region Data members |
|
140 private readonly bool iIncludeTimeStamp; |
|
141 private StreamWriter iStream; |
|
142 private bool iVerbose = false; |
|
143 #endregion |
|
144 } |
|
145 } |