Removed the old project
This commit is contained in:
parent
0df278d861
commit
eed681e3bc
|
@ -1,21 +0,0 @@
|
|||
using SweetLib.Classes.Storer;
|
||||
|
||||
namespace SweetLib.Classes.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Exception thrown by <see cref="RegistryStorer"/>.
|
||||
/// </summary>
|
||||
public class RegistryStorerException : System.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RegistryStorerException"/>.
|
||||
/// </summary>
|
||||
public RegistryStorerException(){}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RegistryStorerException"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">Exception message.</param>
|
||||
public RegistryStorerException(string message):base(message) {}
|
||||
}
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
namespace SweetLib.Classes.Storer
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface, which provides several methods to store simple data.
|
||||
/// </summary>
|
||||
public interface IStorer
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads a string value.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <param name="defaultValue">Default value, if this value does not exist.</param>
|
||||
/// <returns>Value of the <see cref="key"/> in <see cref="section"/>.</returns>
|
||||
string ReadString(string section, string key, string defaultValue = "");
|
||||
|
||||
/// <summary>
|
||||
/// Reads an integer value.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <param name="defaultValue">Default value, if this value does not exist.</param>
|
||||
/// <returns>Value of the <see cref="key"/> in <see cref="section"/>.</returns>
|
||||
int ReadInteger(string section, string key, int defaultValue = 0);
|
||||
|
||||
/// <summary>
|
||||
/// Reads a bool value.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <param name="defaultValue">Default value, if this value does not exist.</param>
|
||||
/// <returns>Value of the <see cref="key"/> in <see cref="section"/>.</returns>
|
||||
bool ReadBool(string section, string key, bool defaultValue = false);
|
||||
|
||||
/// <summary>
|
||||
/// Checks, if a key exists inside a section.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <returns>True, if <see cref="key"/> is found inside <see cref="section"/>.</returns>
|
||||
bool HasKey(string section, string key);
|
||||
|
||||
/// <summary>
|
||||
/// Writes a string value.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <param name="value">Value to be stored.</param>
|
||||
void WriteString(string section, string key, string value);
|
||||
|
||||
/// <summary>
|
||||
/// Writes an integer value.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <param name="value">Value to be stored.</param>
|
||||
void WriteInteger(string section, string key, int value);
|
||||
|
||||
/// <summary>
|
||||
/// Writes a bool value.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key of the stored data.</param>
|
||||
/// <param name="value">Value to be stored.</param>
|
||||
void WriteBool(string section, string key, bool value);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a key inside a <see cref="section"/>.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||
/// <param name="key">Represents the key to be deleted.</param>
|
||||
void DeleteKey(string section, string key);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a section with all its keys.
|
||||
/// </summary>
|
||||
/// <param name="section">Represents the section to be deleted.</param>
|
||||
void DeleteSection(string section);
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace SweetLib.Classes.Storer
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of an <see cref="IStorer"/> interface which stores the data inside an ini file.
|
||||
/// </summary>
|
||||
public class IniFileStorer : IStorer
|
||||
{
|
||||
/// <summary>
|
||||
/// Ini file path.
|
||||
/// </summary>
|
||||
public string FileName { get; }
|
||||
|
||||
[DllImport("kernel32", CharSet = CharSet.Unicode)]
|
||||
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
|
||||
|
||||
[DllImport("kernel32", CharSet = CharSet.Unicode)]
|
||||
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder value, int size, string filePath);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="IniFileStorer"/> with a specified file name.
|
||||
/// </summary>
|
||||
/// <param name="fileName">The file name of the ini file.</param>
|
||||
public IniFileStorer(string fileName)
|
||||
{
|
||||
FileName = fileName;
|
||||
}
|
||||
|
||||
public string ReadString(string section, string key, string defaultValue = "")
|
||||
{
|
||||
var builder = new StringBuilder(255);
|
||||
GetPrivateProfileString(section, key, defaultValue, builder, 255, FileName);
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public int ReadInteger(string section, string key, int defaultValue = 0)
|
||||
{
|
||||
int result;
|
||||
if (!int.TryParse(ReadString(section, key, defaultValue.ToString()), out result))
|
||||
result = defaultValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool ReadBool(string section, string key, bool defaultValue = false)
|
||||
{
|
||||
return ReadInteger(section, key, defaultValue ? 1 : 0) > 0;
|
||||
}
|
||||
|
||||
public bool HasKey(string section, string key)
|
||||
{
|
||||
return ReadString(section, key).Length > 0;
|
||||
}
|
||||
|
||||
public void WriteString(string section, string key, string value)
|
||||
{
|
||||
WritePrivateProfileString(section, key, value, FileName);
|
||||
}
|
||||
|
||||
public void WriteInteger(string section, string key, int value)
|
||||
{
|
||||
WriteString(section, key, value.ToString());
|
||||
}
|
||||
|
||||
public void WriteBool(string section, string key, bool value)
|
||||
{
|
||||
WriteInteger(section, key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
public void DeleteKey(string section, string key)
|
||||
{
|
||||
WriteString(section, key, null);
|
||||
}
|
||||
|
||||
public void DeleteSection(string section)
|
||||
{
|
||||
WriteString(section, null, null);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
using Microsoft.Win32;
|
||||
using SweetLib.Classes.Exception;
|
||||
|
||||
namespace SweetLib.Classes.Storer
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of an <see cref="IStorer"/> interface which stores the data inside the registry.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Sections will be interpreted as subkeys on registry level.
|
||||
/// </remarks>
|
||||
public class RegistryStorer : IStorer
|
||||
{
|
||||
/// <summary>
|
||||
/// The base registry key in which will be operated.
|
||||
/// </summary>
|
||||
public RegistryKey OperatingRegistryKey { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="RegistryStorer"/> with a specified application name.
|
||||
/// </summary>
|
||||
/// <param name="appName">The applications base name. This will be used as name for a sub key inside the software key below the base key.</param>
|
||||
/// <remarks>
|
||||
/// This will use current user as the base key.
|
||||
/// </remarks>
|
||||
public RegistryStorer(string appName) : this(Registry.CurrentUser, appName) { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="RegistryStorer"/> with a specified application name.
|
||||
/// </summary>
|
||||
/// <param name="baseRegistryKey">Provide a key of <see cref="Registry"/>, e.G. <i>Registry.CurrentUser</i>.</param>
|
||||
/// <param name="appName">The applications base name. This will be used as name for a sub key inside the software key below the base key.</param>
|
||||
public RegistryStorer(RegistryKey baseRegistryKey, string appName)
|
||||
{
|
||||
baseRegistryKey = baseRegistryKey.CreateSubKey("SOFTWARE");
|
||||
OperatingRegistryKey = baseRegistryKey?.CreateSubKey(appName);
|
||||
|
||||
if (OperatingRegistryKey == null)
|
||||
throw new RegistryStorerException("Unable to create registriy key.");
|
||||
}
|
||||
|
||||
public string ReadString(string section, string key, string defaultValue = "")
|
||||
{
|
||||
var localRegKey = OperatingRegistryKey.OpenSubKey(section);
|
||||
return (string)localRegKey?.GetValue(key.ToUpper());
|
||||
}
|
||||
|
||||
public int ReadInteger(string section, string key, int defaultValue = 0)
|
||||
{
|
||||
int result;
|
||||
if (!int.TryParse(ReadString(section, key, defaultValue.ToString()), out result))
|
||||
result = defaultValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool ReadBool(string section, string key, bool defaultValue = false)
|
||||
{
|
||||
return ReadInteger(section, key, defaultValue ? 1 : 0) > 0;
|
||||
}
|
||||
|
||||
public bool HasKey(string section, string key)
|
||||
{
|
||||
return ReadString(section, key).Length > 0;
|
||||
}
|
||||
|
||||
public void WriteString(string section, string key, string value)
|
||||
{
|
||||
var localRegKey = OperatingRegistryKey.CreateSubKey(section);
|
||||
localRegKey?.SetValue(key.ToUpper(), value);
|
||||
}
|
||||
|
||||
public void WriteInteger(string section, string key, int value)
|
||||
{
|
||||
WriteString(section, key, value.ToString());
|
||||
}
|
||||
|
||||
public void WriteBool(string section, string key, bool value)
|
||||
{
|
||||
WriteInteger(section, key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
public void DeleteKey(string section, string key)
|
||||
{
|
||||
var localRegKey = OperatingRegistryKey.CreateSubKey(section);
|
||||
localRegKey?.DeleteValue(key);
|
||||
}
|
||||
|
||||
public void DeleteSection(string section)
|
||||
{
|
||||
OperatingRegistryKey.DeleteSubKey(section);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||
// die einer Assembly zugeordnet sind.
|
||||
[assembly: AssemblyTitle("SweetLib")]
|
||||
[assembly: AssemblyDescription("Sweet collection of helpful utils in .Net")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Serraniel")]
|
||||
[assembly: AssemblyProduct("SweetLib")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017 by Serraniel")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
|
||||
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||
[assembly: Guid("02c1f8ef-32f2-4e77-a36d-79129402af37")]
|
||||
|
||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||
//
|
||||
// Hauptversion
|
||||
// Nebenversion
|
||||
// Buildnummer
|
||||
// Revision
|
||||
//
|
||||
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
|
||||
// übernehmen, indem Sie "*" eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("0.1.26.0")]
|
||||
[assembly: AssemblyFileVersion("0.1.26.0")]
|
|
@ -1,66 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{02C1F8EF-32F2-4E77-A36D-79129402AF37}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SweetLib</RootNamespace>
|
||||
<AssemblyName>SweetLib</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Classes\Exception\RegistryStorerException.cs" />
|
||||
<Compile Include="Classes\Storer\IniFileStorer.cs" />
|
||||
<Compile Include="Classes\Storer\IStorer.cs" />
|
||||
<Compile Include="Classes\Storer\RegistryStorer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utils\Logger\Memory\ArchivableConsoleLogMemory.cs" />
|
||||
<Compile Include="Utils\Logger\Memory\ILogMemory.cs" />
|
||||
<Compile Include="Utils\Logger\Message\LogMessageFormatter.cs" />
|
||||
<Compile Include="Utils\Logger\Logger.cs" />
|
||||
<Compile Include="Utils\Logger\Message\LogMessage.cs" />
|
||||
<Compile Include="Utils\SweetUtils.cs" />
|
||||
<Compile Include="Utils\TaskBar\TaskBarProgress.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
|
@ -1,18 +0,0 @@
|
|||
<?xml version="1.0"?>
|
||||
<package >
|
||||
<metadata>
|
||||
<id>$id$</id>
|
||||
<version>$version$-alpha</version>
|
||||
<title>$title$</title>
|
||||
<authors>Serraniel</authors>
|
||||
<owners>$author$</owners>
|
||||
<licenseUrl>https://opensource.org/licenses/GPL-3.0</licenseUrl>
|
||||
<projectUrl>https://github.com/Serraniel/SweetLib</projectUrl>
|
||||
<iconUrl>https://github.com/Serraniel/SweetLib/blob/develop/nuget_icon.png?raw=true</iconUrl>
|
||||
<requireLicenseAcceptance>true</requireLicenseAcceptance>
|
||||
<description>$description$</description>
|
||||
<releaseNotes>First pre version just to try that nuget package thing :)</releaseNotes>
|
||||
<copyright>Copyright © 2017 Serraniel</copyright>
|
||||
<tags>Utility</tags>
|
||||
</metadata>
|
||||
</package>
|
|
@ -1,128 +0,0 @@
|
|||
using System;
|
||||
using SweetLib.Utils.Logger.Memory;
|
||||
using SweetLib.Utils.Logger.Message;
|
||||
|
||||
namespace SweetLib.Utils.Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// Enum which contains the several log levels.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum LogLevel
|
||||
{
|
||||
None = 0,
|
||||
Trace = 1 << 0,
|
||||
Debug = 1 << 1,
|
||||
Info = 1 << 2,
|
||||
Warn = 1 << 3,
|
||||
Error = 1 << 4,
|
||||
|
||||
All = Int32.MaxValue,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Global logger class providing several methods to log events by the application.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// As <see cref="DefaultLogMemory"/> a <see cref="ArchivableConsoleLogMemory"/> will be used. You can change this to any other implementation at any time while runtime.
|
||||
/// Default log levels are set as bitflags in <see cref="GlobalLogLevel"/>.
|
||||
/// </remarks>
|
||||
public static class Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// The global log level. Only messages with the set <see cref="LogLevel"/> will be procedered.
|
||||
/// </summary>
|
||||
public static LogLevel GlobalLogLevel { get; set; } = LogLevel.Info | LogLevel.Warn | LogLevel.Error;
|
||||
|
||||
/// <summary>
|
||||
/// The default <see cref="ILogMemory"/> which will be used for any logging action, if no custom <see cref="ILogMemory"/> is set as parameter.
|
||||
/// </summary>
|
||||
public static ILogMemory DefaultLogMemory = new ArchivableConsoleLogMemory();
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message into the global <see cref="DefaultLogMemory"/>.
|
||||
/// </summary>
|
||||
/// <param name="logLevel">The log level of this message.</param>
|
||||
/// <param name="message">The message to log.</param>
|
||||
public static void Log(LogLevel logLevel, string message)
|
||||
{
|
||||
Log(logLevel, message, DefaultLogMemory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message into the provided <see cref="logMemory"/>.
|
||||
/// </summary>
|
||||
/// <param name="logLevel">The log level of this message.</param>
|
||||
/// /// <param name="message">The message to log.</param>
|
||||
/// <param name="logMemory">The <see cref="ILogMemory"/> to store the <see cref="message"/> into.</param>
|
||||
public static void Log(LogLevel logLevel, string message, ILogMemory logMemory)
|
||||
{
|
||||
Log(new LogMessage(logLevel, message), logMemory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message into the provided <see cref="logMemory"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">A <see cref="LogMessage"/> object to store.</param>
|
||||
/// <param name="logMemory">The <see cref="ILogMemory"/> to store the <see cref="message"/> into.</param>
|
||||
/// <remarks>In general use cases you should either use one of the <see cref="Log(SweetLib.Utils.Logger.LogLevel,string)"/> or <see cref="Log(SweetLib.Utils.Logger.LogLevel,string)"/> methods which will generate a call to this method.</remarks>
|
||||
public static void Log(LogMessage message, ILogMemory logMemory)
|
||||
{
|
||||
if (message == null)
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
|
||||
if (logMemory == null)
|
||||
throw new ArgumentNullException(nameof(logMemory));
|
||||
|
||||
if ((GlobalLogLevel & message.LogLevel) == LogLevel.None)
|
||||
return;
|
||||
|
||||
logMemory.Remember(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message with the <see cref="LogLevel.Trace"/> log level.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to log.</param>
|
||||
public static void Trace(string message)
|
||||
{
|
||||
Log(LogLevel.Trace, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message with the <see cref="LogLevel.Debug"/> log level.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to log.</param>
|
||||
public static void Debug(string message)
|
||||
{
|
||||
Log(LogLevel.Debug, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message with the <see cref="LogLevel.Info"/> log level.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to log.</param>
|
||||
public static void Info(string message)
|
||||
{
|
||||
Log(LogLevel.Info, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message with the <see cref="LogLevel.Warn"/> log level.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to log.</param>
|
||||
public static void Warn(string message)
|
||||
{
|
||||
Log(LogLevel.Warn, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will log a message with the <see cref="LogLevel.Error"/> log level.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to log.</param>
|
||||
public static void Error(string message)
|
||||
{
|
||||
Log(LogLevel.Error, message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,163 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using SweetLib.Utils.Logger.Message;
|
||||
|
||||
namespace SweetLib.Utils.Logger.Memory
|
||||
{
|
||||
public class ArchivableConsoleLogMemory : ILogMemory
|
||||
{
|
||||
private string TempFile { get; } = Path.GetTempFileName();
|
||||
|
||||
private Timer QueueTimer { get; }
|
||||
|
||||
private ConcurrentQueue<LogMessage> LogQueue { get; } = new ConcurrentQueue<LogMessage>();
|
||||
|
||||
public string ArchiveFile { get; set; } = null;
|
||||
|
||||
public bool AutoArchiveOnDispose { get; set; } = true;
|
||||
|
||||
public ArchivableConsoleLogMemory() : this(null) { }
|
||||
|
||||
public ArchivableConsoleLogMemory(string archiveFile)
|
||||
{
|
||||
ArchiveFile = archiveFile;
|
||||
|
||||
QueueTimer = new Timer(e => ProcessQueue(), null, TimeSpan.Zero, TimeSpan.FromSeconds(30));
|
||||
}
|
||||
|
||||
~ArchivableConsoleLogMemory()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private void ProcessQueue(bool isDisposing = false)
|
||||
{
|
||||
if (LogQueue.IsEmpty)
|
||||
return;
|
||||
|
||||
// if we are disposing no need to lock. This might cause issues!
|
||||
if (!isDisposing)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (LogQueue.IsEmpty)
|
||||
return;
|
||||
|
||||
LogMessage message;
|
||||
if (LogQueue.TryDequeue(out message))
|
||||
File.AppendAllText(TempFile, message.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (LogQueue.IsEmpty)
|
||||
return;
|
||||
|
||||
LogMessage message;
|
||||
if (LogQueue.TryDequeue(out message))
|
||||
File.AppendAllText(TempFile, message.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void Remember(LogMessage message)
|
||||
{
|
||||
var consoleColor = Console.ForegroundColor;
|
||||
|
||||
switch (message.LogLevel)
|
||||
{
|
||||
case LogLevel.Trace:
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
break;
|
||||
case LogLevel.Warn:
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine(message.ToString());
|
||||
LogQueue.Enqueue(message);
|
||||
|
||||
Console.ForegroundColor = consoleColor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Forget(LogMessage message)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
var lines = File.ReadAllLines(TempFile).ToList();
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (line == message.ToString())
|
||||
lines.Remove(line);
|
||||
}
|
||||
|
||||
File.WriteAllLines(TempFile, lines);
|
||||
}
|
||||
}
|
||||
|
||||
public void Archive(string fullFileName = null)
|
||||
{
|
||||
if (fullFileName == null)
|
||||
fullFileName = ArchiveFile;
|
||||
|
||||
if (fullFileName == null)
|
||||
throw new FileNotFoundException("target file not found");
|
||||
|
||||
using (var tmpFileStream = new FileInfo(TempFile).OpenRead())
|
||||
{
|
||||
if (!Directory.Exists(Path.GetDirectoryName(fullFileName)))
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(fullFileName));
|
||||
|
||||
using (var compressedFileStream = File.Create(fullFileName))
|
||||
{
|
||||
using (var compressionsStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
|
||||
{
|
||||
tmpFileStream.CopyTo(compressionsStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
QueueTimer.Dispose();
|
||||
|
||||
ProcessQueue(true);
|
||||
|
||||
if (AutoArchiveOnDispose)
|
||||
{
|
||||
try
|
||||
{
|
||||
Archive();
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
if (disposing)
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
File.Delete(TempFile);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
using SweetLib.Utils.Logger.Message;
|
||||
|
||||
namespace SweetLib.Utils.Logger.Memory
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for a class to store and proceed <see cref="LogMessage"/> objects.
|
||||
/// </summary>
|
||||
public interface ILogMemory
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a <see cref="message"/> into the <see cref="ILogMemory"/>.
|
||||
/// </summary>
|
||||
/// <param name="message"><see cref="LogMessage"/> to be stored.</param>
|
||||
void Remember(LogMessage message);
|
||||
|
||||
/// <summary>
|
||||
/// Removes a <see cref="message"/> from the <see cref="ILogMemory"/>.
|
||||
/// </summary>
|
||||
/// <param name="message"><see cref="LogMessage"/> to be removed.</param>
|
||||
/// <remarks>This might not have any effect depending on the <see cref="ILogMemory"/> implementation.</remarks>
|
||||
void Forget(LogMessage message);
|
||||
|
||||
/// <summary>
|
||||
/// Saves all remembered <see cref="LogMessage"/> objects into a persistent file.
|
||||
/// </summary>
|
||||
/// <param name="fullFileName">File name to store the <see cref="LogMessage"/> objects.</param>
|
||||
void Archive(string fullFileName);
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SweetLib.Utils.Logger.Message
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="LogMessage"/> contains all event log data which should be logged in as a single log message.
|
||||
/// </summary>
|
||||
public class LogMessage : IFormattable
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="LogLevel"/> of this event log.
|
||||
/// </summary>
|
||||
public LogLevel LogLevel { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The message of this event log.
|
||||
/// </summary>
|
||||
public string Message { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The date and time of this event log.
|
||||
/// </summary>
|
||||
public DateTime LogDateTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="LogMessage"/> instance. <see cref="DateTime.Now"/> will be the <see cref="LogDateTime"/>.
|
||||
/// </summary>
|
||||
/// <param name="logLevel">The log level of this event log.</param>
|
||||
/// <param name="message">The message of this event log.</param>
|
||||
public LogMessage(LogLevel logLevel, string message) : this(logLevel, message, DateTime.Now) { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="LogMessage"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="logLevel">The log level of this event log.</param>
|
||||
/// <param name="message">The message of this event log.</param>
|
||||
/// <param name="logDateTime">The <see cref="DateTime"/> of this event log.</param>
|
||||
public LogMessage(LogLevel logLevel, string message, DateTime logDateTime)
|
||||
{
|
||||
if (message == null)
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
|
||||
if (logDateTime == null)
|
||||
throw new ArgumentNullException(nameof(logDateTime));
|
||||
|
||||
LogLevel = logLevel;
|
||||
Message = message;
|
||||
LogDateTime = logDateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a formatted <see cref="string"/> of this event log. <see cref="LogMessageFormatter.DefaultFormatString"/> will be used to format this event log.
|
||||
/// </summary>
|
||||
/// <returns>A formated <see cref="string"/> of this event log.</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return ToString(LogMessageFormatter.DefaultFormatString, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a formatted <see cref="string"/> of this event log with a given format.
|
||||
/// </summary>
|
||||
/// <param name="format">The format to be used. See <see cref="LogMessageFormatter"/> for more format information.</param>
|
||||
/// <param name="formatProvider">Optional, an <see cref="IFormatProvider"/> interface to be used while formatting if needed.</param>
|
||||
/// <returns>A formated <see cref="string"/> of this event log.</returns>
|
||||
public string ToString(string format, IFormatProvider formatProvider = null)
|
||||
{
|
||||
return LogMessageFormatter.Instance.Format(format, this, formatProvider);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SweetLib.Utils.Logger.Message
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="ICustomFormatter"/> which is used to format <see cref="LogMessage"/> objects.
|
||||
/// </summary>
|
||||
/// <remarks>This class implements a singleton pattern.</remarks>
|
||||
public class LogMessageFormatter : ICustomFormatter
|
||||
{
|
||||
|
||||
private LogMessageFormatter() { }
|
||||
|
||||
private static object Locker { get; } = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Accesses the global instance of the <see cref="LogMessageFormatter"/>.
|
||||
/// </summary>
|
||||
private static LogMessageFormatter FormatterInstance { get; set; }
|
||||
|
||||
public static string DefaultFormatString { get; set; } = $"[{CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern} - {CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern}] (LL): V";
|
||||
|
||||
/// <summary>
|
||||
/// The default format string which is used to format <see cref="LogMessage"/> objects, if no custom format string is provided.
|
||||
/// </summary>
|
||||
public static LogMessageFormatter Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (FormatterInstance != null)
|
||||
return FormatterInstance;
|
||||
|
||||
lock (Locker)
|
||||
{
|
||||
if (FormatterInstance != null)
|
||||
return FormatterInstance;
|
||||
|
||||
FormatterInstance = new LogMessageFormatter();
|
||||
}
|
||||
return FormatterInstance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats a <see cref="LogMessage"/> object.
|
||||
/// </summary>
|
||||
/// <param name="format">The format string. If <see cref="null"/>, <see cref="DefaultFormatString"/> will be used.</param>
|
||||
/// <param name="arg">The <see cref="LogMessage"/> object to be formatted.</param>
|
||||
/// <param name="formatProvider">Optional, an <see cref="IFormatProvider"/> interface to be used while formatting if needed.</param>
|
||||
/// <returns>A formatted <see cref="string"/> of the <see cref="LogMessage"/>.</returns>
|
||||
/// <remarks>
|
||||
/// If <see cref="arg"/> is not a <see cref="LogMessage"/> object, it will either be returned the formatted string implemented by the type of <see cref="arg"/>, if <see cref="IFormattable"/> is implemented by it, or the <see cref="arg.ToString()"/> result."
|
||||
/// </remarks>
|
||||
public string Format(string format, object arg, IFormatProvider formatProvider)
|
||||
{
|
||||
if (format == null)
|
||||
format = DefaultFormatString;
|
||||
|
||||
if (arg == null)
|
||||
throw new ArgumentNullException(nameof(arg));
|
||||
|
||||
if (arg.GetType() != typeof(LogMessage))
|
||||
{
|
||||
var formattable = arg as IFormattable;
|
||||
return formattable?.ToString(format, formatProvider) ?? arg.ToString();
|
||||
}
|
||||
|
||||
var message = (LogMessage)arg;
|
||||
|
||||
var result = message.LogDateTime.ToString(format, formatProvider);
|
||||
|
||||
result = result.Replace("LL", message.LogLevel.ToString());
|
||||
result = result.Replace("V", message.Message);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SweetLib.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// A generic class containing useful methods.
|
||||
/// </summary>
|
||||
public static class SweetUtils
|
||||
{
|
||||
public static char DefaultFileNameReplaceChar { get; set; } = '-';
|
||||
|
||||
/// <summary>
|
||||
/// Legalizes a file name with the <see cref="DefaultFileNameReplaceChar"/> character.
|
||||
/// </summary>
|
||||
/// <param name="fileName">File name to legalize.</param>
|
||||
/// <returns>Legalized file name.</returns>
|
||||
public static string LegalizeFilename(string fileName)
|
||||
{
|
||||
return LegalizeFilename(fileName, '-');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Legalizes a file name by a given replace character.
|
||||
/// </summary>
|
||||
/// <param name="fileName">File name to legalize.</param>
|
||||
/// <param name="replaceChar">Character to be used as replace character.</param>
|
||||
/// <returns>Legalized file name.</returns>
|
||||
public static string LegalizeFilename(string fileName, char replaceChar)
|
||||
{
|
||||
var invalidChars = System.IO.Path.GetInvalidFileNameChars();
|
||||
|
||||
if (invalidChars.Contains(replaceChar))
|
||||
throw new IOException($"Replace character {replaceChar} is an invalid file name character.");
|
||||
|
||||
return invalidChars.Aggregate(fileName, (current, c) => current.Replace(c, replaceChar));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> into an Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="date"><see cref="DateTime"/> to convert into Unix timestamp.</param>
|
||||
/// <returns>Converted Unix timestamp.</returns>
|
||||
public static double DateTimeToUnixTimeStamp(DateTime date)
|
||||
{
|
||||
return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Unix timestamp into a <see cref="DateTime"/>.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">Unix timestamp to convert.</param>
|
||||
/// <returns>Converted <see cref="DateTime"/>.</returns>
|
||||
public static DateTime UnixTimestampToDateTime(double timestamp)
|
||||
{
|
||||
return new DateTime(1970, 1, 1).AddMilliseconds(timestamp);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SweetLib.Utils.TaskBar
|
||||
{
|
||||
public static class TaskBarProgress
|
||||
{
|
||||
public enum TaskbarStates
|
||||
{
|
||||
NoProgress = 0,
|
||||
Indeterminate = 0x1,
|
||||
Normal = 0x2,
|
||||
Error = 0x4,
|
||||
Paused = 0x8
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("26C41017-74B8-4C70-88AA-61B11D8C0D5A")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
private interface ITaskbarList3
|
||||
{
|
||||
// ITaskbarList
|
||||
[PreserveSig]
|
||||
void HrInit();
|
||||
[PreserveSig]
|
||||
void AddTab(IntPtr hwnd);
|
||||
[PreserveSig]
|
||||
void DeleteTab(IntPtr hwnd);
|
||||
[PreserveSig]
|
||||
void ActivateTab(IntPtr hwnd);
|
||||
[PreserveSig]
|
||||
void SetActiveAlt(IntPtr hwnd);
|
||||
|
||||
// ITaskbarList2
|
||||
[PreserveSig]
|
||||
void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
|
||||
|
||||
// ITaskbarList3
|
||||
[PreserveSig]
|
||||
void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
|
||||
[PreserveSig]
|
||||
void SetProgressState(IntPtr hwnd, TaskbarStates state);
|
||||
}
|
||||
|
||||
[Guid("D6031210-4108-4E8A-B740-A627052FDAC2")]
|
||||
[ClassInterface(ClassInterfaceType.None)]
|
||||
[ComImport]
|
||||
private class TaskbarInstance
|
||||
{
|
||||
}
|
||||
|
||||
private static ITaskbarList3 taskbarInstance = (ITaskbarList3)new TaskbarInstance();
|
||||
private static bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);
|
||||
|
||||
public static void SetState(IntPtr windowHandle, TaskbarStates taskbarState)
|
||||
{
|
||||
if (taskbarSupported) taskbarInstance.SetProgressState(windowHandle, taskbarState);
|
||||
}
|
||||
|
||||
public static void SetValue(IntPtr windowHandle, double progressValue, double progressMax)
|
||||
{
|
||||
if (taskbarSupported) taskbarInstance.SetProgressValue(windowHandle, (ulong)progressValue, (ulong)progressMax);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
nuget pack SweetLib.csproj -properties Configuration=Release -symbols
|
||||
pause
|
|
@ -7,9 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib.Demo", "SweetLib.D
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib.Demo.Console", "SweetLib.Demo.Console\SweetLib.Demo.Console.csproj", "{C8681E08-6F23-45E1-A16F-BBA72003219B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib.Old", "SweetLib.Old\SweetLib.Old.csproj", "{02C1F8EF-32F2-4E77-A36D-79129402AF37}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib", "SweetLib\SweetLib.csproj", "{7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SweetLib", "SweetLib\SweetLib.csproj", "{7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -25,10 +23,6 @@ Global
|
|||
{C8681E08-6F23-45E1-A16F-BBA72003219B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C8681E08-6F23-45E1-A16F-BBA72003219B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C8681E08-6F23-45E1-A16F-BBA72003219B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{02C1F8EF-32F2-4E77-A36D-79129402AF37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{02C1F8EF-32F2-4E77-A36D-79129402AF37}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{02C1F8EF-32F2-4E77-A36D-79129402AF37}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{02C1F8EF-32F2-4E77-A36D-79129402AF37}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
|
|
Loading…
Reference in a new issue