diff --git a/src/logging.cpp b/src/logging.cpp new file mode 100644 index 00000000..6c85d313 --- /dev/null +++ b/src/logging.cpp @@ -0,0 +1,95 @@ +#include "logging.h" +#include "overlay.h" +#include +#include + +string os, cpu, gpu, ram, kernel, driver; +bool sysInfoFetched = false; +int gpuLoadLog = 0, cpuLoadLog = 0; +uint64_t elapsedLog; +std::vector logFiles; +double fps; +std::vector logArray; +ofstream out; +int num; +bool loggingOn; +uint64_t log_start, log_end; + +string exec(string command) { + char buffer[128]; + string result = ""; + + // Open pipe to file + FILE* pipe = popen(command.c_str(), "r"); + if (!pipe) { + return "popen failed!"; + } + + // read till end of process: + while (!feof(pipe)) { + + // use buffer to read and add to result + if (fgets(buffer, 128, pipe) != NULL) + result += buffer; + } + + pclose(pipe); + return result; +} + +void upload_file(){ + std::string command = "curl --include --request POST https://flightlessmango.com/logs -F 'log[game_id]=26506' -F 'log[user_id]=176' -F 'attachment=true' -A 'mangohud' "; + command += " -F 'log[uploads][]=@" + logFiles.back() + "'"; + + command += " | grep Location | cut -c11-"; + std::string url = exec(command); + exec("xdg-open " + url); +} + +void upload_files(){ + std::string command = "curl --include --request POST https://flightlessmango.com/logs -F 'log[game_id]=26506' -F 'log[user_id]=176' -F 'attachment=true' -A 'mangohud' "; + for (auto& file : logFiles) + command += " -F 'log[uploads][]=@" + file + "'"; + + command += " | grep Location | cut -c11-"; + std::string url = exec(command); + exec("xdg-open " + url); +} + +void writeFile(string filename){ + logFiles.push_back(filename); + out.open(filename, ios::out | ios::app); + out << "os," << "cpu," << "gpu," << "ram," << "kernel," << "driver" << endl; + out << os << "," << cpu << "," << gpu << "," << ram << "," << kernel << "," << driver << endl; + + for (size_t i = 0; i < logArray.size(); i++) + out << logArray[i].fps << "," << logArray[i].cpu << "," << logArray[i].gpu << "," << logArray[i].previous << endl; + + out.close(); + logArray.clear(); +} + +string get_current_time(){ + time_t now_log = time(0); + tm *log_time = localtime(&now_log); + std::ostringstream buffer; + buffer << std::put_time(log_time, "%Y-%m-%d_%H-%M-%S"); + string date = buffer.str(); + return date; +} + +void logging(void *params_void){ + overlay_params *params = reinterpret_cast(params_void); + while (loggingOn){ + uint64_t now = os_time_get(); + elapsedLog = now - log_start; + logArray.push_back({fps, cpuLoadLog, gpuLoadLog, elapsedLog}); + + if (params->log_duration && (elapsedLog) >= params->log_duration * 1000000) + loggingOn = false; + else + this_thread::sleep_for(chrono::milliseconds(params->log_interval)); + } + + writeFile(params->output_file + get_current_time()); +} \ No newline at end of file diff --git a/src/logging.h b/src/logging.h index a2d1778d..6188e0f4 100644 --- a/src/logging.h +++ b/src/logging.h @@ -7,13 +7,6 @@ #include "mesa/util/os_time.h" using namespace std; - -string os, cpu, gpu, ram, kernel, driver; -bool sysInfoFetched = false; -int gpuLoadLog = 0, cpuLoadLog = 0; -uint64_t elapsedLog; -std::vector logFiles; - struct logData{ double fps; int cpu; @@ -21,88 +14,21 @@ struct logData{ uint64_t previous; }; -double fps; -std::vector logArray; -ofstream out; -int num; -bool loggingOn; -uint64_t log_start, log_end; - -string exec(string command) { - char buffer[128]; - string result = ""; - - // Open pipe to file - FILE* pipe = popen(command.c_str(), "r"); - if (!pipe) { - return "popen failed!"; - } - - // read till end of process: - while (!feof(pipe)) { - - // use buffer to read and add to result - if (fgets(buffer, 128, pipe) != NULL) - result += buffer; - } - - pclose(pipe); - return result; -} - -void upload_file(){ - std::string command = "curl --include --request POST https://flightlessmango.com/logs -F 'log[game_id]=26506' -F 'log[user_id]=176' -F 'attachment=true' -A 'mangohud' "; - command += " -F 'log[uploads][]=@" + logFiles.back() + "'"; - - command += " | grep Location | cut -c11-"; - std::string url = exec(command); - exec("xdg-open " + url); -} - -void upload_files(){ - std::string command = "curl --include --request POST https://flightlessmango.com/logs -F 'log[game_id]=26506' -F 'log[user_id]=176' -F 'attachment=true' -A 'mangohud' "; - for (auto& file : logFiles) - command += " -F 'log[uploads][]=@" + file + "'"; - - command += " | grep Location | cut -c11-"; - std::string url = exec(command); - exec("xdg-open " + url); -} - -void writeFile(string filename){ - logFiles.push_back(filename); - out.open(filename, ios::out | ios::app); - out << "os," << "cpu," << "gpu," << "ram," << "kernel," << "driver" << endl; - out << os << "," << cpu << "," << gpu << "," << ram << "," << kernel << "," << driver << endl; - - for (size_t i = 0; i < logArray.size(); i++) - out << logArray[i].fps << "," << logArray[i].cpu << "," << logArray[i].gpu << "," << logArray[i].previous << endl; - - out.close(); - logArray.clear(); -} - -string get_current_time(){ - time_t now_log = time(0); - tm *log_time = localtime(&now_log); - std::ostringstream buffer; - buffer << std::put_time(log_time, "%Y-%m-%d_%H-%M-%S"); - string date = buffer.str(); - return date; -} - -void logging(void *params_void){ - overlay_params *params = reinterpret_cast(params_void); - while (loggingOn){ - uint64_t now = os_time_get(); - elapsedLog = now - log_start; - logArray.push_back({fps, cpuLoadLog, gpuLoadLog, elapsedLog}); - - if (params->log_duration && (elapsedLog) >= params->log_duration * 1000000) - loggingOn = false; - else - this_thread::sleep_for(chrono::milliseconds(params->log_interval)); - } - - writeFile(params->output_file + get_current_time()); -} \ No newline at end of file +extern string os, cpu, gpu, ram, kernel, driver; +extern bool sysInfoFetched; +extern int gpuLoadLog, cpuLoadLog; +extern uint64_t elapsedLog; +extern std::vector logFiles; +extern double fps; +extern std::vector logArray; +extern ofstream out; +extern int num; +extern bool loggingOn; +extern uint64_t log_start, log_end; + +void logging(void *params_void); +void writeFile(string filename); +string exec(string command); +string get_current_time(void); +void upload_file(void); +void upload_files(void); \ No newline at end of file diff --git a/src/meson.build b/src/meson.build index db74c2af..090b49b5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -33,6 +33,7 @@ vklayer_files = files( 'elfhacks.cpp', 'real_dlsym.cpp', 'pci_ids.cpp', + 'logging.cpp', ) opengl_files = files(