Logger cleanup, remove redundant WriteToFile method

This commit is contained in:
n00mkrad
2025-11-09 16:16:42 +01:00
parent 094dea9059
commit c52e2abde9
2 changed files with 16 additions and 44 deletions

View File

@@ -7,7 +7,6 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DT = System.DateTime;
namespace Flowframes
{
@@ -18,7 +17,7 @@ namespace Flowframes
public const string defaultLogName = "sessionlog";
public static long id;
private const int maxLogSize = 100;
private const int maxLogSize = 500;
private static Dictionary<string, List<string>> sessionLogs = new Dictionary<string, List<string>>();
private static string _lastUi = "";
public static string LastUiLine { get { return _lastUi; } }
@@ -42,6 +41,7 @@ namespace Flowframes
}
private static ConcurrentQueue<LogEntry> logQueue = new ConcurrentQueue<LogEntry>();
private static readonly string _logPath = Paths.GetLogPath();
public static void Log(string msg, bool hidden = false, bool replaceLastLine = false, string filename = "")
{
@@ -51,17 +51,12 @@ namespace Flowframes
public static void ShowNext()
{
LogEntry entry;
if (logQueue.TryDequeue(out entry))
if (logQueue.TryDequeue(out var entry))
Show(entry);
}
public static void Show(LogEntry entry, bool logToFile = true)
{
//if (entry.logMessage.Contains("frame order info"))
// Debugger.Break();
if (entry.logMessage.IsEmpty())
return;
@@ -81,7 +76,8 @@ namespace Flowframes
{
if (!entry.hidden && entry.replaceLastLine)
{
textbox.Invoke(() => {
textbox.Invoke(() =>
{
textbox.Suspend();
string[] lines = textbox.Text.SplitIntoLines();
textbox.Text = string.Join(Environment.NewLine, lines.Take(lines.Count() - 1).ToArray());
@@ -121,8 +117,11 @@ namespace Flowframes
if (string.IsNullOrWhiteSpace(filename))
filename = defaultLogName;
if (Path.GetExtension(filename) != ".txt")
filename = Path.ChangeExtension(filename, "txt");
if (filename.Contains("txt"))
{
Debugger.Break(); // filename should not have extension
filename = filename.Replace(".txt", "");
}
logStr = logStr.Replace(Environment.NewLine, " ").TrimWhitespaces();
string time = DateTime.Now.ToString("MM-dd-yyyy HH:mm:ss");
@@ -134,31 +133,28 @@ namespace Flowframes
sessionLog.RemoveAt(0);
sessionLogs[filename] = sessionLog;
for(int attempt = 0; attempt < _maxLogFileWriteAttempts; attempt++)
for (int attempt = 0; attempt < _maxLogFileWriteAttempts; attempt++)
{
try
{
file = Path.Combine(Paths.GetLogPath(), filename);
file = Path.Combine(_logPath, $"{filename}.txt");
File.AppendAllText(file, appendStr);
id++;
break;
}
catch
{
Console.WriteLine($"Failed to write to log file (attempt {attempt+1}/{_maxLogFileWriteAttempts})");
Console.WriteLine($"Failed to write to log file (attempt {attempt + 1}/{_maxLogFileWriteAttempts})");
}
}
}
public static List<string> GetSessionLog(string filename)
{
if (!filename.Contains(".txt"))
filename = Path.ChangeExtension(filename, "txt");
if (sessionLogs.ContainsKey(filename))
return sessionLogs[filename];
else
return new List<string>();
return new List<string>();
}
public static List<string> GetSessionLogLastLines(string filename, int linesCount = 5)
@@ -173,30 +169,6 @@ namespace Flowframes
Log(s, hidden, replaceLastLine, filename);
}
public static void WriteToFile(string content, bool append, string filename)
{
if (string.IsNullOrWhiteSpace(filename))
filename = defaultLogName;
if (Path.GetExtension(filename) != ".txt")
filename = Path.ChangeExtension(filename, "txt");
try
{
file = Path.Combine(Paths.GetLogPath(), filename);
string time = DT.Now.Month + "-" + DT.Now.Day + "-" + DT.Now.Year + " " + DT.Now.Hour + ":" + DT.Now.Minute + ":" + DT.Now.Second;
if (append)
File.AppendAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
else
File.WriteAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
}
catch
{
}
}
public static void ClearLogBox()
{
textbox.Invoke(() => textbox.Text = "");

View File

@@ -52,7 +52,7 @@ namespace Flowframes.Os
Config.Set("compressedPyVersion", Updater.GetInstalledVer().ToString());
Logger.Log("Done compressing python runtime.");
Logger.WriteToFile(compactOutput, true, "compact");
Logger.LogToFile(compactOutput, noLineBreak: false, filename: "compact");
}
catch { }
Program.mainForm.SetWorking(false);