Skip to content

Commit

Permalink
Fix logging. Avoid crash on group collapse.
Browse files Browse the repository at this point in the history
  • Loading branch information
lilhoser committed Apr 16, 2024
1 parent f5d3ef0 commit 2a4b002
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 51 deletions.
105 changes: 56 additions & 49 deletions pizzaui/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public MainWindow()
{
InitializeComponent();
TraceLogger.Initialize();
pizzalib.TraceLogger.Initialize();
m_AudioPlayer = new AudioPlayer();
string settingsPath = pizzalib.Settings.DefaultSettingsFileLocation;
if (!File.Exists(settingsPath))
Expand Down Expand Up @@ -79,6 +80,7 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
m_AudioPlayer.Shutdown();
TraceLogger.Shutdown();
pizzalib.TraceLogger.Shutdown();
m_CallManager.Stop();
}

Expand Down Expand Up @@ -116,7 +118,6 @@ private void openCaptureToolStripMenuItem_Click(object sender, EventArgs e)
var call = (TranscribedCall)JsonConvert.DeserializeObject(line, typeof(TranscribedCall))!;
calls.Add(call);
}
transcriptionListview.ClearObjects();
transcriptionListview.SetObjects(calls);
this.Text = $"PizzaWave - Capture {target}";
}
Expand Down Expand Up @@ -180,41 +181,62 @@ private void openSettingsToolStripMenuItem_Click(object sender, EventArgs e)
UpdateProgressLabel($"Settings loaded from {dialog.FileName}");
}

private void startToolStripMenuItem_Click(object sender, EventArgs e)
private async void startToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!m_CallManager.IsStarted())
if (m_CallManager.IsStarted())
{
UpdateProgressLabel("Starting call manager...");
startToolStripMenuItem.Enabled = false;
stopToolStripMenuItem.Enabled = false;
try
{
_ = m_CallManager.Start();
}
catch (Exception ex)
return;
}

InitializeListview();
this.Enabled = false; // lock window until init is done
UpdateProgressLabel("Initializing, please wait...");

try
{
_ = await m_CallManager.Initialize(m_Settings);
}
catch (Exception ex)
{
Trace(TraceLoggerType.MainWindow, TraceEventType.Error, $"{ex.Message}");
UpdateProgressLabel($"{ex.Message}");
return;
}
finally
{
this.Invoke(() => this.Enabled = true);
}

UpdateProgressLabel("Starting call manager...");
startToolStripMenuItem.Enabled = false;
stopToolStripMenuItem.Enabled = false;
try
{
_ = m_CallManager.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
startToolStripMenuItem.Enabled = true;
return;
}
//
// Start a timer to check if the server is up after 5 seconds
//
var timer = new System.Windows.Forms.Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler(delegate (object? sender, EventArgs e)
{
timer.Stop();
if (!m_CallManager.IsStarted())
{
MessageBox.Show(ex.Message);
startToolStripMenuItem.Enabled = true;
UpdateProgressLabel("Unable to start call manager. Please see logs.");
return;
}
//
// Start a timer to check if the server is up after 5 seconds
//
var timer = new System.Windows.Forms.Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler(delegate (object? sender, EventArgs e)
{
timer.Stop();
if (!m_CallManager.IsStarted())
{
UpdateProgressLabel("Unable to start call manager. Please see logs.");
return;
}
stopToolStripMenuItem.Enabled = true;
UpdateProgressLabel("Call manager started.");
});
timer.Start();
}
stopToolStripMenuItem.Enabled = true;
UpdateProgressLabel("Call manager started.");
});
timer.Start();
}

private void stopToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -584,6 +606,8 @@ private void ApplyNewSettings()
return;
}

InitializeListview();

//
// This routine can be called:
// by MainWindow_Shown on app startup
Expand All @@ -593,25 +617,8 @@ private void ApplyNewSettings()
// It's important that the server is not active.
//
TraceLogger.SetLevel(m_Settings.TraceLevelApp);
pizzalib.TraceLogger.SetLevel(m_Settings.TraceLevelApp);
ApplySettingsToForm();
InitializeListview();
this.Enabled = false; // lock window until init is done

UpdateProgressLabel("Initializing, please wait...");

Task.Run(() =>
{
try
{
_ = m_CallManager.Initialize(m_Settings);
}
catch (Exception ex)
{
Trace(TraceLoggerType.MainWindow, TraceEventType.Error, $"{ex.Message}");
UpdateProgressLabel($"{ex.Message}");
}
this.Invoke(() => this.Enabled = true);
});
}

private void ApplySettingsToForm()
Expand Down
16 changes: 16 additions & 0 deletions pizzaui/MainWindowListview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@ private void SetGroupingStrategy()
transcriptionListview.ShowGroups = true;
transcriptionListview.AlwaysGroupByColumn = transcriptionListview.GetColumn("Talkgroup");
transcriptionListview.ShowItemCountOnGroups = true;
transcriptionListview.GroupExpandingCollapsing += new EventHandler<GroupExpandingCollapsingEventArgs>(
delegate (object? sender, GroupExpandingCollapsingEventArgs args)
{
//
// TODO: This is a workaround to an OLV bug. For whatever reason, the undelrying listview
// groups member is not being synced with OLV's groups member and this manifests as a crash
// whenever the group is collapsed/expanded. This workaround just prevents a crash; the
// expand/collapse button does not work.
//
if (transcriptionListview.Groups.Count == 0)
{
transcriptionListview.ShowGroups = true;
transcriptionListview.BuildGroups();
}
args.Canceled = false;
});
}
}

Expand Down
3 changes: 1 addition & 2 deletions pizzaui/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"profiles": {
"pizzawave": {
"commandName": "Project",
"commandLineArgs": "--headless"
"commandName": "Project"
}
}
}

0 comments on commit 2a4b002

Please sign in to comment.