mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-08-07 02:00:45 +00:00
965c91a428
* build(deps): bump third-party/lizardbyte-common Bumps [third-party/lizardbyte-common](https://github.com/LizardByte/lizardbyte-common) from `06cd442` to `011ad2b`. - [Release notes](https://github.com/LizardByte/lizardbyte-common/releases) - [Commits](https://github.com/LizardByte/lizardbyte-common/compare/06cd442b808f02f1674f3192a84d25b9a503c482...011ad2bb139083dcf4ec21f9c09f07470891a668) --- updated-dependencies: - dependency-name: third-party/lizardbyte-common dependency-version: 011ad2bb139083dcf4ec21f9c09f07470891a668 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> * style: run clang-format across C++ sources Reformatted conditionals, loops, and long function calls across core, platform, NVENC, and test helper code to match updated formatting rules, including minor spacing normalization in type declarations. These are style-only edits with no intended behavior changes. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
129 lines
3.8 KiB
C++
129 lines
3.8 KiB
C++
/**
|
|
* @file tests/tests_log_checker.h
|
|
* @brief Utility functions to check log file contents.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <regex>
|
|
#include <src/logging.h>
|
|
#include <string>
|
|
|
|
namespace log_checker {
|
|
|
|
/**
|
|
* @brief Remove the timestamp prefix from a log line.
|
|
* @param line The log line.
|
|
* @return The log line without the timestamp prefix.
|
|
*/
|
|
inline std::string remove_timestamp_prefix(const std::string &line) {
|
|
static const std::regex timestamp_regex(R"(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\]: )");
|
|
return std::regex_replace(line, timestamp_regex, "");
|
|
}
|
|
|
|
/**
|
|
* @brief Check if a log file contains a line that starts with the given string.
|
|
* @param log_file Path to the log file.
|
|
* @param start_str The string that the line should start with.
|
|
* @return True if such a line is found, false otherwise.
|
|
*/
|
|
inline bool line_starts_with(const std::string &log_file, const std::string_view &start_str) {
|
|
logging::log_flush();
|
|
|
|
std::ifstream input(log_file);
|
|
if (!input.is_open()) {
|
|
return false;
|
|
}
|
|
|
|
for (std::string line; std::getline(input, line);) {
|
|
line = remove_timestamp_prefix(line);
|
|
if (line.rfind(start_str, 0) == 0) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @brief Check if a log file contains a line that ends with the given string.
|
|
* @param log_file Path to the log file.
|
|
* @param end_str The string that the line should end with.
|
|
* @return True if such a line is found, false otherwise.
|
|
*/
|
|
inline bool line_ends_with(const std::string &log_file, const std::string_view &end_str) {
|
|
logging::log_flush();
|
|
|
|
std::ifstream input(log_file);
|
|
if (!input.is_open()) {
|
|
return false;
|
|
}
|
|
|
|
for (std::string line; std::getline(input, line);) {
|
|
line = remove_timestamp_prefix(line);
|
|
if (line.size() >= end_str.size() && line.compare(line.size() - end_str.size(), end_str.size(), end_str) == 0) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @brief Check if a log file contains a line that equals the given string.
|
|
* @param log_file Path to the log file.
|
|
* @param str The string that the line should equal.
|
|
* @return True if such a line is found, false otherwise.
|
|
*/
|
|
inline bool line_equals(const std::string &log_file, const std::string_view &str) {
|
|
logging::log_flush();
|
|
|
|
std::ifstream input(log_file);
|
|
if (!input.is_open()) {
|
|
return false;
|
|
}
|
|
|
|
for (std::string line; std::getline(input, line);) {
|
|
line = remove_timestamp_prefix(line);
|
|
if (line == str) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @brief Check if a log file contains a line that contains the given substring.
|
|
* @param log_file Path to the log file.
|
|
* @param substr The substring to search for.
|
|
* @param case_insensitive Whether the search should be case-insensitive.
|
|
* @return True if such a line is found, false otherwise.
|
|
*/
|
|
inline bool line_contains(const std::string &log_file, const std::string_view &substr, bool case_insensitive = false) {
|
|
logging::log_flush();
|
|
|
|
std::ifstream input(log_file);
|
|
if (!input.is_open()) {
|
|
return false;
|
|
}
|
|
|
|
std::string search_str(substr);
|
|
if (case_insensitive) {
|
|
// sonarcloud complains about this, but the solution doesn't work for macOS-12
|
|
std::transform(search_str.begin(), search_str.end(), search_str.begin(), ::tolower);
|
|
}
|
|
|
|
for (std::string line; std::getline(input, line);) {
|
|
line = remove_timestamp_prefix(line);
|
|
if (case_insensitive) {
|
|
// sonarcloud complains about this, but the solution doesn't work for macOS-12
|
|
std::transform(line.begin(), line.end(), line.begin(), ::tolower);
|
|
}
|
|
if (line.find(search_str) != std::string::npos) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
} // namespace log_checker
|