2017-11-18 17:27:06 +01:00
|
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
|
|
|
|
|
//
|
2018-07-27 04:52:14 +02:00
|
|
|
|
// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki
|
|
|
|
|
|
// Contact: JustArchi@JustArchi.net
|
2017-11-18 17:27:06 +01:00
|
|
|
|
//
|
2018-07-27 04:52:14 +02:00
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
|
// You may obtain a copy of the License at
|
2017-11-18 17:27:06 +01:00
|
|
|
|
//
|
2018-07-27 04:52:14 +02:00
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
//
|
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
|
// limitations under the License.
|
2015-10-28 19:21:27 +01:00
|
|
|
|
|
2016-07-04 21:58:50 +02:00
|
|
|
|
using System.Linq;
|
2018-09-08 01:03:55 +02:00
|
|
|
|
using ArchiSteamFarm.Collections;
|
2018-09-08 00:05:23 +02:00
|
|
|
|
using ArchiSteamFarm.IPC;
|
2016-07-04 19:22:46 +02:00
|
|
|
|
using NLog;
|
|
|
|
|
|
using NLog.Config;
|
|
|
|
|
|
using NLog.Targets;
|
2015-10-25 06:16:50 +01:00
|
|
|
|
|
2018-09-08 01:03:55 +02:00
|
|
|
|
namespace ArchiSteamFarm.NLog {
|
2016-06-10 00:49:44 +02:00
|
|
|
|
internal static class Logging {
|
2016-11-24 07:32:16 +01:00
|
|
|
|
private const string GeneralLayout = @"${date:format=yyyy-MM-dd HH\:mm\:ss}|${processname}-${processid}|${level:uppercase=true}|" + LayoutMessage;
|
|
|
|
|
|
private const string LayoutMessage = @"${logger}|${message}${onexception:inner= ${exception:format=toString,Data}}";
|
2016-06-10 00:49:44 +02:00
|
|
|
|
|
2016-07-07 23:27:34 +02:00
|
|
|
|
private static readonly ConcurrentHashSet<LoggingRule> ConsoleLoggingRules = new ConcurrentHashSet<LoggingRule>();
|
2016-06-10 00:49:44 +02:00
|
|
|
|
|
2017-06-26 01:37:14 +02:00
|
|
|
|
private static bool IsUsingCustomConfiguration;
|
2016-08-06 22:16:46 +02:00
|
|
|
|
private static bool IsWaitingForUserInput;
|
2016-07-04 22:30:29 +02:00
|
|
|
|
|
2017-06-26 01:39:22 +02:00
|
|
|
|
internal static void EnableTraceLogging() {
|
2017-06-26 01:37:14 +02:00
|
|
|
|
if (IsUsingCustomConfiguration || (LogManager.Configuration == null)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool reload = false;
|
|
|
|
|
|
foreach (LoggingRule rule in LogManager.Configuration.LoggingRules.Where(rule => rule.IsLoggingEnabledForLevel(LogLevel.Debug) && !rule.IsLoggingEnabledForLevel(LogLevel.Trace))) {
|
|
|
|
|
|
rule.EnableLoggingForLevel(LogLevel.Trace);
|
|
|
|
|
|
reload = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (reload) {
|
|
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-31 02:42:23 +01:00
|
|
|
|
internal static void InitCoreLoggers() {
|
2016-07-04 21:40:09 +02:00
|
|
|
|
if (LogManager.Configuration != null) {
|
2017-06-26 01:37:14 +02:00
|
|
|
|
IsUsingCustomConfiguration = true;
|
2016-07-04 22:03:12 +02:00
|
|
|
|
InitConsoleLoggers();
|
2016-07-07 23:27:34 +02:00
|
|
|
|
LogManager.ConfigurationChanged += OnConfigurationChanged;
|
2016-07-04 21:40:09 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-17 09:54:22 +02:00
|
|
|
|
ConfigurationItemFactory.Default.ParseMessageTemplates = false;
|
2016-07-04 19:22:46 +02:00
|
|
|
|
LoggingConfiguration config = new LoggingConfiguration();
|
2016-06-10 00:49:44 +02:00
|
|
|
|
|
2018-05-17 09:54:22 +02:00
|
|
|
|
ColoredConsoleTarget coloredConsoleTarget = new ColoredConsoleTarget("ColoredConsole") { Layout = GeneralLayout };
|
2016-07-04 22:30:29 +02:00
|
|
|
|
|
2016-09-21 19:26:20 +02:00
|
|
|
|
config.AddTarget(coloredConsoleTarget);
|
2016-11-06 12:19:43 +01:00
|
|
|
|
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, coloredConsoleTarget));
|
2016-07-04 22:30:29 +02:00
|
|
|
|
|
2017-06-26 08:42:00 +02:00
|
|
|
|
FileTarget fileTarget = new FileTarget("File") {
|
|
|
|
|
|
DeleteOldFileOnStartup = true,
|
|
|
|
|
|
FileName = SharedInfo.LogFile,
|
|
|
|
|
|
Layout = GeneralLayout
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
config.AddTarget(fileTarget);
|
|
|
|
|
|
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, fileTarget));
|
2016-07-04 19:22:46 +02:00
|
|
|
|
|
2016-08-06 22:16:46 +02:00
|
|
|
|
LogManager.Configuration = config;
|
|
|
|
|
|
InitConsoleLoggers();
|
2016-06-10 00:49:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-31 02:42:23 +01:00
|
|
|
|
internal static void InitHistoryLogger() {
|
2018-07-24 20:37:14 +02:00
|
|
|
|
if (LogManager.Configuration == null) {
|
2018-01-31 02:42:23 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-24 20:43:00 +02:00
|
|
|
|
HistoryTarget historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
|
2018-07-24 20:37:14 +02:00
|
|
|
|
|
2018-07-24 20:43:00 +02:00
|
|
|
|
if ((historyTarget == null) && !IsUsingCustomConfiguration) {
|
2018-07-24 20:37:14 +02:00
|
|
|
|
// TODO: We could use some nice HTML layout for this
|
|
|
|
|
|
historyTarget = new HistoryTarget("History") {
|
|
|
|
|
|
Layout = GeneralLayout,
|
|
|
|
|
|
MaxCount = 20
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LogManager.Configuration.AddTarget(historyTarget);
|
|
|
|
|
|
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, historyTarget));
|
2018-01-31 02:42:23 +01:00
|
|
|
|
|
2018-07-24 20:37:14 +02:00
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
|
|
}
|
2018-01-31 02:42:23 +01:00
|
|
|
|
|
2018-09-08 00:05:23 +02:00
|
|
|
|
ArchiKestrel.OnNewHistoryTarget(historyTarget);
|
2018-01-31 02:42:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
internal static void OnUserInputEnd() {
|
|
|
|
|
|
IsWaitingForUserInput = false;
|
2016-07-07 23:36:35 +02:00
|
|
|
|
|
2016-07-04 21:58:50 +02:00
|
|
|
|
if (ConsoleLoggingRules.Count == 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-08 00:05:23 +02:00
|
|
|
|
bool reconfigure = false;
|
2018-05-17 09:54:22 +02:00
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
foreach (LoggingRule consoleLoggingRule in ConsoleLoggingRules.Where(consoleLoggingRule => !LogManager.Configuration.LoggingRules.Contains(consoleLoggingRule))) {
|
|
|
|
|
|
LogManager.Configuration.LoggingRules.Add(consoleLoggingRule);
|
2018-09-08 00:05:23 +02:00
|
|
|
|
reconfigure = true;
|
2016-07-04 21:58:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-08 00:05:23 +02:00
|
|
|
|
if (reconfigure) {
|
2018-05-17 09:54:22 +02:00
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
|
|
}
|
2016-07-04 21:58:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
internal static void OnUserInputStart() {
|
|
|
|
|
|
IsWaitingForUserInput = true;
|
2016-07-07 23:36:35 +02:00
|
|
|
|
|
2016-07-04 21:58:50 +02:00
|
|
|
|
if (ConsoleLoggingRules.Count == 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-08 00:05:23 +02:00
|
|
|
|
bool reconfigure = false;
|
2018-05-17 09:54:22 +02:00
|
|
|
|
|
2018-05-17 09:55:38 +02:00
|
|
|
|
foreach (LoggingRule consoleLoggingRule in ConsoleLoggingRules) {
|
|
|
|
|
|
if (LogManager.Configuration.LoggingRules.Remove(consoleLoggingRule)) {
|
2018-09-08 00:05:23 +02:00
|
|
|
|
reconfigure = true;
|
2018-05-17 09:54:22 +02:00
|
|
|
|
}
|
2016-07-04 21:58:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-08 00:05:23 +02:00
|
|
|
|
if (reconfigure) {
|
2018-05-17 09:54:22 +02:00
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
|
|
}
|
2016-07-04 21:58:50 +02:00
|
|
|
|
}
|
2016-07-04 19:22:46 +02:00
|
|
|
|
|
2016-07-04 22:03:12 +02:00
|
|
|
|
private static void InitConsoleLoggers() {
|
2016-07-08 06:48:38 +02:00
|
|
|
|
ConsoleLoggingRules.Clear();
|
2018-05-17 09:54:22 +02:00
|
|
|
|
|
2016-09-21 19:26:20 +02:00
|
|
|
|
foreach (LoggingRule loggingRule in LogManager.Configuration.LoggingRules.Where(loggingRule => loggingRule.Targets.Any(target => target is ColoredConsoleTarget || target is ConsoleTarget))) {
|
2016-07-04 22:03:12 +02:00
|
|
|
|
ConsoleLoggingRules.Add(loggingRule);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-07-07 23:27:34 +02:00
|
|
|
|
|
|
|
|
|
|
private static void OnConfigurationChanged(object sender, LoggingConfigurationChangedEventArgs e) {
|
|
|
|
|
|
if ((sender == null) || (e == null)) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
|
2016-07-07 23:27:34 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InitConsoleLoggers();
|
2016-07-07 23:36:35 +02:00
|
|
|
|
|
|
|
|
|
|
if (IsWaitingForUserInput) {
|
|
|
|
|
|
OnUserInputStart();
|
|
|
|
|
|
}
|
2018-01-31 02:42:23 +01:00
|
|
|
|
|
2018-07-24 20:43:00 +02:00
|
|
|
|
HistoryTarget historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
|
2018-09-08 00:05:23 +02:00
|
|
|
|
ArchiKestrel.OnNewHistoryTarget(historyTarget);
|
2016-07-07 23:27:34 +02:00
|
|
|
|
}
|
2016-06-10 00:49:44 +02:00
|
|
|
|
}
|
2018-09-08 00:05:23 +02:00
|
|
|
|
}
|