Menu

[r477]: / trunk / GoogleContactsSync / ILogger.cs  Maximize  Restore  History

Download this file

137 lines (114 with data), 4.2 kB

  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
using System;
using System.Collections.Generic;
using System.IO;
namespace GoContactSyncMod
{
enum EventType
{
Debug,
Information,
Warning,
Error
}
struct LogEntry
{
public DateTime date;
public EventType type;
public string msg;
public LogEntry(DateTime _date, EventType _type, string _msg)
{
date = _date; type = _type; msg = _msg;
}
}
static class Logger
{
public static List<LogEntry> messages = new List<LogEntry>();
public delegate void LogUpdatedHandler(string Message);
public static event LogUpdatedHandler LogUpdated;
private static StreamWriter logwriter;
public static readonly string Folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Go Contact Sync Mod\\";
static Logger()
{
if (!Directory.Exists(Folder))
Directory.CreateDirectory(Folder);
try
{
string logFileName = Folder + "log.txt";
//If log file is bigger than 1 MB, move it to backup file and create new file
FileInfo logFile = new FileInfo(logFileName);
if (logFile.Exists && logFile.Length >= 1000000)
File.Move(logFileName, logFileName + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"));
logwriter = new StreamWriter(logFileName, true);
logwriter.WriteLine("[Start Rolling]");
logwriter.Flush();
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
public static void Close()
{
try
{
if (logwriter != null)
{
logwriter.WriteLine("[End Rolling]");
logwriter.Flush();
logwriter.Close();
}
}
catch(Exception e)
{
ErrorHandler.Handle(e);
}
}
private static string formatMessage(string message, EventType eventType)
{
return String.Format("{0}:{1}{2}", eventType, Environment.NewLine, message);
}
private static string GetLogLine(LogEntry entry)
{
return String.Format("[{0} | {1}]\t{2}\r\n", entry.date, entry.type, entry.msg);
}
public static void Log(string message, EventType eventType)
{
LogEntry new_logEntry = new LogEntry(DateTime.Now, eventType, message);
messages.Add(new_logEntry);
try
{
logwriter.Write(GetLogLine(new_logEntry));
logwriter.Flush();
}
catch (Exception)
{
//ignore it, because if you handle this error, the handler will again log the message
//ErrorHandler.Handle(ex);
}
//Populate LogMessage to all subscribed Logger-Outputs, but only if not Debug message, Debug messages are only logged to logfile
if (LogUpdated != null && eventType > EventType.Debug)
LogUpdated(GetLogLine(new_logEntry));
}
/*
public void LogUnique(string message, EventType eventType)
{
string logMessage = formatMessage(message, eventType);
if (!messages.ContainsValue(logMessage))
messages.Add(DateTime.Now, logMessage); //TODO: Outdated, no dictionary used anymore.
}
*/
public static void ClearLog()
{
messages.Clear();
}
/*
public string GetText()
{
StringBuilder output = new StringBuilder();
foreach (var logitem in messages)
output.AppendLine(GetLogLine(logitem));
return output.ToString();
}
*/
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.