1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-09 06:15:49 +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();
Lib.Initialize(new App.Builder{
ErrorHandler = Reporter
});
}

View File

@ -7,9 +7,10 @@
using TweetDuck.Configuration;
using TweetDuck.Core.Other;
using TweetLib.Core;
using TweetLib.Core.Application;
namespace TweetDuck{
sealed class Reporter{
sealed class Reporter : IAppErrorHandler{
private readonly string logFile;
public Reporter(string logFile){
@ -29,8 +30,12 @@ public bool LogVerbose(string data){
}
public bool LogImportant(string data){
return ((IAppErrorHandler)this).Log(data);
}
bool IAppErrorHandler.Log(string text){
#if DEBUG
Debug.WriteLine(data);
Debug.WriteLine(text);
#endif
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(data).Append("\r\n\r\n");
build.Append(text).Append("\r\n\r\n");
try{
File.AppendAllText(logFile, build.ToString(), Encoding.UTF8);

View File

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