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

Add IAppErrorHandler w/ implementation

This commit is contained in:
chylex 2019-05-26 13:56:36 +02:00
parent da706d7ed8
commit 77030c9b23
4 changed files with 20 additions and 3 deletions

View File

@ -58,6 +58,7 @@ static Program(){
Config = new ConfigManager(); Config = new ConfigManager();
Lib.Initialize(new App.Builder{ Lib.Initialize(new App.Builder{
ErrorHandler = Reporter
}); });
} }

View File

@ -7,9 +7,10 @@
using TweetDuck.Configuration; using TweetDuck.Configuration;
using TweetDuck.Core.Other; using TweetDuck.Core.Other;
using TweetLib.Core; using TweetLib.Core;
using TweetLib.Core.Application;
namespace TweetDuck{ namespace TweetDuck{
sealed class Reporter{ sealed class Reporter : IAppErrorHandler{
private readonly string logFile; private readonly string logFile;
public Reporter(string logFile){ public Reporter(string logFile){
@ -29,8 +30,12 @@ public bool LogVerbose(string data){
} }
public bool LogImportant(string data){ public bool LogImportant(string data){
return ((IAppErrorHandler)this).Log(data);
}
bool IAppErrorHandler.Log(string text){
#if DEBUG #if DEBUG
Debug.WriteLine(data); Debug.WriteLine(text);
#endif #endif
StringBuilder build = new StringBuilder(); StringBuilder build = new StringBuilder();
@ -40,7 +45,7 @@ public bool LogImportant(string data){
} }
build.Append("[").Append(DateTime.Now.ToString("G", Lib.Culture)).Append("]\r\n"); build.Append("[").Append(DateTime.Now.ToString("G", Lib.Culture)).Append("]\r\n");
build.Append(data).Append("\r\n\r\n"); build.Append(text).Append("\r\n\r\n");
try{ try{
File.AppendAllText(logFile, build.ToString(), Encoding.UTF8); File.AppendAllText(logFile, build.ToString(), Encoding.UTF8);

View File

@ -3,14 +3,17 @@
namespace TweetLib.Core{ namespace TweetLib.Core{
public sealed class App{ public sealed class App{
public static IAppErrorHandler ErrorHandler { get; private set; }
// Builder // Builder
public sealed class Builder{ public sealed class Builder{
public IAppErrorHandler? ErrorHandler { get; set; }
// Validation // Validation
internal void Initialize(){ internal void Initialize(){
App.ErrorHandler = Validate(ErrorHandler, nameof(ErrorHandler))!;
} }
private T Validate<T>(T obj, string name){ private T Validate<T>(T obj, string name){

View File

@ -0,0 +1,8 @@
using System;
namespace TweetLib.Core.Application{
public interface IAppErrorHandler{
bool Log(string text);
void HandleException(string caption, string message, bool canIgnore, Exception e);
}
}