ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/trunk/EmuXPortal/Logging/logger.cs
Revision: 14
Committed: Tue Apr 3 21:35:13 2012 UTC (11 years, 10 months ago) by william
File size: 4147 byte(s)
Log Message:
add logging support

File Contents

# User Rev Content
1 william 14 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     }
39     public void Write(Stream s, string format, params object[] args)
40     {
41     using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8))
42     {
43     sw.AutoFlush = true;
44     Log_Write(sw, format, args);
45     sw.Close();
46     }
47     }
48     public void WriteLine(string format, params object[] args)
49     {
50     FileMode mode = FileMode.OpenOrCreate;
51     FileAccess access = FileAccess.ReadWrite;
52     if (new FileInfo(log_file_path).Exists) { mode = FileMode.Append; access = FileAccess.Write; }
53     using (FileStream fs = new FileStream(log_file_path, mode, access, FileShare.ReadWrite)) { WriteLine(fs, format, args); }
54     }
55     public void WriteLine(Stream s, string format, params object[] args)
56     {
57     using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8))
58     {
59     sw.AutoFlush = true;
60     Log_WriteLine(sw, format, args);
61     sw.Close();
62     }
63     }
64    
65     private string GenerateTimestamp()
66     {
67     DateTime now = DateTime.Now;
68     string timestamp = "";
69     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"));
70     return timestamp;
71     }
72    
73     private void Log_Write(StreamWriter sw, string format, params object[] args)
74     {
75     string formatted_string = string.Format(format, args);
76     string t = string.Format("{0}{1}", GenerateTimestamp(), formatted_string);
77     sw.Write(t);
78     Console.Write(t);
79     }
80     private void Log_WriteLine(StreamWriter sw, string format, params object[] args)
81     {
82     string formatted_string = string.Format(format, args);
83     string t = string.Format("{0}{1}", GenerateTimestamp(), formatted_string);
84     sw.WriteLine(t);
85     Console.WriteLine(t);
86     }
87     #endregion
88     }
89     #endregion
90     }
91     }