1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.IO; |
6 |
using System.Windows.Forms; |
7 |
|
8 |
namespace EmuXPortal.Logging |
9 |
{ |
10 |
public static class logger |
11 |
{ |
12 |
private static Log log = new Log(); |
13 |
public static void Write(string format, params object[] args) { log.Write(format, args); } |
14 |
public static void Write(Stream s, string format, params object[] args) { log.Write(s, format, args); } |
15 |
public static void WriteLine(string format, params object[] args) { log.WriteLine(format, args); } |
16 |
public static void WriteLine(Stream s, string format, params object[] args) { log.WriteLine(s, format, args); } |
17 |
|
18 |
#region Logger implementation |
19 |
private interface ILog |
20 |
{ |
21 |
void Write(string format, params object[] args); |
22 |
void Write(Stream s, string format, params object[] args); |
23 |
void WriteLine(string format, params object[] args); |
24 |
void WriteLine(Stream s, string format, params object[] args); |
25 |
} |
26 |
private class Log : ILog |
27 |
{ |
28 |
static string APP_PATH = Application.StartupPath; |
29 |
static string log_file = "EmuXPortal.log"; |
30 |
string log_file_path = string.Format(@"{0}\{1}", APP_PATH, log_file); |
31 |
#region ILog Members |
32 |
public void Write(string format, params object[] args) |
33 |
{ |
34 |
FileMode mode = FileMode.OpenOrCreate; |
35 |
FileAccess access = FileAccess.ReadWrite; |
36 |
if (new FileInfo(log_file_path).Exists) { mode = FileMode.Append; access = FileAccess.Write; } |
37 |
using (FileStream fs = new FileStream(log_file_path, mode, access, FileShare.ReadWrite)) { Write(fs, format, args); } |
38 |
format = null; |
39 |
args = null; |
40 |
} |
41 |
public void Write(Stream s, string format, params object[] args) |
42 |
{ |
43 |
using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8)) |
44 |
{ |
45 |
sw.AutoFlush = true; |
46 |
Log_Write(sw, format, args); |
47 |
sw.Close(); |
48 |
} |
49 |
format = null; |
50 |
args = null; |
51 |
} |
52 |
public void WriteLine(string format, params object[] args) |
53 |
{ |
54 |
FileMode mode = FileMode.OpenOrCreate; |
55 |
FileAccess access = FileAccess.ReadWrite; |
56 |
if (new FileInfo(log_file_path).Exists) { mode = FileMode.Append; access = FileAccess.Write; } |
57 |
using (FileStream fs = new FileStream(log_file_path, mode, access, FileShare.ReadWrite)) { WriteLine(fs, format, args); } |
58 |
format = null; |
59 |
args = null; |
60 |
} |
61 |
public void WriteLine(Stream s, string format, params object[] args) |
62 |
{ |
63 |
using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8)) |
64 |
{ |
65 |
sw.AutoFlush = true; |
66 |
Log_WriteLine(sw, format, args); |
67 |
format = null; |
68 |
args = null; |
69 |
sw.Close(); |
70 |
} |
71 |
s.Dispose(); |
72 |
s = null; |
73 |
} |
74 |
|
75 |
private string GenerateTimestamp() |
76 |
{ |
77 |
DateTime now = DateTime.Now; |
78 |
string timestamp = ""; |
79 |
timestamp = string.Format("{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00} {6} - ", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.ToString("tt")); |
80 |
return timestamp; |
81 |
} |
82 |
|
83 |
private void Log_Write(StreamWriter sw, string format, params object[] args) |
84 |
{ |
85 |
string formatted_string = string.Format(format, args); |
86 |
string timestamp = GenerateTimestamp(); |
87 |
string t = string.Format("{0}{1}", timestamp, formatted_string); |
88 |
timestamp = null; |
89 |
sw.Write(t); |
90 |
Console.Write(t); |
91 |
formatted_string = null; |
92 |
t = null; |
93 |
format = null; |
94 |
args = null; |
95 |
} |
96 |
private void Log_WriteLine(StreamWriter sw, string format, params object[] args) |
97 |
{ |
98 |
string formatted_string = string.Format(format, args); |
99 |
string timestamp = GenerateTimestamp(); |
100 |
string t = string.Format("{0}{1}", timestamp, formatted_string); |
101 |
timestamp = null; |
102 |
sw.WriteLine(t); |
103 |
Console.WriteLine(t); |
104 |
formatted_string = null; |
105 |
t = null; |
106 |
format = null; |
107 |
args = null; |
108 |
} |
109 |
#endregion |
110 |
} |
111 |
#endregion |
112 |
} |
113 |
} |