Skip to content

Commit

Permalink
Updated depedencies. Better logging. Attempt to fix Issue #24.
Browse files Browse the repository at this point in the history
- Commands file not saved at exit anymore
- Only watching changes
- Display log threshold settings dialog now works correctly
  • Loading branch information
tig committed Feb 6, 2022
1 parent 62e2b76 commit d04dee1
Show file tree
Hide file tree
Showing 24 changed files with 148 additions and 446 deletions.
324 changes: 0 additions & 324 deletions Installer/MCEControl.commands

This file was deleted.

2 changes: 1 addition & 1 deletion Installer/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.8.10
2.2.8.62
6 changes: 4 additions & 2 deletions src/AssemblyFileVersion.tt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
if (n > -1)
dir = dir.Remove(n, dir.Length - n) + "..\\..\\";
string file = Path.Combine(dir, "..\\Installer\\version.txt");
Console.WriteLine("Generating " + file + " ...");

string contents = File.ReadAllText(file);

string[] parts = contents.Split('.');
Expand All @@ -22,11 +24,11 @@
string version = string.Join(".", parts);
// write the new version number back to the file
File.WriteAllText(file, version);
Console.WriteLine("Generated Build #: " + version);
Console.WriteLine("Wrote " + version + " to " + file);
#>
// This file was generated by a tool. Any changes made manually will be lost
// the next time this project is built (via AssemblyFileVersion.tt).
using System.Reflection;
[assembly: AssemblyVersion("<#= version #>")]
[assembly: AssemblyFileVersion("<#= version #>")]
[assembly: AssemblyCopyright("Copyright © 2021 Kindel Systems, LLC.")]
[assembly: AssemblyCopyright("Copyright © 2022 Kindel Systems, LLC.")]
17 changes: 11 additions & 6 deletions src/Commands/CommandInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ public static CommandInvoker Create(string userCommandsFile, string currentVersi
serializedCmds = SerializedCommands.LoadCommands(userCommandsFile, currentVersion);
if (serializedCmds != null && serializedCmds.commandArray != null) {
foreach (var cmd in serializedCmds.commandArray) {
// TELEMETRY: Mark user defined commands as such so they don't get collected
if (!commands.ContainsKey(cmd.Cmd)) {
cmd.UserDefined = true;
}
if (!string.IsNullOrWhiteSpace(cmd.Cmd)) {
// TELEMETRY: Mark user defined commands as such so they don't get collected
if (!commands.ContainsKey(cmd.Cmd)) {
cmd.UserDefined = true;
}

commands.Add(cmd);
commands.Add(cmd);
}
else {
Logger.Instance.Log4.Error($"{commands.GetType().Name}: Cmd name can't be blank or whitespace ({cmd})");
}
}
Logger.Instance.Log4.Info($"{commands.GetType().Name}: {serializedCmds.Count} commands loaded");
}
Expand Down Expand Up @@ -115,7 +120,7 @@ private void Add(Command cmd, bool log = false) {
}
}
else {
Logger.Instance.Log4.Info($"{this.GetType().Name}: Error parsing command: {cmd.ToString()}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: Error parsing command: {cmd}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/McecCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override bool Execute() {
}
}
catch (Exception e) {
Logger.Instance.Log4.Info($"{this.GetType().Name}: ({Cmd}:{Args}) <{match.Groups[1].Value} {cmd.ToString()}/> - {e.Message}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: ({Cmd}:{Args}) <{match.Groups[1].Value} {cmd.ToString()}/> - {e.Message}");
return false;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SendInputCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public override bool Execute() {
}
}
catch (Exception e) {
Logger.Instance.Log4.Info($"{this.GetType().Name}: failed. {e.Message}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: failed. {e.Message}");
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SendMessageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override bool Execute() {
}
}
catch (Exception e) {
Logger.Instance.Log4.Info($"{this.GetType().Name}: Failed for {ClassName} with error: {e.Message}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: Failed for {ClassName} with error: {e.Message}");
return true;
}
return false;
Expand Down
44 changes: 25 additions & 19 deletions src/Commands/SerializedCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,38 @@ static public SerializedCommands LoadCommands(string userCommandsFile, string cu
SerializedCommands cmds = null;
FileStream fs = null;
try {
Logger.Instance.Log4.Info($"CommandInvoker: Loading user-defined commands from {userCommandsFile}");
Logger.Instance.Log4.Info($"SerializedCommands: Loading user-defined commands from {userCommandsFile}");
fs = new FileStream(userCommandsFile, FileMode.Open, FileAccess.Read);
cmds = Deserialize(fs);

// Is this a legacy load? If so, enable all commands and warn user
if (string.IsNullOrEmpty(cmds.Version)) {
var msg = $"{userCommandsFile} was created with a legacy version of MCE Controller.\n\nConverting it and enabling all commands it contains.\n\nDisable any commands that are not used using the Commands window.";
MessageBox.Show(msg, Application.ProductName);
Logger.Instance.Log4.Info($"CommandInvoker: {msg}");
cmds.Version = currentVersion;
cmds.commandArray = cmds.commandArray.Select(c => { c.Enabled = true; return c; }).ToArray();
if (cmds == null) {
// Deserialization failed. We could automatically rewrite the file, but that would be rude.
// Deserialize logged this
}

// If this was written by an older version, re-write it to update it
if (!string.IsNullOrEmpty(cmds.Version) && (new Version(currentVersion).CompareTo(new Version(cmds.Version))) > 0) {
Logger.Instance.Log4.Info($"CommandInvoker: Upgrading .commands file from v{cmds.Version}");
SaveCommands(userCommandsFile, cmds, currentVersion);
else {
// Is this a legacy load? If so, enable all commands and warn user
if (string.IsNullOrEmpty(cmds.Version)) {
var msg = $"{userCommandsFile} was created with a legacy version of MCE Controller.\n\nConverting it and enabling all commands it contains.\n\nDisable any commands that are not used using the Commands window.";
MessageBox.Show(msg, Application.ProductName);
Logger.Instance.Log4.Info($"SerializedCommands: {msg}");
cmds.Version = currentVersion;
cmds.commandArray = cmds.commandArray.Select(c => { c.Enabled = true; return c; }).ToArray();
}

// If this was written by an older version, re-write it to update it
if (!string.IsNullOrEmpty(cmds.Version) && (new Version(currentVersion).CompareTo(new Version(cmds.Version))) > 0) {
Logger.Instance.Log4.Info($"SerializedCommands: Upgrading .commands file from v{cmds.Version}");
SaveCommands(userCommandsFile, cmds, currentVersion);
}
}
}
catch (FileNotFoundException) {
Logger.Instance.Log4.Info($"CommandInvoker: {userCommandsFile} was not found");
Logger.Instance.Log4.Error($"SerializedCommands: {userCommandsFile} was not found");
}
catch (Exception ex) {
var msg = $"No commands loaded. Error reading {userCommandsFile} - {ex.Message}.\n\nSee log file for details: {Logger.Instance.LogFile}\n\nFor help, open an issue at github.com/tig/mcec";
MessageBox.Show(msg, currentVersion);
Logger.Instance.Log4.Info($"CommandInvoker: {msg}");
Logger.Instance.Log4.Error($"SerializedCommands: {msg}");
Logger.DumpException(ex);
}
finally {
Expand All @@ -118,7 +124,7 @@ static public void SaveCommands(string userCommandsFile, SerializedCommands comm
FileStream ucFS = null;
try {
commands.Version = currentVersion;
ucFS = new FileStream(userCommandsFile, FileMode.Create, FileAccess.ReadWrite);
ucFS = new FileStream(userCommandsFile, FileMode.Create);
new XmlSerializer(typeof(SerializedCommands)).Serialize(ucFS, commands);
}
catch (Exception e) {
Expand All @@ -127,7 +133,7 @@ static public void SaveCommands(string userCommandsFile, SerializedCommands comm
$"See log file for details: {Logger.Instance.LogFile}\n\n" +
$"For help, open an issue at github.com/tig/mcec";
MessageBox.Show(msg, Application.ProductName);
Logger.Instance.Log4.Info($"CommandInvoker: {msg}");
Logger.Instance.Log4.Error($"SerializedCommands: {msg}");
Logger.DumpException(e);
}
finally {
Expand Down Expand Up @@ -166,11 +172,11 @@ private static SerializedCommands Deserialize(Stream xmlStream) {
cmds = (SerializedCommands)new XmlSerializer(typeof(SerializedCommands)).Deserialize(lcReader);
}
catch (InvalidOperationException ex) {
Logger.Instance.Log4.Info($"CommandInvoker: No commands loaded. Error parsing .commands XML. {ex.FullMessage()}");
Logger.Instance.Log4.Error($"SerializedCommands: No commands loaded. Error parsing .commands XML. {ex.FullMessage()}");
Logger.DumpException(ex);
}
catch (Exception ex) {
Logger.Instance.Log4.Info($"CommandInvoker: Error parsing .commands XML. {ex.Message}");
Logger.Instance.Log4.Error($"SerializedCommands: Error parsing .commands XML. {ex.Message}");
Logger.DumpException(ex);
}
finally {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SetForegroundWindowCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override bool Execute() {
}
}
catch (Exception e) {
Logger.Instance.Log4.Info($"{this.GetType().Name}: Failed for {ClassName} with error: {e.Message}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: Failed for {ClassName} with error: {e.Message}");
return false;
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/ShutdownCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public override bool Execute() {
break;

default:
Logger.Instance.Log4.Info($"{this.GetType().Name}: ShutdownCommands: Invalid command: {ToString()}");
Logger.Instance.Log4.Info($"{this.GetType().Name}: Invalid command: {ToString()}");
break;
}
}
Expand All @@ -106,9 +106,11 @@ public override bool Execute() {
}

public static void Shutdown(string shutdownArgs) {
Logger.Instance.Log4.Debug($"ShutdownCommand: Invoking 'shutdown.exe {shutdownArgs}'");
var proc = System.Diagnostics.Process.Start("shutdown", shutdownArgs);
proc.WaitForExit(1000);
if (proc.ExitCode != 0x0) {
Logger.Instance.Log4.Error($"ShutdownCommand: 'shutdown.exe {shutdownArgs}' failed ({proc.ExitCode:X}). Forcing Win32Exception...");
throw new System.ComponentModel.Win32Exception(proc.ExitCode);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/StartProcessCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public override bool Execute() {
p.WaitForInputIdle(1000); // TODO: Make this settable
}
catch (System.InvalidOperationException e) {
Logger.Instance.Log4.Info($"{this.GetType().Name}: {e.Message}");
Logger.Instance.Log4.Error($"{this.GetType().Name}: {e.Message}");
return false;
}
}
Expand Down
23 changes: 11 additions & 12 deletions src/Dialogs/SettingsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ private void InitializeComponent() {
this._labelLogLevel.AutoSize = true;
this._labelLogLevel.Location = new System.Drawing.Point(13, 35);
this._labelLogLevel.Name = "_labelLogLevel";
this._labelLogLevel.Size = new System.Drawing.Size(78, 13);
this._labelLogLevel.Size = new System.Drawing.Size(277, 13);
this._labelLogLevel.TabIndex = 1;
this._labelLogLevel.Text = "Log Threshold:";
this._labelLogLevel.Text = "Log Threshold (display only; log files always contain ALL):";
//
// _checkBoxHideOnStartup
//
Expand Down Expand Up @@ -311,7 +311,7 @@ private void InitializeComponent() {
this._checkBoxEnableClient.Location = new System.Drawing.Point(20, 10);
this._checkBoxEnableClient.Margin = new System.Windows.Forms.Padding(1);
this._checkBoxEnableClient.Name = "_checkBoxEnableClient";
this._checkBoxEnableClient.Size = new System.Drawing.Size(88, 17);
this._checkBoxEnableClient.Size = new System.Drawing.Size(95, 21);
this._checkBoxEnableClient.TabIndex = 1;
this._checkBoxEnableClient.Text = "Enable &Client";
this._toolTipClient.SetToolTip(this._checkBoxEnableClient, "Starts a TCP/IP client connection to the specified address:port. Commands will be" +
Expand Down Expand Up @@ -408,7 +408,7 @@ private void InitializeComponent() {
this._checkBoxEnableServer.Location = new System.Drawing.Point(20, 10);
this._checkBoxEnableServer.Margin = new System.Windows.Forms.Padding(1);
this._checkBoxEnableServer.Name = "_checkBoxEnableServer";
this._checkBoxEnableServer.Size = new System.Drawing.Size(93, 17);
this._checkBoxEnableServer.Size = new System.Drawing.Size(100, 21);
this._checkBoxEnableServer.TabIndex = 0;
this._checkBoxEnableServer.Text = "Enable &Server";
this._toolTipServer.SetToolTip(this._checkBoxEnableServer, "Enables the TCP/IP server. It will listen on the specified port for commands.");
Expand Down Expand Up @@ -570,7 +570,7 @@ private void InitializeComponent() {
this._checkBoxEnableSerialServer.Location = new System.Drawing.Point(20, 10);
this._checkBoxEnableSerialServer.Margin = new System.Windows.Forms.Padding(1);
this._checkBoxEnableSerialServer.Name = "_checkBoxEnableSerialServer";
this._checkBoxEnableSerialServer.Size = new System.Drawing.Size(122, 17);
this._checkBoxEnableSerialServer.Size = new System.Drawing.Size(129, 21);
this._checkBoxEnableSerialServer.TabIndex = 1;
this._checkBoxEnableSerialServer.Text = "Enable Serial Server";
this._checkBoxEnableSerialServer.UseVisualStyleBackColor = true;
Expand Down Expand Up @@ -782,7 +782,7 @@ private void InitializeComponent() {
this._checkBoxEnableActivityMonitor.Location = new System.Drawing.Point(20, 10);
this._checkBoxEnableActivityMonitor.Margin = new System.Windows.Forms.Padding(1);
this._checkBoxEnableActivityMonitor.Name = "_checkBoxEnableActivityMonitor";
this._checkBoxEnableActivityMonitor.Size = new System.Drawing.Size(159, 17);
this._checkBoxEnableActivityMonitor.Size = new System.Drawing.Size(166, 21);
this._checkBoxEnableActivityMonitor.TabIndex = 0;
this._checkBoxEnableActivityMonitor.Text = "Enable &User Activity Monitor";
this._checkBoxEnableActivityMonitor.UseVisualStyleBackColor = true;
Expand Down Expand Up @@ -810,7 +810,7 @@ private void InitializeComponent() {
this._presenceDetection.AutoSize = true;
this._presenceDetection.Location = new System.Drawing.Point(17, 76);
this._presenceDetection.Name = "_presenceDetection";
this._presenceDetection.Size = new System.Drawing.Size(368, 17);
this._presenceDetection.Size = new System.Drawing.Size(375, 21);
this._presenceDetection.TabIndex = 3;
this._presenceDetection.Text = "Don\'t send message if Power Broadcast API indicates user is not present";
this._presenceDetection.UseVisualStyleBackColor = true;
Expand All @@ -821,7 +821,7 @@ private void InitializeComponent() {
this._unlockDetection.AutoSize = true;
this._unlockDetection.Location = new System.Drawing.Point(17, 53);
this._unlockDetection.Name = "_unlockDetection";
this._unlockDetection.Size = new System.Drawing.Size(216, 17);
this._unlockDetection.Size = new System.Drawing.Size(223, 21);
this._unlockDetection.TabIndex = 1;
this._unlockDetection.Text = "Don\'t send message if desktop is locked";
this._unlockDetection.UseVisualStyleBackColor = true;
Expand All @@ -832,7 +832,7 @@ private void InitializeComponent() {
this._inputDetection.AutoSize = true;
this._inputDetection.Location = new System.Drawing.Point(17, 30);
this._inputDetection.Name = "_inputDetection";
this._inputDetection.Size = new System.Drawing.Size(289, 17);
this._inputDetection.Size = new System.Drawing.Size(296, 21);
this._inputDetection.TabIndex = 0;
this._inputDetection.Text = "Send message when keyboard/mouse input is detected";
this._inputDetection.UseVisualStyleBackColor = true;
Expand Down Expand Up @@ -1012,9 +1012,6 @@ public SettingsDialog(AppSettings settings) {
}

_textBoxPacing.Text = $"{Settings.CommandPacing}";

//comboBoxLogThresholds.SelectedIndex = LogManager.GetLogger("MCEControl").Logger.Repository.LevelMap["ALL"].Value;

_buttonOk.Enabled = false;
}

Expand Down Expand Up @@ -1254,6 +1251,8 @@ private void SettingsDialog_Load(object sender, EventArgs e) {

private void comboBoxLogThresholds_SelectedIndexChanged(object sender, EventArgs e) {
Settings.TextBoxLogThreshold = _comboBoxLogThresholds.SelectedItem.ToString();
Logger.Instance.TextBoxThreshold = LogManager.GetLogger("MCEControl").Logger.Repository.LevelMap[Settings.TextBoxLogThreshold];

SettingsChanged();
}

Expand Down
Loading

0 comments on commit d04dee1

Please sign in to comment.