Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

Commit f9a06e8

Browse files
committed
feat: add logging
1 parent 97fc96b commit f9a06e8

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
None
1111

12-
## 24.4.0
12+
## 24.5.0
13+
14+
## Added
15+
16+
- Logging
1317

1418
### Changed
1519

1620
- Remove FPS displaying
1721
- Fixed FPS to 20
22+
- Change read buffer size to 1048576
1823

1924
## 24.4.3
2025

Source/Logger.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.IO;
2+
3+
namespace EdcHost;
4+
5+
/// <summary>
6+
/// Logger class provides logging functionality.
7+
/// </summary>
8+
internal class Logger {
9+
private string _logFilePath;
10+
11+
public Logger(string logFilePath) {
12+
_logFilePath = logFilePath;
13+
14+
string dir = Path.GetDirectoryName(_logFilePath);
15+
if (!Directory.Exists(dir)) {
16+
Directory.CreateDirectory(dir);
17+
}
18+
}
19+
20+
public void Debug(string message) {
21+
using (StreamWriter sw = File.AppendText(_logFilePath)) {
22+
sw.WriteLine(message);
23+
}
24+
}
25+
}

Source/MainWindow.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4+
using System.IO;
45
using System.IO.Ports;
56
using System.Threading;
67
using System.Windows.Forms;
@@ -90,7 +91,7 @@ private static readonly (Mat Departure, Mat Destination) IconOrder = (
9091
/// <summary>
9192
/// The length of the buffer for serial ports.
9293
/// </summary>
93-
private const int SerialPortBufferLength = 1024;
94+
private const int SerialPortBufferLength = 1048576;
9495

9596
#endregion
9697

@@ -170,6 +171,7 @@ public Dictionary<CampType, SerialPort> SerialPortDict
170171
private CoordinateConverter _coordinateConverter;
171172
private Game _game = new Game();
172173
private Dictionary<CampType, Locator> _locatorDict = new Dictionary<CampType, Locator>();
174+
private Logger _logger = new(Path.Combine(Directory.GetCurrentDirectory(), "Logs", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log"));
173175
private Point2f[] _monitorCorners = new Point2f[4];
174176
private OpenCvSharp.Size _monitorFrameSize;
175177
private List<Order> _orderToTransmitList = new List<Order>();
@@ -378,6 +380,11 @@ private void Communicate()
378380
{
379381
foreach (var camp in MainWindow.AllCampList)
380382
{
383+
if (this._game.Camp != camp)
384+
{
385+
continue;
386+
}
387+
381388
if (
382389
this._serialPortDict[camp] == null ||
383390
!this._serialPortDict[camp].IsOpen
@@ -461,6 +468,7 @@ private void Communicate()
461468
ownChargingPiles: ownChargingPiles,
462469
opponentChargingPiles: opponentChargingPiles
463470
);
471+
_logger.Debug($"{DateTime.Now.ToString("HH:mm:ss")} [0x01 {camp}] {Convert.ToHexString(gameInfoPacket.GetBytes())}");
464472
var bytesToWrite = gameInfoPacket.GetBytes();
465473
try
466474
{
@@ -540,6 +548,7 @@ private void Communicate()
540548
orderInDeliveryList: orderInDeliveryList,
541549
latestPendingOrder: latestPendingOrder
542550
);
551+
_logger.Debug($"{DateTime.Now.ToString("HH:mm:ss")} [0x05 {camp}] {Convert.ToHexString(packet.GetBytes())}");
543552

544553
var bytesToWrite = packet.GetBytes();
545554

0 commit comments

Comments
 (0)