[Box Backup-commit] COMMIT r2121 - in box/trunk: bin/bbackupquery bin/bbstoreaccounts lib/common

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Thu Apr 3 23:32:53 BST 2008


Author: chris
Date: 2008-04-03 22:32:46 +0000 (Thu, 03 Apr 2008)
New Revision: 2121

Modified:
   box/trunk/bin/bbackupquery/BackupQueries.cpp
   box/trunk/bin/bbstoreaccounts/bbstoreaccounts.cpp
   box/trunk/lib/common/Utils.cpp
   box/trunk/lib/common/Utils.h
Log:
Make usage output (from bbstoreaccounts info and bbackupquery usage)
consistent and easier to read, with blocks, B/kB/MB/GB, % and an ASCII
bar graph.


Modified: box/trunk/bin/bbackupquery/BackupQueries.cpp
===================================================================
--- box/trunk/bin/bbackupquery/BackupQueries.cpp	2008-03-30 02:33:36 UTC (rev 2120)
+++ box/trunk/bin/bbackupquery/BackupQueries.cpp	2008-04-03 22:32:46 UTC (rev 2121)
@@ -27,6 +27,8 @@
 
 #include <set>
 #include <limits>
+#include <iostream>
+#include <ostream>
 
 #include "BackupQueries.h"
 #include "Utils.h"
@@ -2156,22 +2158,9 @@
 // --------------------------------------------------------------------------
 void BackupQueries::CommandUsageDisplayEntry(const char *Name, int64_t Size, int64_t HardLimit, int32_t BlockSize)
 {
-	// Calculate size in Mb
-	double mb = (((double)Size) * ((double)BlockSize)) / ((double)(1024*1024));
-	int64_t percent = (Size * 100) / HardLimit;
-
-	// Bar graph
-	char bar[41];
-	unsigned int b = (int)((Size * (sizeof(bar)-1)) / HardLimit);
-	if(b > sizeof(bar)-1) {b = sizeof(bar)-1;}
-	for(unsigned int l = 0; l < b; l++)
-	{
-		bar[l] = '*';
-	}
-	bar[b] = '\0';
-
-	// Print the entryj
-	::printf("%14s %10.1fMb %3d%% %s\n", Name, mb, (int32_t)percent, bar);
+	std::cout << FormatUsageLineStart(Name) <<
+		FormatUsageBar(Size, Size * BlockSize, HardLimit * BlockSize) <<
+		std::endl;
 }
 
 

Modified: box/trunk/bin/bbstoreaccounts/bbstoreaccounts.cpp
===================================================================
--- box/trunk/bin/bbstoreaccounts/bbstoreaccounts.cpp	2008-03-30 02:33:36 UTC (rev 2120)
+++ box/trunk/bin/bbstoreaccounts/bbstoreaccounts.cpp	2008-04-03 22:32:46 UTC (rev 2121)
@@ -13,8 +13,11 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <limits.h>
+
+#include <algorithm>
+#include <iostream>
+#include <ostream>
 #include <vector>
-#include <algorithm>
 
 #include "BoxPortsAndFiles.h"
 #include "BackupStoreConfigVerify.h"
@@ -62,22 +65,10 @@
 	return controller.GetDiscSet(DiscSet).GetBlockSize();
 }
 
-const char *BlockSizeToString(int64_t Blocks, int DiscSet)
+std::string BlockSizeToString(int64_t Blocks, int64_t MaxBlocks, int DiscSet)
 {
-	// Not reentrant, nor can be used in the same function call twice, etc.
-	static char string[256];
-	
-	// Work out size in Mb.
-	double mb = (Blocks * BlockSizeOfDiscSet(DiscSet)) / (1024.0*1024.0);
-	
-	// Format string
-#ifdef WIN32
-	sprintf(string, "%I64d (%.2fMb)", Blocks, mb);
-#else
-	sprintf(string, "%lld (%.2fMb)", Blocks, mb);
-#endif
-	
-	return string;
+	return FormatUsageBar(Blocks, Blocks * BlockSizeOfDiscSet(DiscSet),
+		MaxBlocks * BlockSizeOfDiscSet(DiscSet));
 }
 
 int64_t SizeStringToBlocks(const char *string, int DiscSet)
@@ -229,15 +220,30 @@
 	std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(ID, rootDir, discSet, true /* ReadOnly */));
 	
 	// Then print out lots of info
-	printf("                  Account ID: %08x\n", ID);
-	printf("              Last object ID: %lld\n", info->GetLastObjectIDUsed());
-	printf("                 Blocks used: %s\n", BlockSizeToString(info->GetBlocksUsed(), discSet));
-	printf("    Blocks used by old files: %s\n", BlockSizeToString(info->GetBlocksInOldFiles(), discSet));
-	printf("Blocks used by deleted files: %s\n", BlockSizeToString(info->GetBlocksInDeletedFiles(), discSet));
-	printf("  Blocks used by directories: %s\n", BlockSizeToString(info->GetBlocksInDirectories(), discSet));
-	printf("            Block soft limit: %s\n", BlockSizeToString(info->GetBlocksSoftLimit(), discSet));
-	printf("            Block hard limit: %s\n", BlockSizeToString(info->GetBlocksHardLimit(), discSet));
-	printf("         Client store marker: %lld\n", info->GetClientStoreMarker());
+	std::cout << FormatUsageLineStart("Account ID") <<
+		BOX_FORMAT_ACCOUNT(ID) << std::endl;
+	std::cout << FormatUsageLineStart("Last object ID") <<
+		BOX_FORMAT_OBJECTID(info->GetLastObjectIDUsed()) << std::endl;
+	std::cout << FormatUsageLineStart("Used") <<
+		BlockSizeToString(info->GetBlocksUsed(),
+			info->GetBlocksHardLimit(), discSet) << std::endl;
+	std::cout << FormatUsageLineStart("Old files") <<
+		BlockSizeToString(info->GetBlocksInOldFiles(),
+			info->GetBlocksHardLimit(), discSet) << std::endl;
+	std::cout << FormatUsageLineStart("Deleted files") <<
+		BlockSizeToString(info->GetBlocksInDeletedFiles(),
+			info->GetBlocksHardLimit(), discSet) << std::endl;
+	std::cout << FormatUsageLineStart("Directories") <<
+		BlockSizeToString(info->GetBlocksInDirectories(),
+			info->GetBlocksHardLimit(), discSet) << std::endl;
+	std::cout << FormatUsageLineStart("Soft limit") <<
+		BlockSizeToString(info->GetBlocksSoftLimit(),
+			info->GetBlocksHardLimit(), discSet) << std::endl;
+	std::cout << FormatUsageLineStart("Hard limit") <<
+		BlockSizeToString(info->GetBlocksHardLimit(),
+			info->GetBlocksHardLimit(), discSet) << std::endl;
+	std::cout << FormatUsageLineStart("Client store marker") <<
+		info->GetLastObjectIDUsed() << std::endl;
 	
 	return 0;
 }

Modified: box/trunk/lib/common/Utils.cpp
===================================================================
--- box/trunk/lib/common/Utils.cpp	2008-03-30 02:33:36 UTC (rev 2120)
+++ box/trunk/lib/common/Utils.cpp	2008-04-03 22:32:46 UTC (rev 2121)
@@ -159,6 +159,65 @@
 	return ((st.st_mode & S_IFDIR) == 0)?ObjectExists_File:ObjectExists_Dir;
 }
 
+std::string HumanReadableSize(int64_t Bytes)
+{
+	double readableValue = Bytes;
+	std::string units = " B";
 
+	if (readableValue > 1024)
+	{
+		readableValue /= 1024;
+		units = "kB";
+	}
+  
+	if (readableValue > 1024)
+	{
+		readableValue /= 1024;
+		units = "MB";
+	}
+  
+	if (readableValue > 1024)
+	{
+		readableValue /= 1024;
+		units = "GB";
+	}
+  
+	std::ostringstream result;
+	result << std::fixed << std::setprecision(2) << readableValue <<
+		" " << units;
+	return result.str();
+}
 
+std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max)
+{
+	std::ostringstream result;
+	
+	// Bar graph
+	char bar[17];
+	unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max);
+	if(b > sizeof(bar)-1) {b = sizeof(bar)-1;}
+	for(unsigned int l = 0; l < b; l++)
+	{
+		bar[l] = '*';
+	}
+	for(unsigned int l = b; l < sizeof(bar) - 1; l++)
+	{
+		bar[l] = ' ';
+	}
+	bar[sizeof(bar)-1] = '\0';
+	
+	result << std::fixed <<
+		std::setw(10) << Blocks << " blocks, " <<
+		std::setw(10) << HumanReadableSize(Bytes) << ", " << 
+		std::setw(3) << std::setprecision(0) <<
+		((Bytes*100)/Max) << "% |" << bar << "|";
+	
+	return result.str();
+}
 
+std::string FormatUsageLineStart(const std::string& rName)
+{
+	std::ostringstream result;	
+	result << std::setw(20) << std::right << rName << ": ";
+	return result.str();
+}

Modified: box/trunk/lib/common/Utils.h
===================================================================
--- box/trunk/lib/common/Utils.h	2008-03-30 02:33:36 UTC (rev 2120)
+++ box/trunk/lib/common/Utils.h	2008-04-03 22:32:46 UTC (rev 2121)
@@ -30,6 +30,9 @@
 	ObjectExists_Dir = 2
 };
 int ObjectExists(const std::string& rFilename);
+std::string HumanReadableSize(int64_t Bytes);
+std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max);
+std::string FormatUsageLineStart(const std::string& rName);
 
 #include "MemLeakFindOff.h"
 




More information about the Boxbackup-commit mailing list