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.
26 using System
.Diagnostics
;
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
;
46 File
.Delete(Program
.LogFile
);
47 } catch (Exception e
) {
49 LogGenericException(e
);
55 internal static void LogGenericWTF(string message
, string botName
= "Main", [CallerMemberName
] string previousMethodName
= null) {
56 if (string.IsNullOrEmpty(message
)) {
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
)) {
68 Log("[!!] ERROR: " + previousMethodName
+ "() <" + botName
+ "> " + message
);
71 internal static void LogGenericException(Exception exception
, string botName
= "Main", [CallerMemberName
] string previousMethodName
= null) {
72 if (exception
== null) {
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
)) {
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
)) {
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
)) {
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
)) {
114 Log("[#] DEBUG: " + previousMethodName
+ "() <" + botName
+ "> " + message
);
117 private static void Log(string message
) {
118 if (string.IsNullOrEmpty(message
)) {
122 string loggedMessage
= DateTime
.Now
+ " " + message
+ Environment
.NewLine
;
124 // Write on console only when not awaiting response from user
125 if (!Program
.ConsoleIsBusy
) {
127 Console
.Write(loggedMessage
);
138 File
.AppendAllText(Program
.LogFile
, loggedMessage
);
139 } catch (Exception e
) {
141 LogGenericException(e
);