1 //
2 // Copyright (c) 2010-2025 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Antmicro.Migrant;
10 using Antmicro.Renode.Core;
11 
12 namespace Antmicro.Renode.Logging
13 {
14     public sealed class LogEntry : ISpeciallySerializable
15     {
LogEntry(DateTime time, LogLevel level, string message, int sourceId = NoSource, bool forceMachineName = false, int? threadId = null)16         public LogEntry(DateTime time, LogLevel level, string message, int sourceId = NoSource, bool forceMachineName = false, int? threadId = null)
17         {
18             Message = message;
19             numericLogLevel = level.NumericLevel;
20             SourceId = sourceId;
21             Time = time;
22             ThreadId = threadId;
23             ForceMachineName = forceMachineName;
24             Count = 1;
25             GetNames();
26         }
27 
EqualsWithoutIdTimeAndCount(LogEntry entry)28         public bool EqualsWithoutIdTimeAndCount(LogEntry entry)
29         {
30             return entry != null &&
31                 numericLogLevel == entry.numericLogLevel &&
32                 ThreadId == entry.ThreadId &&
33                 SourceId == entry.SourceId &&
34                 Message == entry.Message;
35         }
36 
Equals(object obj)37         public override bool Equals(object obj)
38         {
39             var leobj = obj as LogEntry;
40             if(leobj != null)
41             {
42                 return Id == leobj.Id;
43             }
44             return false;
45         }
46 
GetHashCode()47         public override int GetHashCode()
48         {
49             return (int)Id;
50         }
51 
Load(PrimitiveReader reader)52         public void Load(PrimitiveReader reader)
53         {
54             Id = reader.ReadUInt64();
55             Message = reader.ReadString();
56             SourceId = reader.ReadInt32();
57             ThreadId = reader.ReadInt32();
58             Time = new DateTime(reader.ReadInt64());
59             numericLogLevel = reader.ReadInt32();
60             Count = reader.ReadInt32();
61             GetNames();
62 
63             if(ThreadId == -1)
64             {
65                 ThreadId = null;
66             }
67         }
68 
Save(PrimitiveWriter writer)69         public void Save(PrimitiveWriter writer)
70         {
71             writer.Write(Id);
72             writer.Write(Message);
73             writer.Write(SourceId);
74             writer.Write(ThreadId ?? -1);
75             writer.Write(Time.Ticks);
76             writer.Write(numericLogLevel);
77             writer.Write(Count);
78         }
79 
80         public ulong Id { get; set; }
81         public int SourceId { get; private set; }
82         public string Message { get; private set; }
83         public int? ThreadId { get; private set; }
84         public DateTime Time { get; private set; }
85         public int Count { get; set; }
86         public LogLevel Type
87         {
88             get
89             {
90                 return (LogLevel)numericLogLevel;
91             }
92         }
93         public string FullMessage
94         {
95             get
96             {
97                 if(fullMessage == null)
98                 {
99                     fullMessage = $"{ObjectName}: {Message}";
100                 }
101                 return fullMessage;
102             }
103         }
104 
105         public string ObjectName
106         {
107             get
108             {
109                 if(objectName != null && objectName.StartsWith(string.Format("{0}.", Machine.SystemBusName)))
110                 {
111                     objectName = objectName.Substring(Machine.SystemBusName.Length + 1);
112                 }
113                 return objectName;
114             }
115         }
116 
117         public string MachineName
118         {
119             get
120             {
121                 return machineName;
122             }
123         }
124 
125         public bool ForceMachineName { get; }
126 
GetNames()127         private void GetNames()
128         {
129             if(SourceId != NoSource)
130             {
131                 EmulationManager.Instance.CurrentEmulation.CurrentLogger.TryGetName(SourceId, out objectName, out machineName);
132             }
133         }
134 
135         private int numericLogLevel;
136         private string objectName;
137         private string machineName;
138         private string fullMessage;
139 
140         private const int NoSource = -1;
141     }
142 }
143 
144