1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-10 17:34:07 +02:00

Move logging to Reporter and make a static instance of it in Program

This commit is contained in:
chylex 2016-09-20 16:38:15 +02:00
parent 3176b6cb8f
commit 4c45d91d4e
2 changed files with 37 additions and 23 deletions

View File

@ -1,8 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using CefSharp;
using TweetDck.Configuration;
@ -37,7 +35,7 @@ static class Program{
public static readonly string TemporaryPath = IsPortable ? Path.Combine(ProgramPath, "portable", "tmp") : Path.Combine(Path.GetTempPath(), BrandName+'_'+Path.GetRandomFileName().Substring(0, 6));
public static readonly string ConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg");
public static readonly string LogFilePath = Path.Combine(ProgramPath, "td-log.txt");
private static readonly string LogFilePath = Path.Combine(ProgramPath, "td-log.txt");
public static readonly string ScriptPath = Path.Combine(ProgramPath, "scripts");
public static readonly string PluginPath = Path.Combine(ProgramPath, "plugins");
@ -48,6 +46,7 @@ static class Program{
private static bool HasCleanedUp;
public static UserConfig UserConfig { get; private set; }
public static Reporter Reporter { get; private set; }
[STAThread]
private static void Main(){
@ -61,6 +60,8 @@ private static void Main(){
return;
}
Reporter = new Reporter(LogFilePath);
string[] programArguments = Environment.GetCommandLineArgs();
if (programArguments.Contains("-restart")){
@ -183,22 +184,8 @@ public static void HandleException(string message, Exception e){ // TODO replace
Reporter.HandleException(BrandName+" Has Failed :(", message, false, new Exception());
}
public static bool Log(string data){
StringBuilder build = new StringBuilder();
if (!File.Exists(LogFilePath)){
build.Append("Please, report all issues to: https://github.com/chylex/TweetDuck/issues\r\n\r\n");
}
build.Append("[").Append(DateTime.Now.ToString("G", CultureInfo.CurrentCulture)).Append("]\r\n");
build.Append(data).Append("\r\n\r\n");
try{
File.AppendAllText(LogFilePath, build.ToString(), Encoding.UTF8);
return true;
}catch{
return false;
}
public static void Log(string message){ // TODO replace all uses
Reporter.Log(message);
}
public static void ReloadConfig(){

View File

@ -1,13 +1,40 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using TweetDck.Core.Other;
namespace TweetDck{
static class Reporter{
public static void HandleException(string caption, string message, bool canIgnore, Exception e){
Program.Log(e.ToString());
class Reporter{
private readonly string logFile;
public Reporter(string logFile){
this.logFile = logFile;
}
public bool Log(string data){
StringBuilder build = new StringBuilder();
if (!File.Exists(logFile)){
build.Append("Please, report all issues to: https://github.com/chylex/TweetDuck/issues\r\n\r\n");
}
build.Append("[").Append(DateTime.Now.ToString("G", CultureInfo.CurrentCulture)).Append("]\r\n");
build.Append(data).Append("\r\n\r\n");
try{
File.AppendAllText(logFile, build.ToString(), Encoding.UTF8);
return true;
}catch{
return false;
}
}
public void HandleException(string caption, string message, bool canIgnore, Exception e){
Log(e.ToString());
FormMessage form = new FormMessage(caption, message+"\r\nError: "+e.Message, canIgnore ? MessageBoxIcon.Warning : MessageBoxIcon.Error);
@ -23,7 +50,7 @@ public static void HandleException(string caption, string message, bool canIgnor
UseVisualStyleBackColor = true
};
btnOpenLog.Click += (sender, args) => Process.Start(Program.LogFilePath);
btnOpenLog.Click += (sender, args) => Process.Start(logFile);
form.AddActionControl(btnOpenLog);