1 //
2 // Copyright (c) 2010-2024 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.Collections.Generic;
9 using System.Collections.ObjectModel;
10 
11 namespace Antmicro.Renode.Logging
12 {
13     public abstract class LoggerBackend : ILoggerBackend
14     {
15         public virtual bool IsControllable { get { return true; } }
16 
Log(LogEntry entry)17         public abstract void Log(LogEntry entry);
18 
Flush()19         public virtual void Flush()
20         {
21         }
22 
Dispose()23         public virtual void Dispose()
24         {
25         }
26 
SetLogLevel(LogLevel level, int sourceId = -1)27         public virtual void SetLogLevel(LogLevel level, int sourceId = -1)
28         {
29             Logger.SetLogLevel(this, level, sourceId);
30             if(sourceId != -1)
31             {
32                 if(level == null)
33                 {
34                     peripheralsWithDifferentLogging.Remove(sourceId);
35                 }
36                 else
37                 {
38                     peripheralsWithDifferentLogging[sourceId] = level;
39                 }
40             }
41             else
42             {
43                 logLevel = level;
44             }
45         }
46 
GetCustomLogLevel(int? id)47         public LogLevel GetCustomLogLevel(int? id)
48         {
49             LogLevel result = null;
50             if(id.HasValue)
51             {
52                 peripheralsWithDifferentLogging.TryGetValue(id.Value, out result);
53             }
54             return result;
55         }
56 
GetCustomLogLevels()57         public IDictionary<int, LogLevel> GetCustomLogLevels()
58         {
59             return new ReadOnlyDictionary<int, LogLevel>(peripheralsWithDifferentLogging);
60         }
61 
GetLogLevel()62         public LogLevel GetLogLevel()
63         {
64             return logLevel;
65         }
66 
Reset()67         public void Reset()
68         {
69             logLevel = Logger.DefaultLogLevel;
70             peripheralsWithDifferentLogging.Clear();
71         }
72 
ShouldBeLogged(LogEntry entry)73         protected bool ShouldBeLogged(LogEntry entry)
74         {
75             return entry.Type >= (GetCustomLogLevel(entry.SourceId) ?? logLevel);
76         }
77 
LoggerBackend()78         protected LoggerBackend()
79         {
80             peripheralsWithDifferentLogging = new Dictionary<int, LogLevel>();
81             logLevel = Logger.DefaultLogLevel;
82         }
83 
84         protected LogLevel logLevel;
85 
86         private readonly Dictionary<int, LogLevel> peripheralsWithDifferentLogging;
87     }
88 }
89 
90