17 using System.Collections;
18 using System.Globalization;
19 using System.Runtime.CompilerServices;
21 using System.Text.RegularExpressions;
28 public static class Log
30 private static readonly Regex LeanPathRegex =
new Regex(
"(?:\\S*?\\\\pythonnet\\\\)|(?:\\S*?\\\\Lean\\\\)|(?:\\S*?/Lean/)|(?:\\S*?/pythonnet/)", RegexOptions.Compiled);
31 private static string _lastTraceText =
"";
32 private static string _lastErrorText =
"";
33 private static bool _debuggingEnabled;
34 private static int _level = 1;
42 get {
return _logHandler; }
43 set { _logHandler = value; }
51 get {
return _debuggingEnabled; }
52 set { _debuggingEnabled = value; }
59 public static string FilePath {
get;
set; } =
"log.txt";
66 get {
return _level; }
67 set { _level = value; }
75 public static void Error(
string error,
bool overrideMessageFloodProtection =
false)
79 if (error == _lastErrorText && !overrideMessageFloodProtection)
return;
80 _logHandler.
Error(error);
81 _lastErrorText = error;
85 Console.WriteLine(
"Log.Error(): Error writing error: " + err.Message);
96 private static void Error(
string method, Exception exception,
string message =
null,
bool overrideMessageFloodProtection =
false)
98 message = method +
"(): " + (message ??
string.Empty) +
" " +
ClearLeanPaths(exception?.ToString());
99 Error(message, overrideMessageFloodProtection);
108 [MethodImpl(MethodImplOptions.NoInlining)]
109 public static void Error(Exception exception,
string message =
null,
bool overrideMessageFloodProtection =
false)
117 public static void Trace(
string traceText,
bool overrideMessageFloodProtection =
false)
121 if (traceText == _lastTraceText && !overrideMessageFloodProtection)
return;
122 _logHandler.
Trace(traceText);
123 _lastTraceText = traceText;
125 catch (Exception err)
127 Console.WriteLine(
"Log.Trace(): Error writing trace: " +err.Message);
134 public static void Trace(
string format, params
object[] args)
136 Trace(
string.Format(CultureInfo.InvariantCulture, format, args));
142 public static void Error(
string format, params
object[] args)
144 Error(
string.Format(CultureInfo.InvariantCulture, format, args));
152 public static void Debug(
string text,
int level = 1)
156 if (!_debuggingEnabled || level < _level)
return;
157 _logHandler.
Debug(text);
159 catch (Exception err)
161 Console.WriteLine(
"Log.Debug(): Error writing debug: " + err.Message);
171 public static string VarDump(
object obj,
int recursion = 0)
173 var result =
new StringBuilder();
179 var t = obj.GetType();
182 var properties = t.GetProperties();
184 foreach (var property
in properties)
189 var value =
property.GetValue(obj,
null);
193 var indent = String.Empty;
199 indent =
new StringBuilder(trail).Insert(0, spaces, recursion - 1).ToString();
205 var displayValue = value.ToString();
206 if (value is
string) displayValue = String.Concat(
'"', displayValue,
'"');
209 result.AppendFormat(CultureInfo.InvariantCulture,
"{0}{1} = {2}\n", indent, property.Name, displayValue);
213 if (!(value is ICollection))
218 result.Append(
VarDump(value, recursion + 1));
225 var elementCount = 0;
226 foreach (var element
in ((ICollection)value))
228 var elementName = $
"{property.Name}[{elementCount}]";
229 indent =
new StringBuilder(trail).Insert(0, spaces, recursion).ToString();
232 result.AppendFormat(CultureInfo.InvariantCulture,
"{0}{1} = {2}\n", indent, elementName, element.ToString());
235 result.Append(
VarDump(element, recursion + 2));
239 result.Append(
VarDump(value, recursion + 1));
246 result.AppendFormat(CultureInfo.InvariantCulture,
"{0}{1} = {2}\n", indent, property.Name,
"null");
257 return result.ToString();
267 if (
string.IsNullOrEmpty(error))
271 return LeanPathRegex.Replace(error,
string.Empty);