Skip to content
Snippets Groups Projects
Commit 6a99b501 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Use bytes for all sizes, introduce BytesToString for pretty-printing.

Fixes #24
parent d1e07260
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,20 @@
public static class Conv
{
public const int MegaBytes = 1024 * 1024;
public const int GigaBytes = 1024 * 1024 * 1024;
/// <summary>
/// Convert bytes into a human-readable string with the appropriate suffix (up to TB).
/// </summary>
/// <param name="numBytes">The number of bytes.</param>
/// <returns>A formatted string with the size showing one decimal.</returns>
public static string BytesToString(long numBytes) {
string[] suffixes = {"Bytes", "KB", "MB", "GB", "TB"};
var order = 0;
while (numBytes >= 1024 && order < suffixes.Length - 1) {
order++;
numBytes /= 1024;
}
return string.Format("{0:0.#} {1}", numBytes, suffixes[order]);
}
}
}
......@@ -51,9 +51,9 @@ namespace ATXCommon
/// <param name="thresh">The number of days used as expiration threshold.</param>
/// <returns>A dictionary having usernames as keys (of those users that actually do have
/// expired directories), where the values are lists of tuples with the DirInfo objects,
/// size and age (in days) of the expired directories.</returns>
public static Dictionary<string, List<Tuple<DirectoryInfo, long, int>>> ExpiredDirs(
DirectoryInfo baseDir,int thresh) {
/// size (in bytes) and age (in days) of the expired directories.</returns>
public static Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>
ExpiredDirs(DirectoryInfo baseDir,int thresh) {
var collection = new Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>();
var now = DateTime.Now;
......@@ -65,7 +65,7 @@ namespace ATXCommon
continue;
long size = -1;
try {
size = GetDirectorySize(subdir.FullName) / Conv.MegaBytes;
size = GetDirectorySize(subdir.FullName);
}
catch (Exception ex) {
Log.Error("ERROR getting directory size of [{0}]: {1}",
......
......@@ -283,7 +283,9 @@ namespace ATXCommon.Serializables
}
foreach (var driveToCheck in SpaceMonitoring) {
msg += "Drive to check free space: " + driveToCheck.DriveName +
" (threshold: " + driveToCheck.SpaceThreshold + ")" + "\n";
" (threshold: " +
Conv.BytesToString(driveToCheck.SpaceThreshold * Conv.MegaBytes) +
")" + "\n";
}
if (string.IsNullOrEmpty(SmtpHost)) {
msg += "SmtpHost: ====== Not configured, disabling email! ======" + "\n";
......
......@@ -53,14 +53,14 @@ namespace ATXCommon
}
/// <summary>
/// Get the free space of a drive in megabytes.
/// Get the free space of a drive in bytes.
/// </summary>
/// /// <param name="drive">The drive name, e.g. "c:".</param>
/// <returns>Free space of a drive in megabytes, zero if an error occured.</returns>
/// <returns>Free space of a drive in bytes, zero if an error occured.</returns>
public static long GetFreeDriveSpace(string drive) {
try {
var dInfo = new DriveInfo(drive);
return dInfo.TotalFreeSpace / Conv.MegaBytes;
return dInfo.TotalFreeSpace;
}
catch (Exception ex) {
Log.Warn("Error in GetFreeDriveSpace({0}): {1}", drive, ex.Message);
......@@ -80,8 +80,8 @@ namespace ATXCommon
continue;
msg += "Drive '" + driveToCheck.DriveName +
"' - free space: " + freeSpace +
" (threshold: " + driveToCheck.SpaceThreshold + ")\n";
"' - free space: " + Conv.BytesToString(freeSpace) +
" (threshold: " + Conv.BytesToString(driveToCheck.SpaceThreshold) + ")\n";
}
return msg;
}
......
......@@ -289,7 +289,7 @@ namespace AutoTx
"Free system memory: " + SystemChecks.GetFreeMemory() + " MB" + "\n";
foreach (var driveToCheck in _config.SpaceMonitoring) {
msg += "Free space on drive '" + driveToCheck.DriveName + "': " +
SystemChecks.GetFreeDriveSpace(driveToCheck.DriveName) + "\n";
Conv.BytesToString(SystemChecks.GetFreeDriveSpace(driveToCheck.DriveName)) + "\n";
}
......@@ -920,8 +920,8 @@ namespace AutoTx
foreach (var userdir in expired.Keys) {
report += "\n - user '" + userdir + "'\n";
foreach (var subdir in expired[userdir]) {
report += string.Format(" - {0} [age: {2} days, size: {1} MB]\n",
subdir.Item1, subdir.Item2, subdir.Item3);
report += string.Format(" - {0} [age: {2} days, size: {1}]\n",
subdir.Item1, Conv.BytesToString(subdir.Item2), subdir.Item3);
}
}
if (string.IsNullOrEmpty(report))
......
......@@ -72,8 +72,8 @@ namespace AutoTx
_roboCommand.RetryOptions.RetryCount = 0;
_roboCommand.RetryOptions.RetryWaitTime = 2;
_roboCommand.Start();
Log.Info("Transfer started, total size: {0} MB",
_status.CurrentTransferSize / Conv.MegaBytes);
Log.Info("Transfer started, total size: {0}",
Conv.BytesToString(_status.CurrentTransferSize));
}
catch (ManagementException ex) {
Log.Error("Error in StartTransfer(): {0}", ex.Message);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment