/// <summary> /// Setup the logging. /// </summary> /// <param name="encodeQueueTask"> /// The encode QueueTask. /// </param> private void SetupLogging(QueueTask encodeQueueTask) { string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId)); string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId)); try { logBuffer = new StringBuilder(); // Clear the current Encode Logs if (File.Exists(logFile)) { File.Delete(logFile); } if (File.Exists(logFile2)) { File.Delete(logFile2); } fileWriter = new StreamWriter(logFile) { AutoFlush = true }; fileWriter.WriteLine(UtilityService.CreateCliLogHeader(encodeQueueTask)); } catch (Exception) { if (fileWriter != null) { fileWriter.Close(); fileWriter.Dispose(); } throw; } }
/// <summary> /// Start a scan for a given source path and title /// </summary> /// <param name="sourcePath">Path to the source file</param> /// <param name="title">the title number to look at</param> private void ScanSource(object sourcePath, int title) { try { IsScanning = true; if (this.ScanStared != null) { this.ScanStared(this, new EventArgs()); } logBuffer = new StringBuilder(); string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string dvdInfoPath = Path.Combine( logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId == 0 ? string.Empty : Init.InstanceId.ToString())); // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file) if (File.Exists(dvdInfoPath)) { File.Delete(dvdInfoPath); } string extraArguments = string.Empty; if (Init.DisableDvdNav) { extraArguments = " --no-dvdnav"; } if (title > 0) { extraArguments += " --scan "; } // Quick fix for "F:\\" style paths. Just get rid of the \\ so the CLI doesn't fall over. // Sould probably clean up the escaping of the strings later. string source = sourcePath.ToString().EndsWith("\\") ? sourcePath.ToString() : "\"" + sourcePath + "\""; this.hbProc = new Process { StartInfo = { FileName = handbrakeCLIPath, Arguments = String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, extraArguments), RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true } }; string command = String.Format(@" -i {0} -t{1} {2} -v ", source, title, extraArguments); this.hbProc = new Process { StartInfo = { FileName = handbrakeCLIPath, Arguments = command, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true } }; // Start the Scan this.hbProc.Start(); this.readData = new Parser(this.hbProc.StandardError.BaseStream); this.readData.OnScanProgress += this.OnScanProgress; this.SouceData = Source.Parse(this.readData); // Write the Buffer out to file. using (StreamWriter scanLog = new StreamWriter(dvdInfoPath)) { // Only write the log file to disk if it's less than 100MB. if (this.readData.Buffer.Length < 100000000) { scanLog.WriteLine(UtilityService.CreateCliLogHeader(null)); scanLog.Write(this.readData.Buffer); logBuffer.AppendLine(this.readData.Buffer.ToString()); } else { throw new Exception( "The Log file has not been written to disk as it has grown above the 100MB limit. This indicates there was a problem during the scan process."); } } IsScanning = false; if (this.ScanCompleted != null) { this.ScanCompleted(this, new ScanCompletedEventArgs(true, null, string.Empty)); } } catch (Exception exc) { this.Stop(); if (this.ScanCompleted != null) { this.ScanCompleted(this, new ScanCompletedEventArgs(false, exc, "An Error has occured in ScanService.ScanSource()")); } } }