Skip to content

Commit 09737af

Browse files
committed
Logging is done
1 parent 626e553 commit 09737af

17 files changed

Lines changed: 154 additions & 3 deletions

File tree

Data/ILog.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Data
8+
{
9+
public interface ILog
10+
{
11+
string GetData();
12+
void ClearLog();
13+
void WriteLog(DateTime dateTime);
14+
}
15+
}

Data/Log.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Data
9+
{
10+
public class Log : ILog
11+
{
12+
readonly static string _filePath = "..\\..\\..\\log.txt";
13+
TextWriter _writer = new StreamWriter(_filePath, true);
14+
ArrayList _list = new ArrayList();
15+
DateTime _time;
16+
static IBallsManager _ballsManager;
17+
18+
public Log(IBallsManager ballsManager)
19+
{
20+
_ballsManager = ballsManager;
21+
}
22+
23+
public DateTime Time {
24+
get => _time; set => _time = value;
25+
}
26+
27+
public string GetData()
28+
{
29+
string data;
30+
31+
data = Time.ToString() + '\n';
32+
if (_ballsManager != null)
33+
{
34+
for (int i = 0; i < _ballsManager.GetBallsCount(); i++)
35+
{
36+
data += i + ": " + _ballsManager.GetBall(i).X + " " + _ballsManager.GetBall(i).Y + '\n';
37+
}
38+
39+
}
40+
41+
data += "\n\n";
42+
43+
return data;
44+
}
45+
46+
47+
public void ClearLog()
48+
{
49+
_writer.Close();
50+
lock(this)
51+
{
52+
File.WriteAllText(_filePath, String.Empty);
53+
}
54+
_writer = new StreamWriter(_filePath, true);
55+
}
56+
57+
public void WriteLog(DateTime dateTime)
58+
{
59+
_time = dateTime;
60+
if (Monitor.TryEnter(this))
61+
{
62+
for (int i = 0; i < _list.Count; i++)
63+
{
64+
_writer.Write(_list[i]);
65+
}
66+
_list.Clear();
67+
_writer.Write(this.GetData());
68+
_writer.Flush();
69+
Monitor.Exit(this);
70+
} else
71+
{
72+
_list.Add(GetData());
73+
}
74+
}
75+
76+
}
77+
}

Data/bin/Debug/net6.0/Data.dll

1.5 KB
Binary file not shown.

Data/bin/Debug/net6.0/Data.pdb

748 Bytes
Binary file not shown.

Logic/bin/Debug/net6.0/Data.dll

1.5 KB
Binary file not shown.

Logic/bin/Debug/net6.0/Data.pdb

748 Bytes
Binary file not shown.

Logic/bin/Debug/net6.0/Logic.dll

0 Bytes
Binary file not shown.

Logic/bin/Debug/net6.0/Logic.pdb

16 Bytes
Binary file not shown.

Presentation/View/MainWindow.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public partial class MainWindow : Window
2929
ILogic _logic;
3030
IModel _model;
3131

32+
ILog _log;
33+
3234
public MainWindow()
3335
{
3436
//InitializeComponent();
@@ -37,8 +39,10 @@ public MainWindow()
3739
_logic = new Logic.Logic(_ballsManager);
3840
_model = new Model.Model(_logic);
3941

42+
_log = new Log(_ballsManager);
43+
4044

41-
DataContext = new MainViewModel(_model);
45+
DataContext = new MainViewModel(_model, _log);
4246
}
4347

4448
private void Close_Window(object sender, RoutedEventArgs e)

Presentation/ViewModel/MainViewModel.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Presentation.Commands;
1+
using Data;
2+
using Presentation.Commands;
23
using Presentation.Model;
34
using System;
45
using System.Collections.Generic;
@@ -24,17 +25,24 @@ public class MainViewModel : INotifyPropertyChanged
2425
private int _tempBallRadius = 25;
2526
private Canvas _canvas;
2627
private IModel _model;
28+
private ILog _log;
29+
private DateTime _dateTime;
2730

2831

2932
//readonly int _radius = 25; // TODO draw from the range e.g. 15 - 40
3033

3134
private DispatcherTimer timer = new DispatcherTimer(); // allows you to change some value at regular time intervals
3235
public event PropertyChangedEventHandler? PropertyChanged;
3336

37+
private DispatcherTimer logTimer = new DispatcherTimer();
3438

35-
public MainViewModel(IModel model)
39+
40+
public MainViewModel(IModel model, ILog log)
3641
{
3742
_model = model;
43+
_log = log;
44+
45+
_log.ClearLog();
3846

3947
BallsNumber = "0";
4048
BallsCounter = BallsNumber;
@@ -44,6 +52,11 @@ public MainViewModel(IModel model)
4452

4553
public ICommand GenerateBallsCommand { get; set; }
4654

55+
public DateTime DateTime
56+
{
57+
get => _dateTime; set => _dateTime = value;
58+
}
59+
4760
public void GenerateBalls(object obj)
4861
{
4962
_canvas = (obj as Canvas);
@@ -77,6 +90,7 @@ public void GenerateBalls(object obj)
7790
Canvas.SetTop(ball, _model.GetLocation(i).Y - _tempBallRadius);
7891
}
7992
MoveBall();
93+
Logging();
8094
}
8195

8296
public string BallsNumber
@@ -137,5 +151,19 @@ public void StopBalls()
137151
{
138152
_model.StopLogic();
139153
}
154+
155+
private void Logging()
156+
{
157+
logTimer.Tick += LogTimerEvent;
158+
logTimer.Interval = TimeSpan.FromMilliseconds(5000);
159+
160+
logTimer.Start();
161+
}
162+
163+
private void LogTimerEvent(object? sender, EventArgs e)
164+
{
165+
_dateTime = DateTime.Now;
166+
_log.WriteLog(_dateTime);
167+
}
140168
}
141169
}

0 commit comments

Comments
 (0)