-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
101 lines (81 loc) · 2.58 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Prototype.Experiment;
namespace Prototype
{
class Logger
{
public enum EntryType
{
Init,
Search,
Click,
Scroll,
KeyPress,
NextQuestion
}
public struct LogEntry
{
public EntryType Type;
public DateTime Time;
public object Data;
public FocusArea Focus;
}
private List<LogEntry> _logEntries = new List<LogEntry>();
private DateTime _startTime = DateTime.Now;
StreamWriter _sw;
void Instance_OnTasksetChanged(TaskSet setName)
{
if (_sw != null)
_sw.Close();
_sw = File.CreateText(_startTime.ToString("yyyyMMddHHmm") + "." + ExperimentInfo.Instance.CurrentTaskSet.tasksfile + ".log");
string header =
ExperimentInfo.Instance.ParticipantID.ToString() + ";" +
ExperimentInfo.Instance.CurrentTaskSet + ";" +
setName.strategy.ToString();
_startTime = DateTime.Now;
_sw.WriteLine(header);
}
public void Log(EntryType type, object data)
{
if (_sw == null)
return;
LogEntry le = new LogEntry();
le.Data = data;
le.Time = DateTime.Now;
le.Type = type;
le.Focus = Model.Default.Focus;
_logEntries.Add(le);
WriteToLogFile(le);
}
private void WriteToLogFile(LogEntry le)
{
string line = (int)(le.Time - _startTime).TotalMilliseconds +
";" + le.Type.ToString() +
";" + le.Focus.Start.ToString() +
";" + le.Focus.End.ToString();
if (le.Type == EntryType.Search)
line += ";" + (string)le.Data;
_sw.WriteLine(line);
_sw.Flush();
}
#region Singleton
static private Logger logger = null;
protected Logger()
{
ExperimentInfo.Instance.OnTasksetChanged += new ExperimentInfo.TaskSetChangedDelegate(Instance_OnTasksetChanged);
}
public static Logger Default
{
get
{
if (logger == null)
logger = new Logger();
return logger;
}
}
#endregion
}
}