Skip to content

Commit da44102

Browse files
committed
Show last log lines for generic ffmpeg erros
1 parent 1b382d5 commit da44102

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

ff-utils-winforms/IO/Logger.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Logger
1818
public const string defaultLogName = "sessionlog";
1919
public static long id;
2020

21+
private static Dictionary<string, string> sessionLogs = new Dictionary<string, string>();
2122
private static string _lastUi = "";
2223
public static string LastUiLine { get { return _lastUi; } }
2324
private static string _lastLog = "";
@@ -110,10 +111,9 @@ public static void LogToFile(string logStr, bool noLineBreak, string filename)
110111

111112
try
112113
{
113-
if (!noLineBreak)
114-
File.AppendAllText(file, $"{Environment.NewLine}[{id}] [{time}]: {logStr}");
115-
else
116-
File.AppendAllText(file, " " + logStr);
114+
string appendStr = noLineBreak ? $" {logStr}" : $"{Environment.NewLine}[{id}] [{time}]: {logStr}";
115+
sessionLogs[filename] = (sessionLogs.ContainsKey(filename) ? sessionLogs[filename] : "") + appendStr;
116+
File.AppendAllText(file, appendStr);
117117
id++;
118118
}
119119
catch
@@ -122,35 +122,28 @@ public static void LogToFile(string logStr, bool noLineBreak, string filename)
122122
}
123123
}
124124

125-
public static void LogIfLastLineDoesNotContainMsg(string s, bool hidden = false, bool replaceLastLine = false, string filename = "")
125+
public static string GetSessionLog(string filename)
126126
{
127-
if (!GetLastLine().Contains(s))
128-
Log(s, hidden, replaceLastLine, filename);
129-
}
130-
131-
public static void WriteToFile(string content, bool append, string filename)
132-
{
133-
if (string.IsNullOrWhiteSpace(filename))
134-
filename = defaultLogName;
135-
136-
if (Path.GetExtension(filename) != ".txt")
127+
if (!filename.Contains(".txt"))
137128
filename = Path.ChangeExtension(filename, "txt");
138129

139-
file = Path.Combine(Paths.GetLogPath(), filename);
140-
141-
string time = DT.Now.Month + "-" + DT.Now.Day + "-" + DT.Now.Year + " " + DT.Now.Hour + ":" + DT.Now.Minute + ":" + DT.Now.Second;
130+
if (sessionLogs.ContainsKey(filename))
131+
return sessionLogs[filename];
132+
else
133+
return "";
134+
}
142135

143-
try
144-
{
145-
if (append)
146-
File.AppendAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
147-
else
148-
File.WriteAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
149-
}
150-
catch
151-
{
136+
public static List<string> GetSessionLogLastLines(string filename, int linesCount = 5)
137+
{
138+
string log = GetSessionLog(filename);
139+
string[] lines = log.SplitIntoLines();
140+
return lines.Reverse().Take(linesCount).Reverse().ToList();
141+
}
152142

153-
}
143+
public static void LogIfLastLineDoesNotContainMsg(string s, bool hidden = false, bool replaceLastLine = false, string filename = "")
144+
{
145+
if (!GetLastLine().Contains(s))
146+
Log(s, hidden, replaceLastLine, filename);
154147
}
155148

156149
public static void ClearLogBox()

ff-utils-winforms/Media/FfmpegOutputHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public static void LogOutput(string line, string[] ignoreStrings, ref string app
4242
}
4343

4444
string lineWithoutPath = RemoveStringsFromLine(line, ignoreStrings);
45+
string log = $"Last 4 log lines:\n{string.Join(Environment.NewLine, Logger.GetSessionLogLastLines("ffmpeg", 4))}";
4546

4647
if (lineWithoutPath.Contains("Error ") || lineWithoutPath.Contains("Unable to ") || lineWithoutPath.Contains("Could not open file"))
4748
{
48-
RunTask.Cancel($"Error: {line}");
49+
RunTask.Cancel($"Error: {line}\n\n{log}");
4950
return;
5051
}
5152

0 commit comments

Comments
 (0)