1 |
b10bb263
|
silhavyj
|
using LDClient.utils.loggers;
|
2 |
d0cf9476
|
Pultak
|
using Microsoft.Extensions.Configuration;
|
3 |
|
|
|
4 |
74bd1e40
|
Pultak
|
namespace LDClient.utils {
|
5 |
f281acac
|
silhavyj
|
|
6 |
9657d7e0
|
silhavyj
|
/// <summary>
|
7 |
|
|
/// This class loads up the configuration file (appsettingss.json).
|
8 |
|
|
/// </summary>
|
9 |
74bd1e40
|
Pultak
|
internal class ConfigLoader {
|
10 |
b10bb263
|
silhavyj
|
|
11 |
9657d7e0
|
silhavyj
|
/// <summary>
|
12 |
|
|
/// Status code indicating a successful termination of an application.
|
13 |
|
|
/// </summary>
|
14 |
b10bb263
|
silhavyj
|
private const int ErrorExitCode = 1;
|
15 |
9657d7e0
|
silhavyj
|
|
16 |
|
|
/// <summary>
|
17 |
|
|
/// Path to the configuration file.
|
18 |
|
|
/// </summary>
|
19 |
b10bb263
|
silhavyj
|
private const string ConfigFile = "appsettings.json";
|
20 |
9657d7e0
|
silhavyj
|
|
21 |
|
|
/// <summary>
|
22 |
|
|
/// Name of the logging section defined in the config file.
|
23 |
|
|
/// </summary>
|
24 |
74bd1e40
|
Pultak
|
private const string LoggingSection = "Logging";
|
25 |
9657d7e0
|
silhavyj
|
|
26 |
|
|
/// <summary>
|
27 |
|
|
/// Name of the network section defined in the config file.
|
28 |
|
|
/// </summary>
|
29 |
58cf6a6f
|
Pultak
|
private const string NetworkSection = "Network";
|
30 |
9657d7e0
|
silhavyj
|
|
31 |
|
|
/// <summary>
|
32 |
|
|
/// Name of the cache section defined in the config file.
|
33 |
|
|
/// </summary>
|
34 |
c2cc7813
|
Pultak
|
private const string CacheSection = "Cache";
|
35 |
9657d7e0
|
silhavyj
|
|
36 |
|
|
/// <summary>
|
37 |
|
|
/// Name of the detection section defined in the config file.
|
38 |
|
|
/// </summary>
|
39 |
f281acac
|
silhavyj
|
private const string DdSection = "DebuggerDetection";
|
40 |
d0cf9476
|
Pultak
|
|
41 |
f09385a3
|
Pultak
|
|
42 |
|
|
public string[] T32ApiCommands { get; private set; } = null!;
|
43 |
|
|
public string T32ApiAddress { get; private set; } = null!;
|
44 |
|
|
public string T32ApiPort { get; private set; } = null!;
|
45 |
|
|
public string T32ApiPacketLen { get; private set; } = null!;
|
46 |
|
|
|
47 |
c2cc7813
|
Pultak
|
#region Logger
|
48 |
f09385a3
|
Pultak
|
|
49 |
9657d7e0
|
silhavyj
|
/// <summary>
|
50 |
|
|
/// Maximum size of the log file (it will start to rotate when this limit is reached).
|
51 |
|
|
/// </summary>
|
52 |
f281acac
|
silhavyj
|
public int LogChunkSize { get; private set; }
|
53 |
9657d7e0
|
silhavyj
|
|
54 |
|
|
/// <summary>
|
55 |
|
|
/// Number of files to be created until there will be zipped up.
|
56 |
|
|
/// </summary>
|
57 |
f281acac
|
silhavyj
|
public int LogChunkMaxCount { get; private set; }
|
58 |
9657d7e0
|
silhavyj
|
|
59 |
|
|
/// <summary>
|
60 |
|
|
/// Maximum number of zip files
|
61 |
|
|
/// </summary>
|
62 |
f281acac
|
silhavyj
|
public int LogArchiveMaxCount { get; private set; }
|
63 |
9657d7e0
|
silhavyj
|
|
64 |
|
|
/// <summary>
|
65 |
|
|
/// Time after which the last logs will be cleaned up.
|
66 |
|
|
/// </summary>
|
67 |
f281acac
|
silhavyj
|
public int LogCleanupPeriod { get; private set; }
|
68 |
9657d7e0
|
silhavyj
|
|
69 |
|
|
/// <summary>
|
70 |
|
|
/// Level of verbosity.
|
71 |
|
|
/// </summary>
|
72 |
f281acac
|
silhavyj
|
public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
|
73 |
9657d7e0
|
silhavyj
|
|
74 |
|
|
/// <summary>
|
75 |
|
|
/// Logger flow type.
|
76 |
|
|
/// </summary>
|
77 |
f281acac
|
silhavyj
|
public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
|
78 |
|
|
|
79 |
c2cc7813
|
Pultak
|
#endregion
|
80 |
d0cf9476
|
Pultak
|
|
81 |
c2cc7813
|
Pultak
|
#region Api
|
82 |
f281acac
|
silhavyj
|
|
83 |
9657d7e0
|
silhavyj
|
/// <summary>
|
84 |
|
|
/// URL to the API (it can be an IP address or a domain name if a DNS server is being used).
|
85 |
|
|
/// </summary>
|
86 |
b10bb263
|
silhavyj
|
public string ApiBaseAddress { get; private set; } = null!;
|
87 |
9657d7e0
|
silhavyj
|
|
88 |
|
|
/// <summary>
|
89 |
|
|
/// Path to the API (e.g. /api/v1/ld-logs).
|
90 |
|
|
/// </summary>
|
91 |
b10bb263
|
silhavyj
|
public string ApiUsbEndPoint { get; private set; } = null!;
|
92 |
9657d7e0
|
silhavyj
|
|
93 |
|
|
/// <summary>
|
94 |
|
|
/// Number of the port that the API runs on.
|
95 |
|
|
/// </summary>
|
96 |
f281acac
|
silhavyj
|
public uint ApiPort { get; private set; }
|
97 |
58cf6a6f
|
Pultak
|
|
98 |
c2cc7813
|
Pultak
|
#endregion
|
99 |
|
|
|
100 |
|
|
#region Cache
|
101 |
f281acac
|
silhavyj
|
|
102 |
9657d7e0
|
silhavyj
|
/// <summary>
|
103 |
|
|
/// Name of the cache (a directory of this name will be created).
|
104 |
|
|
/// </summary>
|
105 |
b10bb263
|
silhavyj
|
public string CacheFileName { get; private set; } = null!;
|
106 |
9657d7e0
|
silhavyj
|
|
107 |
|
|
/// <summary>
|
108 |
|
|
/// Maximum number of payloads (entries) to be sent to the server at a time.
|
109 |
|
|
/// </summary>
|
110 |
f281acac
|
silhavyj
|
public uint MaxRetries { get; private set; }
|
111 |
9657d7e0
|
silhavyj
|
|
112 |
|
|
/// <summary>
|
113 |
|
|
/// Maximum number of entries that can be stored in the database.
|
114 |
|
|
/// </summary>
|
115 |
f281acac
|
silhavyj
|
public uint MaxEntries { get; private set; }
|
116 |
9657d7e0
|
silhavyj
|
|
117 |
|
|
/// <summary>
|
118 |
|
|
/// Period (how often) a certain number of entries will be resent to the server.
|
119 |
|
|
/// </summary>
|
120 |
f281acac
|
silhavyj
|
public uint RetryPeriod { get; private set; }
|
121 |
|
|
|
122 |
c2cc7813
|
Pultak
|
#endregion
|
123 |
|
|
|
124 |
|
|
#region Detection
|
125 |
9657d7e0
|
silhavyj
|
|
126 |
|
|
/// <summary>
|
127 |
|
|
/// Name of the process to be detected (the application programmers use to connect to the debugger).
|
128 |
|
|
/// </summary>
|
129 |
b10bb263
|
silhavyj
|
public string T32ProcessName { get; private set; } = null!;
|
130 |
9657d7e0
|
silhavyj
|
|
131 |
|
|
/// <summary>
|
132 |
|
|
/// How often the application checks if there is the process (T32ProcessName) running on the PC.
|
133 |
|
|
/// </summary>
|
134 |
f281acac
|
silhavyj
|
public uint DetectionPeriod { get; private set; }
|
135 |
9657d7e0
|
silhavyj
|
|
136 |
|
|
/// <summary>
|
137 |
|
|
/// Location of the generated .txt file containing all information about a debugger.
|
138 |
|
|
/// </summary>
|
139 |
b10bb263
|
silhavyj
|
public string T32InfoLocation { get; private set; } = null!;
|
140 |
9657d7e0
|
silhavyj
|
|
141 |
|
|
/// <summary>
|
142 |
|
|
/// Path to the t32rem.exe which is used to send commands to a debugger.
|
143 |
|
|
/// </summary>
|
144 |
f09385a3
|
Pultak
|
public string T32RemExecutable { get; private set; } = null!;
|
145 |
9657d7e0
|
silhavyj
|
|
146 |
|
|
/// <summary>
|
147 |
|
|
/// How many times the application attempts to check if there
|
148 |
|
|
/// has been a .txt file generated containing all the desired information.
|
149 |
|
|
/// </summary>
|
150 |
6dab0250
|
silhavyj
|
public uint FetchInfoMaxAttempts { get; private set; }
|
151 |
9657d7e0
|
silhavyj
|
|
152 |
|
|
/// <summary>
|
153 |
|
|
/// Period in milliseconds after which the application tries to locate and parse the .txt file.
|
154 |
|
|
/// </summary>
|
155 |
6dab0250
|
silhavyj
|
public uint FetchInfoAttemptPeriod { get; private set; }
|
156 |
9657d7e0
|
silhavyj
|
|
157 |
|
|
/// <summary>
|
158 |
|
|
/// Arguments (commands) sent to the t32rem.exe file.
|
159 |
|
|
/// </summary>
|
160 |
f09385a3
|
Pultak
|
public string[] T32RemArguments { get; private set; } = null!;
|
161 |
9657d7e0
|
silhavyj
|
|
162 |
|
|
/// <summary>
|
163 |
|
|
/// Status code indication successful execution of the t32rem.exe file.
|
164 |
|
|
/// </summary>
|
165 |
4dcc6c07
|
silhavyj
|
public int T32RemSuccessExitCode { get; private set; }
|
166 |
9657d7e0
|
silhavyj
|
|
167 |
|
|
/// <summary>
|
168 |
|
|
/// Timeout of the execution of t32rem.exe (when sending one command).
|
169 |
|
|
/// </summary>
|
170 |
4dcc6c07
|
silhavyj
|
public int T32RemWaitTimeoutMs { get; private set; }
|
171 |
|
|
|
172 |
c2cc7813
|
Pultak
|
#endregion
|
173 |
58cf6a6f
|
Pultak
|
|
174 |
9657d7e0
|
silhavyj
|
/// <summary>
|
175 |
|
|
/// Creates an instance of the class.
|
176 |
|
|
/// </summary>
|
177 |
74bd1e40
|
Pultak
|
public ConfigLoader() {
|
178 |
9657d7e0
|
silhavyj
|
// Create a new config builder to read the configuration file.
|
179 |
d0cf9476
|
Pultak
|
var configuration = new ConfigurationBuilder()
|
180 |
b10bb263
|
silhavyj
|
.AddJsonFile(ConfigFile)
|
181 |
d0cf9476
|
Pultak
|
.Build();
|
182 |
|
|
|
183 |
9657d7e0
|
silhavyj
|
// Parse the logger section.
|
184 |
b10bb263
|
silhavyj
|
ReadLoggerSection(configuration);
|
185 |
9657d7e0
|
silhavyj
|
|
186 |
|
|
// Parse the api section.
|
187 |
b10bb263
|
silhavyj
|
ReadApiSection(configuration);
|
188 |
9657d7e0
|
silhavyj
|
|
189 |
|
|
// Parse the cache section.
|
190 |
b10bb263
|
silhavyj
|
ReadCacheSection(configuration);
|
191 |
9657d7e0
|
silhavyj
|
|
192 |
|
|
// Parse the detection section.
|
193 |
b10bb263
|
silhavyj
|
ReadDebuggerSection(configuration);
|
194 |
|
|
|
195 |
|
|
Console.WriteLine("Configuration successfully loaded!");
|
196 |
|
|
}
|
197 |
d0cf9476
|
Pultak
|
|
198 |
9657d7e0
|
silhavyj
|
/// <summary>
|
199 |
|
|
/// Parses the logging section of the configuration file.
|
200 |
|
|
/// </summary>
|
201 |
|
|
/// <param name="configuration">configuration</param>
|
202 |
b10bb263
|
silhavyj
|
private void ReadLoggerSection(IConfiguration configuration) {
|
203 |
d0cf9476
|
Pultak
|
try {
|
204 |
74bd1e40
|
Pultak
|
var logging = configuration.GetSection(LoggingSection);
|
205 |
|
|
LogChunkSize = int.Parse(logging["LogChunkSize"]);
|
206 |
|
|
LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
|
207 |
|
|
LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
|
208 |
|
|
LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
|
209 |
|
|
LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
|
210 |
|
|
LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
|
211 |
b10bb263
|
silhavyj
|
} catch (Exception e) {
|
212 |
|
|
Console.WriteLine(e.Message);
|
213 |
|
|
Environment.Exit(ErrorExitCode);
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
914776bd
|
Pultak
|
|
217 |
9657d7e0
|
silhavyj
|
/// <summary>
|
218 |
|
|
/// Parses the api section of the configuration file.
|
219 |
|
|
/// </summary>
|
220 |
|
|
/// <param name="configuration">configuration</param>
|
221 |
b10bb263
|
silhavyj
|
private void ReadApiSection(IConfiguration configuration) {
|
222 |
|
|
try {
|
223 |
58cf6a6f
|
Pultak
|
var network = configuration.GetSection(NetworkSection);
|
224 |
|
|
ApiBaseAddress = network["ApiBaseAddress"];
|
225 |
|
|
ApiUsbEndPoint = network["ApiLDEndPoint"];
|
226 |
|
|
ApiPort = uint.Parse(network["ApiPort"]);
|
227 |
b10bb263
|
silhavyj
|
} catch (Exception e) {
|
228 |
|
|
Console.WriteLine(e.Message);
|
229 |
|
|
Environment.Exit(ErrorExitCode);
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
|
233 |
9657d7e0
|
silhavyj
|
/// <summary>
|
234 |
|
|
/// Parses the cache section of the configuration file.
|
235 |
|
|
/// </summary>
|
236 |
|
|
/// <param name="configuration">configuration</param>
|
237 |
b10bb263
|
silhavyj
|
private void ReadCacheSection(IConfiguration configuration) {
|
238 |
|
|
try {
|
239 |
c2cc7813
|
Pultak
|
var cache = configuration.GetSection(CacheSection);
|
240 |
|
|
RetryPeriod = uint.Parse(cache["RetryPeriod"]);
|
241 |
|
|
MaxEntries = uint.Parse(cache["MaxEntries"]);
|
242 |
|
|
MaxRetries = uint.Parse(cache["MaxRetries"]);
|
243 |
|
|
CacheFileName = cache["CacheFileName"];
|
244 |
b10bb263
|
silhavyj
|
} catch (Exception e) {
|
245 |
|
|
Console.WriteLine(e.Message);
|
246 |
|
|
Environment.Exit(ErrorExitCode);
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
|
250 |
9657d7e0
|
silhavyj
|
/// <summary>
|
251 |
|
|
/// Parses the detection section of the configuration file.
|
252 |
|
|
/// </summary>
|
253 |
|
|
/// <param name="configuration">configuration</param>
|
254 |
b10bb263
|
silhavyj
|
private void ReadDebuggerSection(IConfiguration configuration) {
|
255 |
|
|
try {
|
256 |
f281acac
|
silhavyj
|
var debugger = configuration.GetSection(DdSection);
|
257 |
914776bd
|
Pultak
|
T32ProcessName = debugger["T32ProcessName"];
|
258 |
|
|
T32InfoLocation = debugger["T32InfoLocation"];
|
259 |
|
|
DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
|
260 |
f09385a3
|
Pultak
|
T32RemExecutable = debugger["T32RemExecutable"];
|
261 |
6dab0250
|
silhavyj
|
FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
|
262 |
|
|
FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
|
263 |
4dcc6c07
|
silhavyj
|
T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
|
264 |
|
|
T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
|
265 |
f09385a3
|
Pultak
|
T32RemArguments = configuration.GetSection($"{DdSection}:T32RemArguments").GetChildren().Select(key => key.Value).ToArray();
|
266 |
|
|
T32ApiCommands = configuration.GetSection($"{DdSection}:T32ApiCommands").GetChildren().Select(key => key.Value).ToArray();
|
267 |
|
|
T32ApiAddress = debugger["T32ApiAddress"];
|
268 |
|
|
T32ApiPort = debugger["T32ApiPort"];
|
269 |
|
|
T32ApiPacketLen = debugger["T32ApiPacketLen"];
|
270 |
|
|
|
271 |
b10bb263
|
silhavyj
|
} catch (Exception e) {
|
272 |
|
|
Console.WriteLine(e);
|
273 |
|
|
Environment.Exit(ErrorExitCode);
|
274 |
d0cf9476
|
Pultak
|
}
|
275 |
|
|
}
|
276 |
|
|
}
|
277 |
|
|
}
|