// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// A Set of Static Utilites
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Utilities
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HandBrake.ApplicationServices.Utilities;
///
/// A Set of Static Utilites
///
public class GeneralUtilities
{
#region Constants and Fields
///
/// The Default Log Directory
///
private static readonly string LogDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
#endregion
#region Properties
///
/// Gets the number of HandBrake instances running.
///
public static int ProcessId
{
get
{
return Process.GetCurrentProcess().Id;
}
}
#endregion
#region Public Methods
///
/// Clear all the log files older than 30 Days
///
///
/// The Number of Days to Keep
///
public static void ClearLogFiles(int daysToKeep)
{
if (Directory.Exists(LogDir))
{
// Get all the log files
var info = new DirectoryInfo(LogDir);
FileInfo[] logFiles = info.GetFiles("*.txt");
// Delete old and excessivly large files (> ~50MB).
foreach (FileInfo file in logFiles)
{
try
{
if (file.LastWriteTime < DateTime.Now.AddDays(-daysToKeep))
{
File.Delete(file.FullName);
}
else if (file.Length > 50000000)
{
File.Delete(file.FullName);
}
}
catch (Exception)
{
// Silently ignore files we can't delete. They are probably being used by the app right now.
}
}
}
}
///
/// Generate the header for the log file.
///
///
/// The generatedlog header.
///
public static StringBuilder CreateLogHeader()
{
var logHeader = new StringBuilder();
StringBuilder gpuBuilder = new StringBuilder();
foreach (var item in SystemInfo.GetGPUInfo)
{
gpuBuilder.AppendLine(string.Format(" {0}", item));
}
if (string.IsNullOrEmpty(gpuBuilder.ToString().Trim()))
{
gpuBuilder.Append("GPU Information is unavailable");
}
logHeader.AppendLine(string.Format("HandBrake {0} - {1}", VersionHelper.GetVersion(), VersionHelper.GetPlatformBitnessVersion()));
logHeader.AppendLine(string.Format("OS: {0} - {1}", Environment.OSVersion, Environment.Is64BitOperatingSystem ? "64bit" : "32bit"));
logHeader.AppendLine(string.Format("CPU: {0}", SystemInfo.GetCpuCount));
logHeader.AppendLine(string.Format("Ram: {0} MB, ", SystemInfo.TotalPhysicalMemory));
logHeader.AppendLine(string.Format("GPU Information:{0}{1}", Environment.NewLine, gpuBuilder.ToString().TrimEnd()));
logHeader.AppendLine(string.Format("Screen: {0}x{1}", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height));
logHeader.AppendLine(string.Format("Temp Dir: {0}", Path.GetTempPath()));
logHeader.AppendLine(string.Format("Install Dir: {0}", Application.StartupPath));
logHeader.AppendLine(string.Format("Data Dir: {0}\n", Application.UserAppDataPath));
logHeader.AppendLine("-------------------------------------------");
return logHeader;
}
///
/// Return the standard log format line of text for a given log message
///
///
/// The Log Message
///
///
/// A Log Message in the format: "[hh:mm:ss] message"
///
public static string LogLine(string message)
{
return string.Format("[{0}] {1}", DateTime.Now.TimeOfDay, message);
}
///
/// The find hand brake instance ids.
///
///
/// The id.
///
///
/// The . True if it's a running HandBrake instance.
///
public static bool IsPidACurrentHandBrakeInstance(int id)
{
List ids = Process.GetProcessesByName("HandBrake").Select(process => process.Id).ToList();
return ids.Contains(id);
}
#endregion
}
}