mirror of
https://github.com/optiscaler/OptiScaler.git
synced 2026-05-29 21:13:14 +00:00
Improve logging
This commit is contained in:
@@ -1,39 +1,59 @@
|
||||
#include "log.h"
|
||||
#include <fstream>
|
||||
#include <format>
|
||||
|
||||
std::ofstream logFileStream;
|
||||
std::ostream null(nullptr);
|
||||
std::ostream* logStream = &null;
|
||||
std::ofstream fileStream;
|
||||
|
||||
std::string getCurrentTimeFormatted() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto now_t = std::chrono::system_clock::to_time_t(now);
|
||||
auto now_tm = *std::localtime(&now_t);
|
||||
auto now_duration = now - std::chrono::system_clock::from_time_t(std::mktime(&now_tm));
|
||||
auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now_duration);
|
||||
std::ostringstream oss;
|
||||
oss << std::setfill('0') << std::setw(2) << now_tm.tm_hour << ":"
|
||||
<< std::setfill('0') << std::setw(2) << now_tm.tm_min << ":"
|
||||
<< std::setfill('0') << std::setw(2) << now_tm.tm_sec << "."
|
||||
<< std::setfill('0') << std::setw(6) << now_us.count();
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void log(const std::string& log) {
|
||||
*logStream << "[" << getCurrentTimeFormatted() << "] " << log << std::endl;
|
||||
}
|
||||
|
||||
NvAPI_Status Ok(const std::source_location& location) {
|
||||
log_time();
|
||||
logFileStream << location.function_name() << ": OK" << std::endl;
|
||||
log(std::format("{}: {}", location.function_name(), "OK"));
|
||||
return NVAPI_OK;
|
||||
}
|
||||
|
||||
NvAPI_Status Error(const std::source_location& location) {
|
||||
log_time();
|
||||
logFileStream << location.function_name() << ": Error" << std::endl;
|
||||
log(std::format("{}: {}", location.function_name(), "Error"));
|
||||
return NVAPI_ERROR;
|
||||
}
|
||||
|
||||
NvAPI_Status Error(NvAPI_Status status, const std::source_location& location) {
|
||||
log_time();
|
||||
logFileStream << location.function_name() << ": " << status << std::endl;
|
||||
log(std::format("{}: {}", location.function_name(), std::to_string(status)));
|
||||
return status;
|
||||
}
|
||||
|
||||
void log_time() {
|
||||
logFileStream << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() << ": ";
|
||||
}
|
||||
|
||||
void log(std::string log) {
|
||||
log_time();
|
||||
logFileStream << log << std::endl;
|
||||
}
|
||||
|
||||
void prepareLogging(std::string fileName) {
|
||||
logFileStream = std::ofstream(fileName, std::ios_base::out | std::ios_base::app);
|
||||
void prepareLogging(std::optional<std::string> fileName) {
|
||||
if (fileName.has_value()) {
|
||||
fileStream.open(fileName.value(), std::ios_base::out | std::ios_base::app);
|
||||
if (fileStream.is_open()) {
|
||||
logStream = &fileStream;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
std::cerr << "Failed to open log file: " << fileName.value() << std::endl;
|
||||
}
|
||||
}
|
||||
// logStream = &std::cout;
|
||||
}
|
||||
|
||||
void closeLogging() {
|
||||
logFileStream.close();
|
||||
fileStream.close();
|
||||
}
|
||||
@@ -6,10 +6,10 @@
|
||||
#include <source_location>
|
||||
#include "../include/nvapi.h"
|
||||
|
||||
void log_time();
|
||||
void log(std::string log);
|
||||
void prepareLogging(std::string fileName);
|
||||
std::string getCurrentTimeFormatted();
|
||||
void log(const std::string& log);
|
||||
NvAPI_Status Ok(const std::source_location &location = std::source_location::current());
|
||||
NvAPI_Status Error(const std::source_location &location = std::source_location::current());
|
||||
NvAPI_Status Error(NvAPI_Status status, const std::source_location &location = std::source_location::current());
|
||||
void prepareLogging(std::optional<std::string> fileName);
|
||||
void closeLogging();
|
||||
NvAPI_Status Ok(const std::source_location& location = std::source_location::current());
|
||||
NvAPI_Status Error(const std::source_location& location = std::source_location::current());
|
||||
NvAPI_Status Error(NvAPI_Status status, const std::source_location& location = std::source_location::current());
|
||||
|
||||
@@ -16,9 +16,13 @@
|
||||
#include "log.h"
|
||||
|
||||
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
|
||||
auto logEnv = std::getenv("NVAPI_LOG");
|
||||
switch (fdwReason) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
prepareLogging("nvapi-dummy.log");
|
||||
if (logEnv && *logEnv == '1')
|
||||
prepareLogging("nvapi-dummy.log");
|
||||
else
|
||||
prepareLogging(std::nullopt);
|
||||
log("--------------");
|
||||
break;
|
||||
case DLL_PROCESS_DETACH:
|
||||
|
||||
Reference in New Issue
Block a user