Skip to content

Commit 11d22aa

Browse files
committed
Add time format to options (UTC or local time)
1 parent 24d4144 commit 11d22aa

13 files changed

+100
-16
lines changed

Backend/Converter/ChainsawToLogConverter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ public IEnumerable<Log> Convert(string text) {
6767
attribute = attributes.GetNamedItem("timestamp");
6868
if (attribute != null) {
6969
var timespan = TimeSpan.FromMilliseconds(Int64.Parse(attribute.Value));
70-
// TODO: Find out why we have to do this here (maybe UTC conversion?)
71-
timespan.Add(TimeSpan.FromHours(2));
7270
log.Timestamp = new DateTime(1970, 1, 1).Add(timespan);
7371
}
7472
attribute = attributes.GetNamedItem("thread");
@@ -95,7 +93,7 @@ public IEnumerable<Log> Convert(string text) {
9593
var application = log.Properties.FirstOrDefault(m => m.Name == "log4japp");
9694
log.Application = application == null ? Constants.APPLICATION_GLOBAL : application.Value;
9795

98-
var context = log.Properties.Where(m => !m.Name.StartsWith("log4j")).Select(m => m.Name + ": " + m.Value);
96+
var context = log.Properties.Where(m => !m.Name.StartsWith("log4j")).OrderBy(m => m.Name).Select(m => m.Name + ": " + m.Value);
9997
log.Context = String.Join(", ", context);
10098
}
10199
}

Backend/Dao/ConfigurationDaoSettings.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace Backend.Manager {
1111
public class ConfigurationDaoSettings : IConfigurationDao {
1212

13+
public event EventHandler<EventArgs> OnConfigurationChanged;
14+
1315
public Configuration Read() {
1416
Configuration configuration = new Configuration();
1517

@@ -38,19 +40,44 @@ public Configuration Read() {
3840
}
3941
configuration.PortLogcat = Convert.ToInt32(portLogcat);
4042

43+
string logTimeFormat = Settings.Default["LogTimeFormat"] as string;
44+
if (String.IsNullOrEmpty(logTimeFormat)) {
45+
logTimeFormat = "convertToLocalTime";
46+
Settings.Default["LogTimeFormat"] = logTimeFormat;
47+
}
48+
if (logTimeFormat == "convertToLocalTime") {
49+
configuration.LogTimeFormat = LogTimeFormat.CONVERT_TO_LOCAL_TIME;
50+
} else if (logTimeFormat == "doNotChange") {
51+
configuration.LogTimeFormat = LogTimeFormat.DO_NOT_CHANGE;
52+
}
53+
4154
Settings.Default.Save();
4255

4356
return configuration;
4457
}
4558

4659
public void Write(Configuration configuration) {
60+
4761
if (configuration.LogType == LogType.CHAINSAW) {
4862
Settings.Default["Type"] = "chainsaw";
4963
} else if (configuration.LogType == LogType.LOGCAT) {
5064
Settings.Default["Type"] = "logcat";
5165
}
66+
5267
Settings.Default["PortChainsaw"] = configuration.PortChainsaw.ToString();
68+
5369
Settings.Default["PortLogcat"] = configuration.PortLogcat.ToString();
70+
71+
if (configuration.LogTimeFormat == LogTimeFormat.CONVERT_TO_LOCAL_TIME) {
72+
Settings.Default["LogTimeFormat"] = "convertToLocalTime";
73+
} else if (configuration.LogTimeFormat == LogTimeFormat.DO_NOT_CHANGE) {
74+
Settings.Default["LogTimeFormat"] = "doNotChange";
75+
}
76+
77+
if (OnConfigurationChanged != null) {
78+
OnConfigurationChanged(this, new EventArgs());
79+
}
80+
5481
Settings.Default.Save();
5582
}
5683
}

Backend/Dao/IConfigurationDao.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ namespace Backend.Dao {
99
public interface IConfigurationDao {
1010
Configuration Read();
1111
void Write(Configuration configuration);
12+
13+
event EventHandler<EventArgs> OnConfigurationChanged;
1214
}
1315
}

Backend/Model/Configuration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class Configuration {
1010
public LogType LogType { get; set; }
1111
public int PortChainsaw { get; set; }
1212
public int PortLogcat { get; set; }
13+
public LogTimeFormat LogTimeFormat { get; set; }
1314
}
1415
}

Backend/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Backend/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
<Setting Name="PortLogcat" Type="System.String" Scope="User">
1212
<Value Profile="(Default)" />
1313
</Setting>
14+
<Setting Name="LogTimeFormat" Type="System.String" Scope="User">
15+
<Value Profile="(Default)" />
16+
</Setting>
1417
</Settings>
1518
</SettingsFile>

Backend/app.config

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
<userSettings>
99
<Backend.Settings>
1010
<setting name="Type" serializeAs="String">
11-
<value/>
11+
<value />
1212
</setting>
1313
<setting name="PortChainsaw" serializeAs="String">
14-
<value/>
14+
<value />
1515
</setting>
1616
<setting name="PortLogcat" serializeAs="String">
17-
<value/>
17+
<value />
18+
</setting>
19+
<setting name="LogTimeFormat" serializeAs="String">
20+
<value />
1821
</setting>
1922
</Backend.Settings>
2023
</userSettings>

Common/Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<DependentUpon>Language.resx</DependentUpon>
6363
</Compile>
6464
<Compile Include="LinqQeueExtensions.cs" />
65+
<Compile Include="LogTimeFormat.cs" />
6566
<Compile Include="LogType.cs" />
6667
<Compile Include="Properties\AssemblyInfo.cs" />
6768
</ItemGroup>

Common/LogTimeFormat.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Common {
8+
public enum LogTimeFormat {
9+
NONE,
10+
DO_NOT_CHANGE,
11+
CONVERT_TO_LOCAL_TIME
12+
}
13+
}

Loginator/ConfigurationWindow.xaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
DataContext="{Binding ConfigurationViewModel, Source={StaticResource Locator}}"
66
ResizeMode="NoResize"
77
WindowStartupLocation="CenterScreen"
8-
Title="Configuration" Height="160" Width="260">
8+
Title="Configuration" Height="260" Width="260">
99
<Grid Margin="2" VerticalAlignment="Stretch">
1010
<StackPanel>
11-
<RadioButton Content="Chainsaw (NLog, log4net, log4j, etc.)" IsChecked="{Binding LogType, Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static common:LogType.CHAINSAW}}" Margin="4,4,0,0"/>
11+
<Label Content="Source" FontWeight="Bold"/>
12+
<RadioButton Content="Chainsaw (NLog, log4net, log4j, etc.)" GroupName="Source" IsChecked="{Binding LogType, Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static common:LogType.CHAINSAW}}" Margin="4,4,0,0"/>
1213
<StackPanel Orientation="Horizontal" Margin="32,4,0,0">
1314
<RadioButton Content="UDP" IsChecked="True" IsEnabled="False"/>
1415
<Label Content="Port"/>
1516
<TextBox Text="{Binding PortChainsaw}" ToolTip="Enter the port to listen to" Width="100"/>
1617
</StackPanel>
17-
<RadioButton Content="Logcat (Android)" IsChecked="{Binding LogType, Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static common:LogType.LOGCAT}}" Margin="4,4,0,0"/>
18+
<RadioButton Content="Logcat (Android)" GroupName="Source" IsChecked="{Binding LogType, Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static common:LogType.LOGCAT}}" Margin="4,4,0,0"/>
1819
<StackPanel Orientation="Horizontal" Margin="32,4,0,0">
1920
<RadioButton Content="UDP" IsChecked="True" IsEnabled="False"/>
2021
<Label Content="Port"/>
2122
<TextBox Text="{Binding PortLogcat}" ToolTip="Enter the port to listen to" Width="100"/>
2223
</StackPanel>
24+
<Label Content="Time format" FontWeight="Bold"/>
25+
<RadioButton Content="Convert to local time" GroupName="TimeFormat" IsChecked="{Binding LogTimeFormat, Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static common:LogTimeFormat.CONVERT_TO_LOCAL_TIME}}" Margin="32,4,0,0"/>
26+
<RadioButton Content="Do not change" GroupName="TimeFormat" IsChecked="{Binding LogTimeFormat, Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static common:LogTimeFormat.DO_NOT_CHANGE}}" Margin="32,4,0,0"/>
2327
</StackPanel>
2428
<StackPanel VerticalAlignment="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
2529
<Button Content="Cancel" Command="{Binding CancelChangesCommand}" Margin="0,0,4,0" Width="80"/>

Loginator/MainWindow.xaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
<RowDefinition Height="4" />
4646
<RowDefinition Height="150" />
4747
</Grid.RowDefinitions>
48-
<!--controls:GridViewColumnResize.Enabled="True"-->
49-
<!--controls:GridViewColumnResize.Width="130"-->
50-
<!--controls:GridViewColumnResize.Width="*"-->
5148
<ListView Grid.Row="0"
5249
SelectedItem="{Binding SelectedLog}"
5350
ItemsSource="{Binding Logs}"
@@ -60,7 +57,7 @@
6057
<GridViewColumn Header="Timestamp" controls:GridViewColumnResize.Width="140">
6158
<GridViewColumn.CellTemplate>
6259
<DataTemplate>
63-
<TextBlock Text="{Binding Timestamp, StringFormat='{}{0:dd.MM.yyyy hh:mm:ss.fff}'}" Foreground="{Binding Level, Converter={StaticResource LevelToForegroundConverter}}" />
60+
<TextBlock Text="{Binding Timestamp, StringFormat='{}{0:dd.MM.yyyy HH:mm:ss.fff}'}" Foreground="{Binding Level, Converter={StaticResource LevelToForegroundConverter}}" />
6461
</DataTemplate>
6562
</GridViewColumn.CellTemplate>
6663
</GridViewColumn>

Loginator/ViewModels/ConfigurationViewModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public string PortLogcat {
4949
}
5050
}
5151

52+
private LogTimeFormat logTimeFormat;
53+
public LogTimeFormat LogTimeFormat {
54+
get { return logTimeFormat; }
55+
set {
56+
logTimeFormat = value;
57+
OnPropertyChanged(nameof(LogTimeFormat));
58+
}
59+
}
60+
5261
public Action CloseAction { get; set; }
5362

5463
public ConfigurationViewModel(IConfigurationDao configurationDao) {
@@ -57,6 +66,7 @@ public ConfigurationViewModel(IConfigurationDao configurationDao) {
5766
LogType = configuration.LogType;
5867
PortChainsaw = configuration.PortChainsaw.ToString();
5968
PortLogcat = configuration.PortLogcat.ToString();
69+
LogTimeFormat = configuration.LogTimeFormat;
6070
}
6171

6272
private ICommand cancelChangesCommand;
@@ -90,7 +100,8 @@ private void AcceptChanges(ConfigurationViewModel serverRule) {
90100
Configuration configuration = new Configuration() {
91101
LogType = LogType,
92102
PortChainsaw = Convert.ToInt32(PortChainsaw),
93-
PortLogcat = Convert.ToInt32(PortLogcat)
103+
PortLogcat = Convert.ToInt32(PortLogcat),
104+
LogTimeFormat = LogTimeFormat
94105
};
95106
ConfigurationDao.Write(configuration);
96107
IoC.Get<Receiver>().Initialize(configuration);

Loginator/ViewModels/LoginatorViewModel.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public class LoginatorViewModel : INotifyPropertyChanged {
3333
internal IApplicationConfiguration ApplicationConfiguration { get; set; }
3434

3535
private const int TIME_INTERVAL_IN_MILLISECONDS = 1000;
36-
36+
3737
private ILogger Logger { get; set; }
3838
private Receiver Receiver { get; set; }
3939
private Timer Timer { get; set; }
40+
41+
private LogTimeFormat LogTimeFormat { get; set; }
4042

4143
private bool isActive;
4244
public bool IsActive {
@@ -123,6 +125,7 @@ public NamespaceViewModel SelectedNamespaceViewModel {
123125
public LoginatorViewModel(IApplicationConfiguration applicationConfiguration, IConfigurationDao configurationDao) {
124126
ApplicationConfiguration = applicationConfiguration;
125127
ConfigurationDao = configurationDao;
128+
ConfigurationDao.OnConfigurationChanged += ConfigurationDao_OnConfigurationChanged;
126129
IsActive = true;
127130
SelectedInitialLogLevel = LoggingLevel.TRACE;
128131
NumberOfLogsPerLevel = Constants.DEFAULT_MAX_NUMBER_OF_LOGS_PER_LEVEL;
@@ -133,6 +136,11 @@ public LoginatorViewModel(IApplicationConfiguration applicationConfiguration, IC
133136
Applications = new ObservableCollection<ApplicationViewModel>();
134137
}
135138

139+
private void ConfigurationDao_OnConfigurationChanged(object sender, EventArgs e) {
140+
Logger.Info("[ConfigurationDao_OnConfigurationChanged] Configuration changed.");
141+
LogTimeFormat = ConfigurationDao.Read().LogTimeFormat;
142+
}
143+
136144
public void StartListener() {
137145
Receiver = IoC.Get<Receiver>();
138146
Receiver.LogReceived += Receiver_LogReceived;
@@ -177,7 +185,11 @@ private void Callback(Object state) {
177185
}
178186

179187
private LogViewModel ToLogViewModel(Log log) {
180-
return Mapper.Map<Log, LogViewModel>(log);
188+
var logViewModel = Mapper.Map<Log, LogViewModel>(log);
189+
if (LogTimeFormat == LogTimeFormat.CONVERT_TO_LOCAL_TIME) {
190+
logViewModel.Timestamp = logViewModel.Timestamp.ToLocalTime();
191+
}
192+
return logViewModel;
181193
}
182194

183195
private void Receiver_LogReceived(object sender, LogReceivedEventArgs e) {

0 commit comments

Comments
 (0)