Skip to content

Commit 976b56f

Browse files
committed
Improved logging functionality and reduced file reads by caching SSM variables.
- Added logging to the GUI - Added developer mode toggle - SSM variables are now cached
1 parent d61cfd8 commit 976b56f

13 files changed

+484
-173
lines changed

FileAES/DecryptForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public DecryptForm(FAES_File faesFile)
1616

1717
decryptPanel.LockFileSelect(true);
1818
decryptPanel.setCloseAfterOperationSuccessful(true);
19-
decryptPanel.setFileToDecrypt(faesFile);
19+
decryptPanel.SetFileToDecrypt(faesFile);
2020
}
2121

2222
private void titleBar_Paint(object sender, PaintEventArgs e)

FileAES/DevForm.cs

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private void ExportLog_Click(object sender, EventArgs e)
111111
{
112112
string logPath = "FileAES-" + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString() + ".log";
113113

114-
_overrideLogPath = Program.settingsManager.GetLogPath();
114+
_overrideLogPath = Program.programManager.GetLogPath();
115115

116116
_overrideLogPath = _overrideLogPath.Replace('/', '\\').TrimStart('/', '\\');
117117

@@ -129,11 +129,11 @@ private void ExportLog_Click(object sender, EventArgs e)
129129
try
130130
{
131131
File.WriteAllText(logPath, consoleTextBox.Text);
132-
AppendWithColour(consoleTextBox, String.Format("[INFO] Log Exported! ({0})", logPath));
132+
Logging.Log(String.Format("Log Exported! ({0})", logPath));
133133
}
134134
catch
135135
{
136-
AppendWithColour(consoleTextBox, String.Format("[WARN] Log file could not be written to '{0}'!", logPath));
136+
Logging.Log(String.Format("Log file could not be written to '{0}'!", logPath), Severity.WARN);
137137
}
138138
}
139139

@@ -156,31 +156,35 @@ private void CommandInput(RichTextBox textbox)
156156
{
157157
string[] input = textbox.Text.ToLower().Split(' ');
158158

159-
uint csBufferTmp = 0;
160159
if (input[0] == "cryptostreambuffer" || input[0] == "csbuffer" || input[0] == "buffer")
161160
{
161+
uint csBufferTmp = 0;
162162
if (input.Length > 1 && !string.IsNullOrEmpty(input[1]) && uint.TryParse(input[1], out csBufferTmp))
163163
{
164-
AppendWithColour(consoleTextBox, String.Format("[INFO] CryptoStream Buffer set to {0} bytes", csBufferTmp));
164+
Logging.Log(String.Format("CryptoStream Buffer set to {0} bytes", csBufferTmp));
165165
FileAES_Utilities.SetCryptoStreamBuffer(csBufferTmp);
166166
}
167-
else AppendWithColour(consoleTextBox, String.Format("[WARN] Too few arguments provided for the '{0}' command!", textbox.Text));
167+
else TooFewArgsError(textbox.Text);
168168
}
169169
else if (input[0] == "getcryptostreambuffer" || input[0] == "getcsbuffer" || input[0] == "getbuffer")
170170
{
171-
AppendWithColour(consoleTextBox, String.Format("[INFO] CryptoStream Buffer is {0} bytes", FileAES_Utilities.GetCryptoStreamBuffer()));
171+
Logging.Log(String.Format("CryptoStream Buffer is {0} bytes", FileAES_Utilities.GetCryptoStreamBuffer()));
172172
}
173173
else if (input[0] == "getfaestempfolder" || input[0] == "gettemp" || input[0] == "gettempfolder")
174174
{
175-
AppendWithColour(consoleTextBox, String.Format("[INFO] FAES Temp Folder is: {0}", FileAES_Utilities.GetFaesTempFolder()));
175+
Logging.Log(String.Format("FAES Temp Folder is: {0}", FileAES_Utilities.GetFaesTempFolder()));
176176
}
177177
else if (input[0] == "getfaesversion" || input[0] == "getfaesver" || input[0] == "faesver")
178178
{
179-
AppendWithColour(consoleTextBox, String.Format("[INFO] FAES Version: {0}", FileAES_Utilities.GetVersion()));
179+
Logging.Log(String.Format("FAES Version: {0}", FileAES_Utilities.GetVersion()));
180180
}
181-
else if (input[0] == "getfaesuiversion" || input[0] == "getfaesuiver" || input[0] == "ver")
181+
else if (input[0] == "getfaesuiversion" || input[0] == "getfaesguiversion" || input[0] == "getfaesuiver" || input[0] == "getfaesguiver" || input[0] == "ver" || input[0] == "guiver" || input[0] == "faesguiver")
182182
{
183-
AppendWithColour(consoleTextBox, String.Format("[INFO] FAES_GUI Version: {0}", Program.GetVersion()));
183+
Logging.Log(String.Format("FAES_GUI Version: {0}", Program.GetVersion()));
184+
}
185+
else if (input[0] == "getssmversion" || input[0] == "getssmver" || input[0] == "ssmver")
186+
{
187+
Logging.Log(String.Format("SSM Version: {0}", SimpleSettingsManager.SSM.GetVersion()));
184188
}
185189
else if (input[0] == "exportlog" || input[0] == "export" || input[0] == "log")
186190
{
@@ -191,69 +195,62 @@ private void CommandInput(RichTextBox textbox)
191195
if (input.Length > 1 && !string.IsNullOrEmpty(input[1]))
192196
{
193197
_overrideLogPath = input[1].Replace("\"", string.Empty).Replace("\'", string.Empty);
194-
Program.settingsManager.SetLogPath(_overrideLogPath);
198+
Program.programManager.SetLogPath(_overrideLogPath);
195199

196-
AppendWithColour(consoleTextBox, String.Format("[INFO] Log path changed to: {0}", _overrideLogPath));
200+
Logging.Log(String.Format("Log path changed to: {0}", _overrideLogPath));
197201
}
198-
else AppendWithColour(consoleTextBox, String.Format("[WARN] Too few arguments provided for the '{0}' command!", textbox.Text));
202+
else TooFewArgsError(textbox.Text);
199203
}
200204
else if (input[0] == "getlogpath" || input[0] == "logpath")
201205
{
202-
_overrideLogPath = Program.settingsManager.GetLogPath();
203-
204-
AppendWithColour(consoleTextBox, String.Format("[INFO] Log path set to: {0}", _overrideLogPath));
206+
_overrideLogPath = Program.programManager.GetLogPath();
207+
Logging.Log(String.Format("Log path set to: {0}", _overrideLogPath));
205208
}
206209
else if (input[0] == "resetlogpath")
207210
{
208-
Program.settingsManager.ResetLogPath();
209-
AppendWithColour(consoleTextBox, String.Format("[INFO] Log path reset!"));
211+
Program.programManager.ResetLogPath();
212+
Logging.Log("Log path reset!");
213+
}
214+
else if (input[0] == "setdevmode" || input[0] == "setdevelopermode" || input[0] == "setdebugmode" || input[0] == "setdebug" || input[0] == "setdev" || input[0] == "setdeveloper")
215+
{
216+
if (input.Length > 1 && !string.IsNullOrEmpty(input[1]))
217+
{
218+
bool dev = false;
219+
if (input[1] == "1" || input[1] == "true" || input[1] == "t" || input[1] == "y" || input[1] == "yes") dev = true;
220+
221+
Program.programManager.SetDevMode(dev);
222+
223+
Logging.Log(String.Format("Developer Mode {0}! (Setting will be applied next launch)", dev ? "Enabled" : "Disabled"));
224+
}
225+
else TooFewArgsError(textbox.Text);
226+
}
227+
else if (input[0] == "getdevmode" || input[0] == "getdevelopermode" || input[0] == "getdebugmode" || input[0] == "getdebug" || input[0] == "getdev" || input[0] == "getdeveloper" || input[0] == "developer" || input[0] == "dev" || input[0] == "debug")
228+
{
229+
Logging.Log(String.Format("Developer Mode is {0}!", Program.programManager.GetDevMode() ? "Enabled" : "Disabled"));
230+
}
231+
else if (input[0] == "resetdevmode" || input[0] == "resetdevelopermode" || input[0] == "resetdebugmode" || input[0] == "resetdebug" || input[0] == "resetdev" || input[0] == "resetdeveloper")
232+
{
233+
Program.programManager.ResetDevMode();
234+
Logging.Log("Developer Mode reset!");
210235
}
211236
else if (input[0] == "clear" || input[0] == "cls")
212237
{
213238
clearConsole.PerformClick();
214239
}
215-
else AppendWithColour(consoleTextBox, String.Format("[WARN] Unknown command: {0}", textbox.Text));
240+
else Logging.Log(String.Format("Unknown command: {0}", textbox.Text), Severity.WARN);
216241

217242
textbox.Clear();
218243
}
219244

220-
private void ConsoleInputTextBox_TextChanged(object sender, EventArgs e)
245+
private void TooFewArgsError(string command)
221246
{
222-
if (string.IsNullOrWhiteSpace(consoleInputTextBox.Text))
223-
sendInputButton.Enabled = false;
224-
else
225-
sendInputButton.Enabled = true;
247+
Logging.Log(String.Format("Too few arguments provided for the '{0}' command!", command), Severity.WARN);
226248
}
227249

228-
private void AppendWithColour(RichTextBox textbox, string text)
229-
{
230-
textbox.SelectionColor = Color.LightGray;
231-
textbox.AppendText(text.ToString());
232-
textbox.AppendText(Environment.NewLine);
233-
234-
CheckKeyword(textbox, "[DEBUG]", Color.Violet);
235-
CheckKeyword(textbox, "[INFO]", Color.LightBlue);
236-
CheckKeyword(textbox, "[WARN]", Color.Yellow);
237-
CheckKeyword(textbox, "[ERROR]", Color.Red);
238-
239-
textbox.SelectionStart = textbox.TextLength;
240-
textbox.SelectionLength = 0;
241-
textbox.ScrollToCaret();
242-
}
243-
244-
private void CheckKeyword(RichTextBox textbox, string find, Color color)
250+
private void ConsoleInputTextBox_TextChanged(object sender, EventArgs e)
245251
{
246-
if (textbox.Text.Contains(find))
247-
{
248-
var matchString = Regex.Escape(find);
249-
foreach (Match match in Regex.Matches(textbox.Text, matchString))
250-
{
251-
textbox.Select(match.Index, find.Length);
252-
textbox.SelectionColor = color;
253-
textbox.Select(textbox.TextLength, 0);
254-
textbox.SelectionColor = Color.LightGray;
255-
};
256-
}
252+
if (string.IsNullOrWhiteSpace(consoleInputTextBox.Text)) sendInputButton.Enabled = false;
253+
else sendInputButton.Enabled = true;
257254
}
258255
}
259256
}

FileAES/EncryptForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public EncryptForm(FAES_File faesFile)
1616

1717
encryptPanel.LockFileSelect(true);
1818
encryptPanel.setCloseAfterOperationSuccessful(true);
19-
encryptPanel.setFileToEncrypt(faesFile);
19+
encryptPanel.SetFileToEncrypt(faesFile);
2020
}
2121

2222
private void titleBar_Paint(object sender, PaintEventArgs e)

FileAES/FileAES.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="DecryptForm.Designer.cs">
9898
<DependentUpon>DecryptForm.cs</DependentUpon>
9999
</Compile>
100+
<Compile Include="Logging.cs" />
100101
<Compile Include="MainForm.cs">
101102
<SubType>Form</SubType>
102103
</Compile>

FileAES/Logging.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using FAES;
2+
using System;
3+
4+
namespace FAES_GUI
5+
{
6+
internal class Logging
7+
{
8+
public static void Log(string log, Severity severity = Severity.INFO)
9+
{
10+
if (FileAES_Utilities.GetVerboseLogging())
11+
{
12+
switch (severity)
13+
{
14+
case Severity.DEBUG:
15+
{
16+
Console.WriteLine("[DEBUG] {0}", log);
17+
break;
18+
}
19+
case Severity.WARN:
20+
{
21+
Console.WriteLine("[WARN] {0}", log);
22+
break;
23+
}
24+
case Severity.ERROR:
25+
{
26+
Console.WriteLine("[ERROR] {0}", log);
27+
break;
28+
}
29+
case Severity.INFO:
30+
default:
31+
{
32+
Console.WriteLine("[INFO] {0}", log);
33+
break;
34+
}
35+
}
36+
}
37+
else if (severity > 0) Console.WriteLine(log);
38+
}
39+
}
40+
41+
internal enum Severity
42+
{
43+
DEBUG,
44+
INFO,
45+
WARN,
46+
ERROR
47+
};
48+
}

FileAES/MainForm.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using FAES;
2-
using FAES_GUI.CustomControls;
32
using System;
43
using System.Drawing;
5-
using System.Threading;
64
using System.Windows.Forms;
75

86
namespace FAES_GUI
@@ -11,7 +9,7 @@ public partial class MainForm : Form
119
{
1210
private bool _closeAfterOperation = false;
1311

14-
private DevForm _devForm = new DevForm();
12+
private DevForm _devForm;
1513

1614
public MainForm(FAES_File faesFile = null)
1715
{
@@ -21,9 +19,14 @@ public MainForm(FAES_File faesFile = null)
2119
titleLabel.Text += Program.GetVersion();
2220
this.Text = titleLabel.Text;
2321

24-
// Hacky solution to the RichTextBox Console.SetOut causing issues if the DevForm is not opened at least once before encryption/decryption (otherwise it hangs)
25-
_devForm.Show();
26-
_devForm.Hide();
22+
if (FileAES_Utilities.GetVerboseLogging())
23+
{
24+
_devForm = new DevForm();
25+
26+
// Hacky solution to the RichTextBox Console.SetOut causing issues if the DevForm is not opened at least once before encryption/decryption (otherwise it hangs)
27+
_devForm.Show();
28+
_devForm.Hide();
29+
}
2730

2831
autoSelectMenuButton.registerDetoggles(new CustomControls.SubMenuButton[3] { encryptMenuButton, decryptMenuButton, settingsMenuButton });
2932
encryptMenuButton.registerDetoggles(new CustomControls.SubMenuButton[3] { autoSelectMenuButton, decryptMenuButton, settingsMenuButton });
@@ -52,6 +55,7 @@ private void titleBar_MouseDown(object sender, MouseEventArgs e)
5255

5356
private void quitButton_Click(object sender, EventArgs e)
5457
{
58+
Logging.Log(String.Format("FAES_GUI(MainGUI): Quit Button pressed. Exiting..."), Severity.DEBUG);
5559
Environment.Exit(0);
5660
}
5761

@@ -147,53 +151,67 @@ private void FAESMenuHandler(FAES_File faesFile)
147151
{
148152
if (faesFile.isFileEncryptable())
149153
{
154+
Logging.Log(String.Format("FAES_GUI(MainGUI): FAESMenuHandler detected a valid, encryptable file! ({0})", faesFile.getPath()), Severity.DEBUG);
155+
150156
encryptMenuButton_Click(null, null);
151157
encryptMenuButton.Selected = true;
152158
autoSelectMenuButton.Selected = false;
153-
encryptPanel.setFileToEncrypt(faesFile);
159+
encryptPanel.SetFileToEncrypt(faesFile);
154160
}
155161
else if (faesFile.isFileDecryptable())
156162
{
163+
Logging.Log(String.Format("FAES_GUI(MainGUI): FAESMenuHandler detected a valid, decryptable file! ({0})", faesFile.getPath()), Severity.DEBUG);
164+
157165
decryptMenuButton_Click(null, null);
158166
decryptMenuButton.Selected = true;
159167
autoSelectMenuButton.Selected = false;
160-
decryptPanel.setFileToDecrypt(faesFile);
168+
decryptPanel.SetFileToDecrypt(faesFile);
161169
}
170+
else Logging.Log(String.Format("FAES_GUI(MainGUI): FAESMenuHandler detected an invalid file! ({0})", faesFile.getPath()), Severity.DEBUG);
162171
}
163172

164173
private void autoSelectMenuButton_Click(object sender, EventArgs e)
165174
{
166175
autoDetect.BringToFront();
176+
Logging.Log(String.Format("FAES_GUI(MainGUI): AutoSelectPanel Active."), Severity.DEBUG);
167177
}
168178

169179
private void encryptMenuButton_Click(object sender, EventArgs e)
170180
{
171181
if (Control.ModifierKeys == Keys.Shift) encryptPanel.ResetFile();
172182
encryptPanel.BringToFront();
183+
Logging.Log(String.Format("FAES_GUI(MainGUI): EncryptPanel Active."), Severity.DEBUG);
173184
}
174185

175186
private void decryptMenuButton_Click(object sender, EventArgs e)
176187
{
177188
if (Control.ModifierKeys == Keys.Shift) decryptPanel.ResetFile();
178189
decryptPanel.BringToFront();
190+
Logging.Log(String.Format("FAES_GUI(MainGUI): DecryptPanel Active."), Severity.DEBUG);
179191
}
180192

181193
private void settingsMenuButton_Click(object sender, EventArgs e)
182194
{
183195
settingsPanel.BringToFront();
184196
settingsPanel.LoadSettings();
197+
Logging.Log(String.Format("FAES_GUI(MainGUI): SettingsPanel Active."), Severity.DEBUG);
185198
}
186199

187200
private void CopyrightLabel_Click(object sender, EventArgs e)
188201
{
189-
if (_devForm.WindowState == FormWindowState.Minimized && _devForm.Visible)
190-
{
191-
_devForm.WindowState = FormWindowState.Normal;
192-
}
193-
else
202+
if (FileAES_Utilities.GetVerboseLogging())
194203
{
195-
_devForm.Visible = !_devForm.Visible;
196-
if (_devForm.Visible) _devForm.WindowState = FormWindowState.Normal;
204+
if (_devForm.WindowState == FormWindowState.Minimized && _devForm.Visible)
205+
{
206+
Logging.Log(String.Format("FAES_GUI(MainGUI): DevForm detected in a minimised state. Setting its WindowState to Normal."), Severity.DEBUG);
207+
_devForm.WindowState = FormWindowState.Normal;
208+
}
209+
else
210+
{
211+
_devForm.Visible = !_devForm.Visible;
212+
if (_devForm.Visible) _devForm.WindowState = FormWindowState.Normal;
213+
Logging.Log(String.Format("FAES_GUI(MainGUI): DevForm visibility changed to: {0}.", _devForm.Visible ? "Shown" : "Hidden"), Severity.DEBUG);
214+
}
197215
}
198216
}
199217
}

0 commit comments

Comments
 (0)