From 5286ffcb30e3005569199c45bca38dfbf346cec3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 19 Sep 2008 12:55:56 +0200 Subject: Dateien in SystemBackendProc nicht nach jeder Abfrage schlie?en --- src/Common/Backends/FileLogger.h | 2 +- src/Common/Backends/SystemBackendProc.cpp | 25 ++++++++++++------------- src/Common/Backends/SystemBackendProc.h | 11 ++++++++--- src/Common/SystemBackend.h | 4 ++-- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Common/Backends/FileLogger.h b/src/Common/Backends/FileLogger.h index 8a72bec..09946df 100644 --- a/src/Common/Backends/FileLogger.h +++ b/src/Common/Backends/FileLogger.h @@ -36,7 +36,7 @@ class FileLogger : public Logger { public: FileLogger(const std::string &filename) - : file(filename.c_str(), std::ios_base::out|std::ios_base::app) {} + : file(filename.c_str(), std::ios::out|std::ios::app) {} }; } diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp index 3a3f283..f2d9152 100644 --- a/src/Common/Backends/SystemBackendProc.cpp +++ b/src/Common/Backends/SystemBackendProc.cpp @@ -19,30 +19,29 @@ #include "SystemBackendProc.h" -#include #include namespace Mad { namespace Common { namespace Backends { -SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() const { +SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() { UptimeInfo uptime = {0, 0}; - std::ifstream file("/proc/uptime"); + uptimeFile.seekg(0, std::ios::beg); - if(!file.good()) + if(!uptimeFile.good()) return uptime; float f; - file >> f; - if(!file.good()) + uptimeFile >> f; + if(!uptimeFile.good()) return uptime; uptime.uptime = (unsigned long)f; - file >> f; - if(!file.good()) + uptimeFile >> f; + if(!uptimeFile.good()) return uptime; uptime.idleTime = (unsigned long)f; @@ -50,19 +49,19 @@ SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() const { return uptime; } -SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() const { +SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() { MemoryInfo memInfo = {0, 0, 0, 0}; - std::ifstream file("/proc/meminfo"); + meminfoFile.seekg(0, std::ios::beg); - if(!file.good()) + if(!meminfoFile.good()) return memInfo; pcrecpp::RE re("(.+):\\s*(\\d+).*"); - while(!file.eof() && file.good()) { + while(!meminfoFile.eof() && meminfoFile.good()) { std::string line; - std::getline(file, line); + std::getline(meminfoFile, line); std::string name; unsigned long value; diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h index 2a85b86..fb9ecf5 100644 --- a/src/Common/Backends/SystemBackendProc.h +++ b/src/Common/Backends/SystemBackendProc.h @@ -22,21 +22,26 @@ #include "../SystemBackend.h" +#include + namespace Mad { namespace Common { namespace Backends { class SystemBackendProc : public SystemBackend { private: - SystemBackendProc() {} + std::ifstream uptimeFile; + std::ifstream meminfoFile; + + SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo") {} public: static void useBackend() { setBackend(std::auto_ptr(new SystemBackendProc())); } - virtual UptimeInfo getUptimeInfo() const; - virtual MemoryInfo getMemoryInfo() const; + virtual UptimeInfo getUptimeInfo(); + virtual MemoryInfo getMemoryInfo(); }; } diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h index 8942ff0..9157e07 100644 --- a/src/Common/SystemBackend.h +++ b/src/Common/SystemBackend.h @@ -52,12 +52,12 @@ class SystemBackend { virtual ~SystemBackend() {} - virtual UptimeInfo getUptimeInfo() const { + virtual UptimeInfo getUptimeInfo() { UptimeInfo ret = {0, 0}; return ret; } - virtual MemoryInfo getMemoryInfo() const { + virtual MemoryInfo getMemoryInfo() { MemoryInfo ret = {0, 0, 0, 0}; return ret; } -- cgit v1.2.3