Don't use namespace std. It makes things too confusing.

This commit is contained in:
Justin Crawford 2013-04-15 11:33:28 -07:00
parent 2dc1716dd3
commit 7261b34ace
2 changed files with 24 additions and 30 deletions

@ -11,10 +11,6 @@ if(NOT CMAKE_BUILD_TYPE)
FORCE) FORCE)
endif(NOT CMAKE_BUILD_TYPE) endif(NOT CMAKE_BUILD_TYPE)
if(APPLE)
message(SEND_ERROR "OSX is currently not supported. Patches are welcome.")
endif()
add_executable(tmux-mem-cpu-load tmux-mem-cpu-load.cpp) add_executable(tmux-mem-cpu-load tmux-mem-cpu-load.cpp)
install(TARGETS tmux-mem-cpu-load install(TARGETS tmux-mem-cpu-load
RUNTIME DESTINATION bin RUNTIME DESTINATION bin

@ -18,13 +18,11 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
using namespace std;
#include <unistd.h> // sleep #include <unistd.h> // sleep
float cpu_percentage( unsigned int cpu_usage_delay ) float cpu_percentage( unsigned int cpu_usage_delay )
{ {
string stat_line; std::string stat_line;
size_t line_start_pos; size_t line_start_pos;
size_t line_end_pos; size_t line_end_pos;
unsigned long long current_user; unsigned long long current_user;
@ -39,10 +37,10 @@ float cpu_percentage( unsigned int cpu_usage_delay )
unsigned long long diff_system; unsigned long long diff_system;
unsigned long long diff_nice; unsigned long long diff_nice;
unsigned long long diff_idle; unsigned long long diff_idle;
istringstream iss; std::istringstream iss;
ifstream stat_file("/proc/stat"); std::ifstream stat_file("/proc/stat");
getline(stat_file, stat_line); std::getline(stat_file, stat_line);
stat_file.close(); stat_file.close();
// skip "cpu" // skip "cpu"
@ -59,7 +57,7 @@ float cpu_percentage( unsigned int cpu_usage_delay )
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );
stat_file.open("/proc/stat"); stat_file.open("/proc/stat");
getline(stat_file, stat_line); std::getline(stat_file, stat_line);
stat_file.close(); stat_file.close();
// skip "cpu" // skip "cpu"
@ -81,16 +79,16 @@ float cpu_percentage( unsigned int cpu_usage_delay )
return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0; return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0;
} }
string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines ) std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines )
{ {
string meter( graph_lines + 2, ' ' ); std::string meter( graph_lines + 2, ' ' );
meter[0] = '['; meter[0] = '[';
meter[meter.length() - 1] = ']'; meter[meter.length() - 1] = ']';
int meter_count = 0; int meter_count = 0;
float percentage; float percentage;
ostringstream oss; std::ostringstream oss;
oss.precision( 1 ); oss.precision( 1 );
oss.setf( ios::fixed | ios::right ); oss.setf( std::ios::fixed | std::ios::right );
percentage = cpu_percentage( cpu_usage_delay ); percentage = cpu_percentage( cpu_usage_delay );
float meter_step = 99.9 / graph_lines; float meter_step = 99.9 / graph_lines;
@ -109,19 +107,19 @@ string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines )
return oss.str(); return oss.str();
} }
string mem_string() std::string mem_string()
{ {
unsigned int total_mem; unsigned int total_mem;
unsigned int used_mem; unsigned int used_mem;
unsigned int unused_mem; unsigned int unused_mem;
size_t line_start_pos; size_t line_start_pos;
size_t line_end_pos; size_t line_end_pos;
istringstream iss; std::istringstream iss;
ostringstream oss; std::ostringstream oss;
string mem_line; std::string mem_line;
ifstream meminfo_file( "/proc/meminfo" ); std::ifstream meminfo_file( "/proc/meminfo" );
getline( meminfo_file, mem_line ); std::getline( meminfo_file, mem_line );
line_start_pos = mem_line.find_first_of( ':' ); line_start_pos = mem_line.find_first_of( ':' );
line_start_pos++; line_start_pos++;
line_end_pos = mem_line.find_first_of( 'k' ); line_end_pos = mem_line.find_first_of( 'k' );
@ -132,7 +130,7 @@ string mem_string()
for( unsigned int i = 0; i < 3; i++ ) for( unsigned int i = 0; i < 3; i++ )
{ {
getline( meminfo_file, mem_line ); std::getline( meminfo_file, mem_line );
line_start_pos = mem_line.find_first_of( ':' ); line_start_pos = mem_line.find_first_of( ':' );
line_start_pos++; line_start_pos++;
line_end_pos = mem_line.find_first_of( 'k' ); line_end_pos = mem_line.find_first_of( 'k' );
@ -147,11 +145,11 @@ string mem_string()
return oss.str(); return oss.str();
} }
string load_string() std::string load_string()
{ {
ifstream loadavg_file( "/proc/loadavg" ); std::ifstream loadavg_file( "/proc/loadavg" );
string load_line; std::string load_line;
getline( loadavg_file, load_line ); std::getline( loadavg_file, load_line );
loadavg_file.close(); loadavg_file.close();
return load_line.substr( 0, 14 ); return load_line.substr( 0, 14 );
@ -163,8 +161,8 @@ int main(int argc, char** argv)
unsigned int graph_lines = 10; unsigned int graph_lines = 10;
try try
{ {
istringstream iss; std::istringstream iss;
iss.exceptions ( ifstream::failbit | ifstream::badbit ); iss.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
if( argc > 1 ) if( argc > 1 )
{ {
iss.str( argv[1] ); iss.str( argv[1] );
@ -179,9 +177,9 @@ int main(int argc, char** argv)
iss >> graph_lines; iss >> graph_lines;
} }
} }
catch(const exception &e) catch(const std::exception &e)
{ {
cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)] [graph lines]" << endl; std::cerr << "Usage: " << argv[0] << " [tmux_status-interval(seconds)] [graph lines]" << std::endl;
return 1; return 1;
} }