Changeset 2222

Show
Ignore:
Timestamp:
07/08/2008 14:47:59 (5 months ago)
Author:
chris
Message:

Add machine-readable output mode (with -m option) to bbstoreaccounts info.

Location:
box/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • box/trunk/bin/bbackupquery/BackupQueries.cpp

    r2182 r2222  
    21912191void BackupQueries::CommandUsageDisplayEntry(const char *Name, int64_t Size, int64_t HardLimit, int32_t BlockSize) 
    21922192{ 
    2193         std::cout << FormatUsageLineStart(Name) << 
    2194                 FormatUsageBar(Size, Size * BlockSize, HardLimit * BlockSize) << 
     2193        std::cout << FormatUsageLineStart(Name, false) << 
     2194                FormatUsageBar(Size, Size * BlockSize, HardLimit * BlockSize, false) << 
    21952195                std::endl; 
    21962196} 
  • box/trunk/bin/bbstoreaccounts/bbstoreaccounts.cpp

    r2149 r2222  
    4040#define MAX_SOFT_LIMIT_SIZE             97 
    4141 
     42bool sMachineReadableOutput = false; 
     43 
    4244void CheckSoftHardLimits(int64_t SoftLimit, int64_t HardLimit) 
    4345{ 
     
    7274{ 
    7375        return FormatUsageBar(Blocks, Blocks * BlockSizeOfDiscSet(DiscSet), 
    74                 MaxBlocks * BlockSizeOfDiscSet(DiscSet)); 
     76                MaxBlocks * BlockSizeOfDiscSet(DiscSet), 
     77                sMachineReadableOutput); 
    7578} 
    7679 
     
    206209{ 
    207210        // Load in the account database  
    208         std::auto_ptr<BackupStoreAccountDatabase> db(BackupStoreAccountDatabase::Read(rConfig.GetKeyValue("AccountDatabase").c_str())); 
     211        std::auto_ptr<BackupStoreAccountDatabase> db( 
     212                BackupStoreAccountDatabase::Read( 
     213                        rConfig.GetKeyValue("AccountDatabase").c_str())); 
    209214         
    210215        // Exists? 
     
    221226        int discSet; 
    222227        acc.GetAccountRoot(ID, rootDir, discSet); 
    223         std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(ID, rootDir, discSet, true /* ReadOnly */)); 
     228        std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(ID, 
     229                rootDir, discSet, true /* ReadOnly */)); 
    224230         
    225231        // Then print out lots of info 
    226         std::cout << FormatUsageLineStart("Account ID") << 
     232        std::cout << FormatUsageLineStart("Account ID", sMachineReadableOutput) << 
    227233                BOX_FORMAT_ACCOUNT(ID) << std::endl; 
    228         std::cout << FormatUsageLineStart("Last object ID") << 
     234        std::cout << FormatUsageLineStart("Last object ID", sMachineReadableOutput) << 
    229235                BOX_FORMAT_OBJECTID(info->GetLastObjectIDUsed()) << std::endl; 
    230         std::cout << FormatUsageLineStart("Used") << 
     236        std::cout << FormatUsageLineStart("Used", sMachineReadableOutput) << 
    231237                BlockSizeToString(info->GetBlocksUsed(), 
    232238                        info->GetBlocksHardLimit(), discSet) << std::endl; 
    233         std::cout << FormatUsageLineStart("Old files") << 
     239        std::cout << FormatUsageLineStart("Old files", sMachineReadableOutput) << 
    234240                BlockSizeToString(info->GetBlocksInOldFiles(), 
    235241                        info->GetBlocksHardLimit(), discSet) << std::endl; 
    236         std::cout << FormatUsageLineStart("Deleted files") << 
     242        std::cout << FormatUsageLineStart("Deleted files", sMachineReadableOutput) << 
    237243                BlockSizeToString(info->GetBlocksInDeletedFiles(), 
    238244                        info->GetBlocksHardLimit(), discSet) << std::endl; 
    239         std::cout << FormatUsageLineStart("Directories") << 
     245        std::cout << FormatUsageLineStart("Directories", sMachineReadableOutput) << 
    240246                BlockSizeToString(info->GetBlocksInDirectories(), 
    241247                        info->GetBlocksHardLimit(), discSet) << std::endl; 
    242         std::cout << FormatUsageLineStart("Soft limit") << 
     248        std::cout << FormatUsageLineStart("Soft limit", sMachineReadableOutput) << 
    243249                BlockSizeToString(info->GetBlocksSoftLimit(), 
    244250                        info->GetBlocksHardLimit(), discSet) << std::endl; 
    245         std::cout << FormatUsageLineStart("Hard limit") << 
     251        std::cout << FormatUsageLineStart("Hard limit", sMachineReadableOutput) << 
    246252                BlockSizeToString(info->GetBlocksHardLimit(), 
    247253                        info->GetBlocksHardLimit(), discSet) << std::endl; 
    248         std::cout << FormatUsageLineStart("Client store marker") << 
     254        std::cout << FormatUsageLineStart("Client store marker", sMachineReadableOutput) << 
    249255                info->GetLastObjectIDUsed() << std::endl; 
    250256         
     
    429435"        set numbers) with the specified soft and hard limits (in blocks\n" 
    430436"        if suffixed with B, MB with M, GB with G)\n" 
    431 "  info <account>\n" 
     437"  info [-m] <account>\n" 
    432438"        Prints information about the specified account including number\n" 
    433 "        of blocks used.\n" 
     439"        of blocks used. The -m option enable machine-readable output.\n" 
    434440"  setlimit <accounts> <softlimit> <hardlimit>\n" 
    435441"        Changes the limits of the account as specified. Numbers are\n" 
     
    465471        // See if there's another entry on the command line 
    466472        int c; 
    467         while((c = getopt(argc, (char * const *)argv, "c:")) != -1) 
     473        while((c = getopt(argc, (char * const *)argv, "c:m")) != -1) 
    468474        { 
    469475                switch(c) 
     
    474480                        break; 
    475481                 
     482                case 'm': 
     483                        // enable machine readable output 
     484                        sMachineReadableOutput = true; 
     485                        break; 
     486 
    476487                case '?': 
    477488                default: 
  • box/trunk/lib/common/Utils.cpp

    r2221 r2222  
    253253} 
    254254 
    255 std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max) 
     255std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max, 
     256        bool MachineReadable) 
    256257{ 
    257258        std::ostringstream result; 
    258259         
    259         // Bar graph 
    260         char bar[17]; 
    261         unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); 
    262         if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} 
    263         for(unsigned int l = 0; l < b; l++) 
    264         { 
    265                 bar[l] = '*'; 
    266         } 
    267         for(unsigned int l = b; l < sizeof(bar) - 1; l++) 
    268         { 
    269                 bar[l] = ' '; 
    270         } 
    271         bar[sizeof(bar)-1] = '\0'; 
    272          
    273         result << std::fixed << 
    274                 std::setw(10) << Blocks << " blocks, " << 
    275                 std::setw(10) << HumanReadableSize(Bytes) << ", " <<  
    276                 std::setw(3) << std::setprecision(0) << 
    277                 ((Bytes*100)/Max) << "% |" << bar << "|"; 
     260 
     261        if (MachineReadable) 
     262        { 
     263                result << (Bytes >> 10) << " kB, " << 
     264                        std::setprecision(0) << ((Bytes*100)/Max) << "%"; 
     265        } 
     266        else 
     267        { 
     268                // Bar graph 
     269                char bar[17]; 
     270                unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); 
     271                if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} 
     272                for(unsigned int l = 0; l < b; l++) 
     273                { 
     274                        bar[l] = '*'; 
     275                } 
     276                for(unsigned int l = b; l < sizeof(bar) - 1; l++) 
     277                { 
     278                        bar[l] = ' '; 
     279                } 
     280                bar[sizeof(bar)-1] = '\0'; 
     281                 
     282                result << std::fixed << 
     283                        std::setw(10) << Blocks << " blocks, " << 
     284                        std::setw(10) << HumanReadableSize(Bytes) << ", " <<  
     285                        std::setw(3) << std::setprecision(0) << 
     286                        ((Bytes*100)/Max) << "% |" << bar << "|"; 
     287        } 
    278288         
    279289        return result.str(); 
    280290} 
    281291 
    282 std::string FormatUsageLineStart(const std::string& rName) 
    283 { 
    284         std::ostringstream result;       
    285         result << std::setw(20) << std::right << rName << ": "; 
     292std::string FormatUsageLineStart(const std::string& rName, 
     293        bool MachineReadable) 
     294{ 
     295        std::ostringstream result; 
     296 
     297        if (MachineReadable) 
     298        { 
     299                result << rName << ": "; 
     300        } 
     301        else 
     302        { 
     303                result << std::setw(20) << std::right << rName << ": "; 
     304        } 
     305 
    286306        return result.str(); 
    287307} 
  • box/trunk/lib/common/Utils.h

    r2127 r2222  
    3232int ObjectExists(const std::string& rFilename); 
    3333std::string HumanReadableSize(int64_t Bytes); 
    34 std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max); 
    35 std::string FormatUsageLineStart(const std::string& rName); 
     34std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max, 
     35        bool MachineReadable); 
     36std::string FormatUsageLineStart(const std::string& rName, 
     37        bool MachineReadable); 
    3638 
    3739#include "MemLeakFindOff.h"