Additional change for r860. Improved writing the network statistics to

Rainmeter.stats.
This commit is contained in:
spx 2011-08-03 19:44:40 +00:00
parent 2c28730c23
commit 02251ed412
3 changed files with 46 additions and 24 deletions

View File

@ -581,13 +581,19 @@ void CMeasureNet::ResetStats()
** Reads statistics. ** Reads statistics.
** **
*/ */
void CMeasureNet::ReadStats(const WCHAR* iniFile) void CMeasureNet::ReadStats(const WCHAR* iniFile, std::wstring& statsDate)
{ {
WCHAR buffer[64]; WCHAR buffer[64];
CConfigParser parser; CConfigParser parser;
parser.Initialize(iniFile, NULL, NULL, L"Statistics"); parser.Initialize(iniFile, NULL, NULL, L"Statistics");
std::wstring date = parser.ReadString(L"Statistics", L"Since", L"", false);
if (!date.empty())
{
statsDate = date;
}
int count = parser.ReadInt(L"Statistics", L"NetStatsCount", 0); int count = parser.ReadInt(L"Statistics", L"NetStatsCount", 0);
c_StatValues.clear(); c_StatValues.clear();
@ -620,16 +626,25 @@ void CMeasureNet::ReadStats(const WCHAR* iniFile)
** Writes statistics. ** Writes statistics.
** **
*/ */
void CMeasureNet::WriteStats(const WCHAR* iniFile) void CMeasureNet::WriteStats(const WCHAR* iniFile, const WCHAR* statsDate)
{ {
WCHAR buffer[32]; WCHAR buffer[32];
WCHAR buffer2[64]; WCHAR buffer2[64];
size_t statsSize = c_StatValues.size() / 2; size_t statsSize = c_StatValues.size() / 2;
_snwprintf_s(buffer, _TRUNCATE, L"%i", (int)statsSize); // Reserve sufficient buffer for statistics
WritePrivateProfileString(L"Statistics", L"NetStatsCount", buffer, iniFile); std::wstring data;
data.reserve((64 * 2) + 128 * statsSize);
// Add date
AppendStatsValue(data, L"Since", statsDate);
// Add stats count
_snwprintf_s(buffer, _TRUNCATE, L"%i", (int)statsSize);
AppendStatsValue(data, L"NetStatsCount", buffer);
// Add stats
for (size_t i = 0; i < statsSize; ++i) for (size_t i = 0; i < statsSize; ++i)
{ {
ULARGE_INTEGER value; ULARGE_INTEGER value;
@ -638,22 +653,39 @@ void CMeasureNet::WriteStats(const WCHAR* iniFile)
_snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInHigh%i", (int)i + 1); _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInHigh%i", (int)i + 1);
_snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart);
WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); AppendStatsValue(data, buffer2, buffer);
_snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInLow%i", (int)i + 1); _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInLow%i", (int)i + 1);
_snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart);
WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); AppendStatsValue(data, buffer2, buffer);
value.QuadPart = c_StatValues[i * 2 + 1]; value.QuadPart = c_StatValues[i * 2 + 1];
_snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutHigh%i", (int)i + 1); _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutHigh%i", (int)i + 1);
_snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart);
WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); AppendStatsValue(data, buffer2, buffer);
_snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutLow%i", (int)i + 1); _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutLow%i", (int)i + 1);
_snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart);
WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); AppendStatsValue(data, buffer2, buffer);
} }
// Write statistics
WritePrivateProfileSection(L"Statistics", data.c_str(), iniFile);
}
/*
** AppendStatsValue
**
** Appends "key=value\0" to given string.
**
*/
void CMeasureNet::AppendStatsValue(std::wstring& data, const WCHAR* key, const WCHAR* value)
{
data += key;
data += L'=';
data += value;
data += L'\0';
} }
/* /*

View File

@ -45,8 +45,8 @@ public:
static void UpdateStats(); static void UpdateStats();
static void ResetStats(); static void ResetStats();
static void ReadStats(const WCHAR* iniFile); static void ReadStats(const WCHAR* iniFile, std::wstring& statsDate);
static void WriteStats(const WCHAR* iniFile); static void WriteStats(const WCHAR* iniFile, const WCHAR* statsDate);
static void InitializeNewApi(); static void InitializeNewApi();
static void FinalizeNewApi(); static void FinalizeNewApi();
@ -56,6 +56,8 @@ protected:
ULONG64 GetNetOctets(NET net); ULONG64 GetNetOctets(NET net);
ULONG64 GetNetStatsValue(NET net); ULONG64 GetNetStatsValue(NET net);
static void AppendStatsValue(std::wstring& data, const WCHAR* key, const WCHAR* value);
double m_CurrentTraffic; double m_CurrentTraffic;
double m_TrafficValue; double m_TrafficValue;
UINT m_Interface; UINT m_Interface;

View File

@ -3750,17 +3750,8 @@ void CRainmeter::ReadStats()
delete [] tmpSz; delete [] tmpSz;
} }
WCHAR* tmpSz = new WCHAR[MAX_LINE_LENGTH];
if (GetPrivateProfileString(L"Statistics", L"Since", L"", tmpSz, MAX_LINE_LENGTH, m_StatsFile.c_str()) > 0)
{
m_StatsDate = tmpSz;
}
delete [] tmpSz;
// Only Net measure has stats at the moment // Only Net measure has stats at the moment
CMeasureNet::ReadStats(m_StatsFile.c_str()); CMeasureNet::ReadStats(m_StatsFile.c_str(), m_StatsDate);
} }
/* /*
@ -3779,11 +3770,8 @@ void CRainmeter::WriteStats(bool bForce)
{ {
lastWrite = ticks; lastWrite = ticks;
// Write the date for statistics
WritePrivateProfileString(L"Statistics", L"Since", m_StatsDate.c_str(), m_StatsFile.c_str());
// Only Net measure has stats at the moment // Only Net measure has stats at the moment
CMeasureNet::WriteStats(m_StatsFile.c_str()); CMeasureNet::WriteStats(m_StatsFile.c_str(), m_StatsDate.c_str());
WritePrivateProfileString(NULL, NULL, NULL, m_StatsFile.c_str()); WritePrivateProfileString(NULL, NULL, NULL, m_StatsFile.c_str());
} }