added logging function and help menu
sophuwu sophie@sophuwu.site
Sun, 27 Aug 2023 20:33:38 +0100
3 files changed,
83 insertions(+),
2 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -4,4 +4,4 @@
set(CMAKE_CXX_STANDARD 17) add_executable(sysgraph main.cpp) -#target_link_libraries(sysgraph -static) +target_link_libraries(sysgraph -static)
A
Makefile
@@ -0,0 +1,42 @@
+ifeq ($(shell command -v cmake), ) +COMPCMD = g++ -o build/sysgraph main.cpp +COMPMSG = g++ +else +ifeq ($(shell command -v ninja), ) +COMPCMD = cmake -S . -B build && cmake --build build +COMPMSG = cmake +else +COMPCMD = cmake -GNinja -S . -B build && cmake --build build +COMPMSG = cmake and ninja +endif +CMAKELISTS = CMakeLists.txt +endif + +ifeq ($(shell command -v strip), ) +STRIPCMD = echo Strip not found. Binary size may be larger than expected. +else +STRIPCMD = strip --strip-all build/sysgraph +endif +ifeq ($(shell command -v upx), ) +UPXCMD = echo UPX not found. Binary size may be larger than expected. +else +UPXCMD = upx --best build/sysgraph +endif + +build: main.cpp $(CMAKELISTS) + @echo "Compiling with $(COMPMSG)" + @sleep 2 + @mkdir -p build + @$(COMPCMD) && $(STRIPCMD) && $(UPXCMD) && echo "Done! Run make test or make install then make clean" || echo "Failed!" + +clean: build + @rm -rf build + @echo "Cleaned up build directory." + +install: build/sysgraph + @echo Installing to /usr/local/bin/sysgraph + @sudo install build/sygraph /usr/local/bin/sysgraph && echo "Done!" || echo "Failed! Try running as root." + +test: build/sysgraph + @echo "Running sysgraph" + @./build/sysgraph
M
main.cpp
→
main.cpp
@@ -1,6 +1,7 @@
#include <string> #include <csignal> #include <sys/ioctl.h> +#include <chrono> int w, h;@@ -141,7 +142,43 @@ std::string print = std::string(";").append(std::to_string(x)).append("H\033[48;5;").append(std::to_string(color)).append("m \033[0m");
for (;y<h;y++) s->append("\033[").append(std::to_string(y)).append(print); } -int main(){ +int helpmenu(int argc, char* argv[]) { + if (argc < 2) return 0; + if (!(std::string(argv[1])=="-h" || std::string(argv[1])=="--help")) return 0; + printf("\nUsage: %s [OPTION]\n\n", argv[0]); + printf(" -h, --help\t\tDisplay this help message.\n"); + printf(" --log <file>\tLog to file (appends). Log frequency: 1 second. Format:\n"); + printf(" \t\tunix, cpu usage, temp, ram taken, buffer, avialbale, free\n\n"); + return 1; +} + +int logarg(int argc, char* argv[], std::string& logdir) { + if (argc < 3) return 0; + if (std::string(argv[1]) != "--log") return 0; + logdir = std::string(argv[2]); + return 1; +} + +void log(std::string logdir, meminfo mem, cpuinfo cpu) { + std::string logbuf; + logbuf.clear(); + logbuf.append(std::to_string(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count())) + .append(",").append(std::to_string(cpu.loadavg)) + .append(",").append(std::to_string(cpu.temp)) + .append(",").append(std::to_string(mem.taken())) + .append(",").append(std::to_string(mem.used()-mem.taken())) + .append(",").append(std::to_string(mem.available)) + .append(",").append(std::to_string(mem.free)) + .append("\n"); + FILE* f = fopen(logdir.c_str(), "a"); + fputs(logbuf.c_str(), f); + fclose(f); +} + +int main(int argc, char* argv[]){ + if (helpmenu(argc, argv)) return 0; + std::string logdir; + int dolog = logarg(argc, argv, logdir); meminfo mem; cpuinfo cpu; int cpugraph[150] = {0};@@ -167,6 +204,8 @@ }
cpugraph[0] = cpu.loadavg; ramgraph[0] = mem.percent(); bufgraph[0] = mem.bufpercent(); + + if (dolog) log(logdir, mem, cpu); if (w < 60 || h < 10) {