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

Commit

Permalink
feat: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Apr 3, 2023
1 parent 97fc96b commit f9a06e8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

None

## 24.4.0
## 24.5.0

## Added

- Logging

### Changed

- Remove FPS displaying
- Fixed FPS to 20
- Change read buffer size to 1048576

## 24.4.3

Expand Down
25 changes: 25 additions & 0 deletions Source/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.IO;

namespace EdcHost;

/// <summary>
/// Logger class provides logging functionality.
/// </summary>
internal class Logger {
private string _logFilePath;

public Logger(string logFilePath) {
_logFilePath = logFilePath;

string dir = Path.GetDirectoryName(_logFilePath);
if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}
}

public void Debug(string message) {
using (StreamWriter sw = File.AppendText(_logFilePath)) {
sw.WriteLine(message);
}
}
}
11 changes: 10 additions & 1 deletion Source/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
Expand Down Expand Up @@ -90,7 +91,7 @@ private static readonly (Mat Departure, Mat Destination) IconOrder = (
/// <summary>
/// The length of the buffer for serial ports.
/// </summary>
private const int SerialPortBufferLength = 1024;
private const int SerialPortBufferLength = 1048576;

#endregion

Expand Down Expand Up @@ -170,6 +171,7 @@ public Dictionary<CampType, SerialPort> SerialPortDict
private CoordinateConverter _coordinateConverter;
private Game _game = new Game();
private Dictionary<CampType, Locator> _locatorDict = new Dictionary<CampType, Locator>();
private Logger _logger = new(Path.Combine(Directory.GetCurrentDirectory(), "Logs", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log"));
private Point2f[] _monitorCorners = new Point2f[4];
private OpenCvSharp.Size _monitorFrameSize;
private List<Order> _orderToTransmitList = new List<Order>();
Expand Down Expand Up @@ -378,6 +380,11 @@ private void Communicate()
{
foreach (var camp in MainWindow.AllCampList)
{
if (this._game.Camp != camp)
{
continue;
}

if (
this._serialPortDict[camp] == null ||
!this._serialPortDict[camp].IsOpen
Expand Down Expand Up @@ -461,6 +468,7 @@ private void Communicate()
ownChargingPiles: ownChargingPiles,
opponentChargingPiles: opponentChargingPiles
);
_logger.Debug($"{DateTime.Now.ToString("HH:mm:ss")} [0x01 {camp}] {Convert.ToHexString(gameInfoPacket.GetBytes())}");
var bytesToWrite = gameInfoPacket.GetBytes();
try
{
Expand Down Expand Up @@ -540,6 +548,7 @@ private void Communicate()
orderInDeliveryList: orderInDeliveryList,
latestPendingOrder: latestPendingOrder
);
_logger.Debug($"{DateTime.Now.ToString("HH:mm:ss")} [0x05 {camp}] {Convert.ToHexString(packet.GetBytes())}");

var bytesToWrite = packet.GetBytes();

Expand Down

0 comments on commit f9a06e8

Please sign in to comment.