Misc
[ArchiSteamFarm.git] / ArchiSteamFarm / Logging.cs
blob413970784140201a922477527f9d1f847cfd4738
1 /*
2 _ _ _ ____ _ _____
3 / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
8 Copyright 2015-2016 Łukasz "JustArchi" Domeradzki
9 Contact: JustArchi@JustArchi.net
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
25 using System;
26 using System.Diagnostics;
27 using System.IO;
28 using System.Runtime.CompilerServices;
30 namespace ArchiSteamFarm {
31 internal static class Logging {
32 private static readonly object FileLock = new object();
34 private static bool LogToFile;
36 internal static void Init() {
37 LogToFile = Program.GlobalConfig.LogToFile;
39 if (LogToFile) {
40 lock (FileLock) {
41 if (!LogToFile) {
42 return;
45 try {
46 File.Delete(Program.LogFile);
47 } catch (Exception e) {
48 LogToFile = false;
49 LogGenericException(e);
55 internal static void LogGenericWTF(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
56 if (string.IsNullOrEmpty(message)) {
57 return;
60 Log("[!!] WTF: " + previousMethodName + "() <" + botName + "> " + message + ", WTF?");
63 internal static void LogGenericError(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
64 if (string.IsNullOrEmpty(message)) {
65 return;
68 Log("[!!] ERROR: " + previousMethodName + "() <" + botName + "> " + message);
71 internal static void LogGenericException(Exception exception, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
72 if (exception == null) {
73 return;
76 Log("[!] EXCEPTION: " + previousMethodName + "() <" + botName + "> " + exception.Message);
77 Log("[!] StackTrace:" + Environment.NewLine + exception.StackTrace);
79 if (exception.InnerException != null) {
80 LogGenericException(exception.InnerException, botName, previousMethodName);
84 internal static void LogGenericWarning(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
85 if (string.IsNullOrEmpty(message)) {
86 return;
89 Log("[!] WARNING: " + previousMethodName + "() <" + botName + "> " + message);
92 internal static void LogGenericInfo(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
93 if (string.IsNullOrEmpty(message)) {
94 return;
97 Log("[*] INFO: " + previousMethodName + "() <" + botName + "> " + message);
100 internal static void LogNullError(string nullObjectName, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
101 if (string.IsNullOrEmpty(nullObjectName)) {
102 return;
105 LogGenericError(nullObjectName + " is null!", botName, previousMethodName);
108 [Conditional("DEBUG")]
109 internal static void LogGenericDebug(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
110 if (string.IsNullOrEmpty(message)) {
111 return;
114 Log("[#] DEBUG: " + previousMethodName + "() <" + botName + "> " + message);
117 private static void Log(string message) {
118 if (string.IsNullOrEmpty(message)) {
119 return;
122 string loggedMessage = DateTime.Now + " " + message + Environment.NewLine;
124 // Write on console only when not awaiting response from user
125 if (!Program.ConsoleIsBusy) {
126 try {
127 Console.Write(loggedMessage);
128 } catch { }
131 if (LogToFile) {
132 lock (FileLock) {
133 if (!LogToFile) {
134 return;
137 try {
138 File.AppendAllText(Program.LogFile, loggedMessage);
139 } catch (Exception e) {
140 LogToFile = false;
141 LogGenericException(e);