Skip to content

Commit cb6d23e

Browse files
committed
Logging refactor
1 parent 2223ec9 commit cb6d23e

10 files changed

+97
-77
lines changed

Phantasma.Core/Log/ConsoleLogger.cs

+14-21
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,25 @@ namespace Phantasma.Core.Log
44
{
55
public class ConsoleLogger : Logger
66
{
7-
private static object _lock = new object();
7+
public ConsoleLogger(LogLevel level) : base(level)
8+
{
89

9-
public static bool AppendTimestamp = false;
10+
}
1011

11-
public override void Write(LogEntryKind kind, string msg)
12+
protected override void Write(LogLevel kind, string msg)
1213
{
13-
if (AppendTimestamp)
14-
{
15-
msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + msg;
16-
}
17-
18-
lock (_lock)
14+
var color = Console.ForegroundColor;
15+
switch (kind)
1916
{
20-
var color = Console.ForegroundColor;
21-
switch (kind)
22-
{
23-
case LogEntryKind.Error: Console.ForegroundColor = ConsoleColor.Red; break;
24-
case LogEntryKind.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break;
25-
case LogEntryKind.Message: Console.ForegroundColor = ConsoleColor.Gray; break;
26-
case LogEntryKind.Success: Console.ForegroundColor = ConsoleColor.Green; break;
27-
case LogEntryKind.Debug: Console.ForegroundColor = ConsoleColor.Cyan; break;
28-
default: return;
29-
}
30-
Console.WriteLine(msg);
31-
Console.ForegroundColor = color;
17+
case LogLevel.Error: Console.ForegroundColor = ConsoleColor.Red; break;
18+
case LogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break;
19+
case LogLevel.Message: Console.ForegroundColor = ConsoleColor.Gray; break;
20+
case LogLevel.Success: Console.ForegroundColor = ConsoleColor.Green; break;
21+
case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Cyan; break;
22+
default: return;
3223
}
24+
Console.WriteLine(msg);
25+
Console.ForegroundColor = color;
3326
}
3427
}
3528
}

Phantasma.Core/Log/DebugLogger.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Phantasma.Core.Log
2+
{
3+
public class DebugLogger : Logger
4+
{
5+
public DebugLogger(LogLevel level = LogLevel.Maximum) : base(level)
6+
{
7+
8+
}
9+
10+
protected override void Write(LogLevel kind, string msg)
11+
{
12+
System.Diagnostics.Debug.WriteLine(msg);
13+
}
14+
}
15+
}

Phantasma.Core/Log/DummyLogger.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ public class DummyLogger : Logger
44
{
55
public static readonly DummyLogger Instance = new DummyLogger();
66

7-
public override void Write(LogEntryKind kind, string msg)
7+
public DummyLogger() : base(LogLevel.None)
8+
{
9+
10+
}
11+
12+
protected override void Write(LogLevel kind, string msg)
813
{
914
}
1015
}

Phantasma.Core/Log/FileLogger.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ namespace Phantasma.Core.Log
55
{
66
public class FileLogger : Logger
77
{
8-
private static object _lock = new object();
98
private static StreamWriter sw;
109

11-
public FileLogger(string file)
10+
public FileLogger(string file, LogLevel level) : base(level)
1211
{
1312
if (sw != null)
1413
{
@@ -23,13 +22,13 @@ public FileLogger(string file)
2322
sw.Close();
2423
}
2524

26-
public override void Write(LogEntryKind kind, string msg)
25+
protected override void Write(LogLevel kind, string msg)
2726
{
28-
lock (_lock)
29-
{
30-
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + msg);
31-
sw.Flush();
32-
}
27+
var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
28+
var output = $"{date} {kind} {msg}";
29+
30+
sw.WriteLine(output);
31+
sw.Flush();
3332
}
3433
}
3534
}

Phantasma.Core/Log/LogKind.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
/// <summary>
44
/// Represents the kind of the log message.
55
/// </summary>
6-
public enum LogEntryKind
6+
public enum LogLevel
77
{
8-
None,
9-
Message,
10-
Success,
11-
Warning,
12-
Error,
13-
Debug,
8+
None = 0,
9+
Success = 1,
10+
Error = 2,
11+
Warning = 3,
12+
Message = 4,
13+
Debug = 5,
14+
Maximum = 6
1415
}
16+
// Don't change the order here, its important, must be ordered by importance
1517
}

Phantasma.Core/Log/Logger.cs

+28-33
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,64 @@ namespace Phantasma.Core.Log
44
{
55
public abstract class Logger
66
{
7-
public LogEntryKind Level = LogEntryKind.Debug;
7+
public readonly LogLevel Level;
88

9-
public abstract void Write(LogEntryKind kind, string msg);
9+
private static object _lock = new object();
1010

11-
public void Message(string msg)
11+
public Logger(LogLevel level)
12+
{
13+
this.Level = level;
14+
}
15+
16+
public static Logger Init(Logger log)
1217
{
13-
if (this.Level < LogEntryKind.Message)
18+
if (log == null)
1419
{
15-
return;
20+
return DummyLogger.Instance;
1621
}
1722

18-
Write(LogEntryKind.Message, msg);
23+
return log;
1924
}
2025

21-
public void Debug(string msg)
26+
protected abstract void Write(LogLevel kind, string msg);
27+
28+
public void RouteMessage(LogLevel kind, string msg)
2229
{
23-
if (this.Level < LogEntryKind.Debug)
30+
if (this.Level < kind)
2431
{
2532
return;
2633
}
2734

28-
Write(LogEntryKind.Debug, msg);
35+
lock (_lock)
36+
{
37+
this.Write(kind, msg);
38+
}
2939
}
3040

31-
public static Logger Init(Logger log)
41+
public void Message(string msg)
3242
{
33-
if (log == null)
34-
{
35-
return DummyLogger.Instance;
36-
}
43+
RouteMessage(LogLevel.Message, msg);
44+
}
3745

38-
return log;
46+
public void Debug(string msg)
47+
{
48+
RouteMessage(LogLevel.Debug, msg);
3949
}
4050

4151
public void Warning(string msg)
4252
{
43-
if (this.Level < LogEntryKind.Warning)
44-
{
45-
return;
46-
}
47-
48-
Write(LogEntryKind.Warning, msg);
53+
RouteMessage(LogLevel.Warning, msg);
4954
}
5055

5156
public void Error(string msg)
5257
{
53-
if (this.Level < LogEntryKind.Error)
54-
{
55-
return;
56-
}
57-
58-
Write(LogEntryKind.Error, msg);
58+
RouteMessage(LogLevel.Error, msg);
5959
}
6060

6161

6262
public void Success(string msg)
6363
{
64-
if (this.Level < LogEntryKind.Success)
65-
{
66-
return;
67-
}
68-
69-
Write(LogEntryKind.Success, msg);
64+
RouteMessage(LogLevel.Success, msg);
7065
}
7166

7267
public void Exception(Exception ex)

Phantasma.Core/Utils/ArgumentParser.cs

+13
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ public bool GetBool(string key, bool defaultVal = false)
137137
return defaultVal;
138138
}
139139

140+
public T GetEnum<T>(string key, T defaultVal) where T : struct, IConvertible
141+
{
142+
if (!typeof(T).IsEnum)
143+
{
144+
throw new ArgumentException("T must be an enumerated type");
145+
}
146+
147+
var temp = GetString(key, defaultVal.ToString());
148+
T result;
149+
if (Enum.TryParse<T>(temp, out result)) { return result; }
150+
return defaultVal;
151+
}
152+
140153
public bool HasValue(string key)
141154
{
142155
return entries.ContainsKey(key);

Phantasma.Tests/AssemblerTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ private TestVM ExecuteScriptWithNexus(IEnumerable<string> scriptString, Action<T
27592759
var owner = PhantasmaKeys.Generate();
27602760
var script = AssemblerUtils.BuildScript(scriptString);
27612761

2762-
var nexus = new Nexus("asmnet", new ConsoleLogger());
2762+
var nexus = new Nexus("asmnet", new DebugLogger());
27632763
nexus.CreateGenesisBlock(owner, Timestamp.Now, 1);
27642764
var tx = new Transaction(nexus.Name, nexus.RootChain.Name, script, 0);
27652765

@@ -2793,7 +2793,7 @@ private TestVM ExecuteScriptIsolated(IEnumerable<string> scriptString, out Trans
27932793
var script = AssemblerUtils.BuildScript(scriptString);
27942794

27952795
var keys = PhantasmaKeys.Generate();
2796-
var nexus = new Nexus("asmnet", new ConsoleLogger());
2796+
var nexus = new Nexus("asmnet", new DebugLogger());
27972797
nexus.CreateGenesisBlock(owner, Timestamp.Now, 1);
27982798
tx = new Transaction(nexus.Name, nexus.RootChain.Name, script, 0);
27992799

Phantasma.Tests/DBStorageTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Text;
1111
using Phantasma.Storage.Context;
1212
using Phantasma.Numerics;
13+
using Phantasma.Core.Log;
1314

1415
namespace Phantasma.Tests
1516
{
@@ -24,7 +25,7 @@ public class DBStorageTests
2425
[TestInitialize()]
2526
public void TestInitialize()
2627
{
27-
_adapterFactory = _adapterFactory = (name) => { return new DBPartition(new ConsoleLogger(), path + name); };
28+
_adapterFactory = _adapterFactory = (name) => { return new DBPartition(new DebugLogger(), path + name); };
2829
_testStorage = new KeyValueStore<string, string>(CreateKeyStoreAdapterTest("test"));
2930
}
3031

Phantasma.Tests/NodeTests.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ private BigInteger GetBalance(Address address)
127127

128128
private void InitMainNode(int _port = 7077)
129129
{
130-
var log = new ConsoleLogger();
131-
132130
string wif = nexusWif;
133131

134132
int port = _port;
@@ -145,8 +143,7 @@ private void InitMainNode(int _port = 7077)
145143
mempool.Start();
146144

147145
// node setup
148-
node = new Node("test node", nexus, mempool, node_keys, port, PeerCaps.Mempool, Enumerable.Empty<String>(), log);
149-
log.Message("Phantasma Node address: " + node_keys.Address.Text);
146+
node = new Node("test node", nexus, mempool, node_keys, port, PeerCaps.Mempool, Enumerable.Empty<String>(), new DebugLogger());
150147
node.Start();
151148
}
152149

0 commit comments

Comments
 (0)