From fff555f57dbcbfc47910a3ac15b6774755568a46 Mon Sep 17 00:00:00 2001 From: Mark Cannon Date: Wed, 1 Sep 2010 18:32:27 +0000 Subject: [PATCH] Refactored some logic (to match trunk), refactored GetDiskSpaceFree/GetDiskSpaceTotal git-svn-id: https://openitg.svn.sourceforge.net/svnroot/openitg/branches/dev@805 83fadc84-e282-4d84-a09a-c4228d6ae7e5 --- src/DiagnosticsUtil.cpp | 183 ++++++++++++++++++++++++++---------------------- 1 file changed, 100 insertions(+), 83 deletions(-) diff --git a/src/DiagnosticsUtil.cpp b/src/DiagnosticsUtil.cpp index e0044a35..b320ce8c 100644 --- a/src/DiagnosticsUtil.cpp +++ b/src/DiagnosticsUtil.cpp @@ -7,6 +7,7 @@ #include "LuaManager.h" #include "DiagnosticsUtil.h" #include "arch/ArchHooks/ArchHooks.h" +#include "UserPackManager.h" // for USER_PACK_SAVE_PATH #include "XmlFile.h" #include "ProductInfo.h" @@ -49,37 +50,53 @@ CString DiagnosticsUtil::GetIP() return sError; } -CString DiagnosticsUtil::GetFreeDiskSpace() +namespace { - uint64_t iDiskSpace = HOOKS->GetDiskSpaceFree( "/UserPacks" ); - double fShownSpace = 0.0; + unsigned KILOBYTE = 1024; + unsigned MEGABYTE = 1024*KILOBYTE; + unsigned GIGABYTE = 1024*MEGABYTE; - CString sSuffix = " bytes", sAmount = "0"; - - if ( (iDiskSpace / (1024*1024*1000)) > 0 ) - { - fShownSpace = iDiskSpace / (1024*1024*1000); - sSuffix = "GB"; - sAmount = ssprintf("%.02f", fShownSpace); - } - else if ( (iDiskSpace / (1024*1024)) > 0 ) - { - fShownSpace = iDiskSpace / (1024*1024); - sSuffix = "MB"; - sAmount = ssprintf("%.02f", fShownSpace); - } - else if ( (iDiskSpace / 1024) > 0 ) - { - fShownSpace = iDiskSpace / 1024; - sSuffix = "Kb"; - sAmount = ssprintf("%.02f", fShownSpace); - } - else + CString FormatByteValue( uint64_t iBytes ) { - sAmount = ssprintf("%llu", iDiskSpace); + double fShownSpace = 0.0f; + + if( iBytes > GIGABYTE ) + { + fShownSpace = iBytes / GIGABYTE; + sSuffix = "GB"; + } + else if( iBytes > MEGABYTE ) + { + fShownSpace = iBytes / MEGABYTE; + sSuffix = "MB"; + } + else if( iBytes > KILOBYTE ) + { + fShownSpace = iByte / KILOBYTE; + sSuffix = "KB"; + } + else + { + fShownSpace = double(iBytes); + sSuffix = "bytes"; + + return ssprintf( "%.02f %s", fShownSpace, sSuffix.c_str() ); } +} + +// XXX: we should probably take a parameter for these later on. +// for now, return the only disk space value that matters to us. + +CString DiagnosticsUtil::GetDiskSpaceFree() +{ + uint64_t iBytes = HOOKS->GetDiskSpaceFree( USER_PACK_SAVE_PATH ); + return FormatByteValue( iBytes ); +} - return ssprintf("%s %s", sAmount.c_str(), sSuffix.c_str()); +CString DiagnosticsUtil::GetDiskSpaceTotal() +{ + uint64_t iBytes = HOOKS->GetDiskSpaceTotal( USER_PACK_SAVE_PATH ); + return FormatByteValue( iBytes ); } int DiagnosticsUtil::GetRevision() @@ -160,57 +177,57 @@ CString DiagnosticsUtil::GetProductName() return CString(PRODUCT_NAME_VER); } -const CString& DiagnosticsUtil::GetSerialNumber() +namespace { - static CString g_SerialNum; - - if ( !g_SerialNum.empty() ) - return g_SerialNum; - -/* Try to grab a serial number from a dongle; - * otherwise generate a fake one. */ - g_SerialNum = iButton::GetSerialNumber(); - - if( g_SerialNum.empty() ) - g_SerialNum = GenerateDebugSerial(); - - return g_SerialNum; + /* this allows us to use the serial numbers on home builds for + * debugging information. VersionDate and VersionNumber are extern'd + * from verstub. */ + CString GenerateDebugSerial() + { + char system, type; + + // set the compilation OS + #if defined(WIN32) + system = 'W'; /* Windows */ + #elif defined(LINUX) + if( VersionSVN ) + system = 'S'; /*nix, with SVN */ + else + system = 'L'; /*nix, no SVN */ + #elif defined(DARWIN) + system = 'M'; /* Mac OS */ + #else + system = 'U'; /* unknown */ + #endif + + // set the compilation arcade type + #ifdef ITG_ARCADE + type = 'A'; + #else + type = 'P'; + #endif + + // if SVN, display revision: "OITG-W-20090409-600-P" + // if no SVN, display build in hex: "OITG-W-20090409-08A-P" + if( VersionSVN ) + return ssprintf( "OITG-%c-%s-%03lu-%c", system, + VersionDate, VersionNumber, type ); + else + return ssprintf( "OITG-%c-%s-%03lX-%c", system, + VersionDate, VersionNumber, type ); + } } -/* this allows us to use the serial numbers on builds for - * more helpful debugging information. PRODUCT_BUILD_DATE - * is defined in ProductInfo.h */ -CString DiagnosticsUtil::GenerateDebugSerial() +CString DiagnosticsUtil::GetSerialNumber() { - char system, type; + /* Attempt to get a serial number from the dongle */ + CString sSerial = iButton::GetSerialNumber(); -// set the compilation OS -#if defined(WIN32) - system = 'W'; /* Windows */ -#elif defined(LINUX) - if( VersionSVN ) - system = 'S'; /*nix, with SVN */ - else - system = 'L'; /*nix, no SVN */ -#elif defined(DARWIN) - system = 'M'; /* Mac OS */ -#else - system = 'U'; /* unknown */ -#endif - -// set the compilation arcade type -#ifdef ITG_ARCADE - type = 'A'; -#else - type = 'P'; -#endif - - // if SVN, display the version regularly: "OITG-W-20090409-600-P" - // if no SVN, display the version in hex: "OITG-W-20090409-08A-P" - if( VersionSVN ) - return ssprintf( "OITG-%c-%s-%03lu-%c", system, VersionDate, VersionNumber, type ); - else - return ssprintf( "OITG-%c-%s-%03lX-%c", system, VersionDate, VersionNumber, type ); + /* If the dongle failed to read, generate a debug serial. */ + if( sSerial.empty() ) + sSerial = GenerateDebugSerial(); + + return sSerial; } bool DiagnosticsUtil::HubIsConnected() @@ -218,10 +235,6 @@ bool DiagnosticsUtil::HubIsConnected() vector vDevices; GetUSBDeviceList( vDevices ); - /* Hub can't be connected if there are no devices. */ - if( vDevices.size() == 0 ) - return false; - for( unsigned i = 0; i < vDevices.size(); i++ ) if( vDevices[i].IsHub() ) return true; @@ -229,31 +242,37 @@ bool DiagnosticsUtil::HubIsConnected() return false; } -CString m_sInputType = ""; +CString g_sInputType = ""; CString DiagnosticsUtil::GetInputType() { - return m_sInputType; + return g_sInputType; } -void DiagnosticsUtil::SetInputType( CString sType ) +void DiagnosticsUtil::SetInputType( const CString &sType ) { - m_sInputType = sType; + g_sInputType = sType; } +// set OPENITG LUA variables from here void SetProgramGlobals( lua_State* L ) { LUA->SetGlobal( "OPENITG", true ); LUA->SetGlobal( "OPENITG_VERSION", PRODUCT_TOKEN ); } +REGISTER_WITH_LUA_FUNCTION( SetProgramGlobals ); + // LUA bindings for diagnostics functions #include "LuaFunctions.h" LuaFunction_NoArgs( GetUptime, SecondsToHHMMSS( (int)RageTimer::GetTimeSinceStart() ) ); LuaFunction_NoArgs( GetNumIOErrors, ITGIO::m_iInputErrorCount ); -LuaFunction_NoArgs( GetFreeDiskSpace, DiagnosticsUtil::GetFreeDiskSpace() ); + +// disk space functions +LuaFunction_NoArgs( GetDiskSpaceFree, DiagnosticsUtil::GetDiskSpaceFree() ); +LuaFunction_NoArgs( GetDiskSpaceTotal, DiagnosticsUtil::GetDiskSpaceTotal() ); // product name function LuaFunction_NoArgs( GetProductName, DiagnosticsUtil::GetProductName() ); @@ -270,8 +289,6 @@ LuaFunction_NoArgs( GetSerialNumber, DiagnosticsUtil::GetSerialNumber() ); LuaFunction_NoArgs( HubIsConnected, DiagnosticsUtil::HubIsConnected() ); LuaFunction_NoArgs( GetInputType, DiagnosticsUtil::GetInputType() ); -// set OPENITG LUA variables from here -REGISTER_WITH_LUA_FUNCTION( SetProgramGlobals ); /* * (c) 2008 BoXoRRoXoRs * All rights reserved. -- 2.11.0