2016-04-20 16:57:49 +08:00
|
|
|
#include "includes.h"
|
2019-09-16 00:09:57 +08:00
|
|
|
|
2020-12-26 19:45:12 +08:00
|
|
|
#ifdef _WIN32
|
2023-09-25 21:08:29 +08:00
|
|
|
#include <windows.h>
|
2020-12-26 19:45:12 +08:00
|
|
|
#else
|
2023-09-25 21:08:29 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2019-09-16 00:09:57 +08:00
|
|
|
#endif
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
void prepare_logdir() {
|
2017-03-28 07:08:18 +08:00
|
|
|
spdlog::drop_all();
|
2016-04-20 16:57:49 +08:00
|
|
|
#ifdef _WIN32
|
2019-10-20 22:55:13 +08:00
|
|
|
system("rmdir /S /Q test_logs");
|
2016-04-20 16:57:49 +08:00
|
|
|
#else
|
2019-10-20 22:40:56 +08:00
|
|
|
auto rv = system("rm -rf test_logs");
|
2023-09-25 21:08:29 +08:00
|
|
|
if (rv != 0) {
|
2019-10-20 22:40:56 +08:00
|
|
|
throw std::runtime_error("Failed to rm -rf test_logs");
|
2018-10-05 20:20:14 +08:00
|
|
|
}
|
2017-03-28 07:08:18 +08:00
|
|
|
#endif
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
std::string file_contents(const std::string &filename) {
|
2020-02-08 00:41:50 +08:00
|
|
|
std::ifstream ifs(filename, std::ios_base::binary);
|
2023-09-25 21:08:29 +08:00
|
|
|
if (!ifs) {
|
2016-04-20 16:57:49 +08:00
|
|
|
throw std::runtime_error("Failed open file ");
|
2018-10-05 20:20:14 +08:00
|
|
|
}
|
2018-03-09 21:26:33 +08:00
|
|
|
return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
std::size_t count_lines(const std::string &filename) {
|
2016-04-20 16:57:49 +08:00
|
|
|
std::ifstream ifs(filename);
|
2023-09-25 21:08:29 +08:00
|
|
|
if (!ifs) {
|
2016-04-20 16:57:49 +08:00
|
|
|
throw std::runtime_error("Failed open file ");
|
2018-10-05 20:20:14 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
std::string line;
|
|
|
|
size_t counter = 0;
|
2018-03-09 21:26:33 +08:00
|
|
|
while (std::getline(ifs, line))
|
2016-04-20 16:57:49 +08:00
|
|
|
counter++;
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
void require_message_count(const std::string &filename, const std::size_t messages) {
|
|
|
|
if (strlen(spdlog::details::os::default_eol) == 0) {
|
2020-02-05 05:23:36 +08:00
|
|
|
REQUIRE(count_lines(filename) == 1);
|
2023-09-25 21:08:29 +08:00
|
|
|
} else {
|
2020-02-05 05:23:36 +08:00
|
|
|
REQUIRE(count_lines(filename) == messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:08:29 +08:00
|
|
|
std::size_t get_filesize(const std::string &filename) {
|
2016-04-20 16:57:49 +08:00
|
|
|
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
|
2023-09-25 21:08:29 +08:00
|
|
|
if (!ifs) {
|
2016-04-20 16:57:49 +08:00
|
|
|
throw std::runtime_error("Failed open file ");
|
2018-10-05 20:20:14 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2016-07-11 23:58:00 +08:00
|
|
|
return static_cast<std::size_t>(ifs.tellg());
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
2017-10-13 07:04:31 +08:00
|
|
|
|
|
|
|
// source: https://stackoverflow.com/a/2072890/192001
|
2023-09-25 21:08:29 +08:00
|
|
|
bool ends_with(std::string const &value, std::string const &ending) {
|
|
|
|
if (ending.size() > value.size()) {
|
2018-03-09 21:26:33 +08:00
|
|
|
return false;
|
2018-10-05 20:20:14 +08:00
|
|
|
}
|
2017-11-06 18:39:04 +08:00
|
|
|
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
2017-10-13 07:04:31 +08:00
|
|
|
}
|
2019-09-15 23:34:29 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2019-09-16 02:01:15 +08:00
|
|
|
// Based on: https://stackoverflow.com/a/37416569/192001
|
2023-09-25 21:08:29 +08:00
|
|
|
std::size_t count_files(const std::string &folder) {
|
2019-09-15 23:49:19 +08:00
|
|
|
size_t counter = 0;
|
2021-01-06 16:55:46 +08:00
|
|
|
WIN32_FIND_DATAA ffd;
|
2019-09-21 23:16:38 +08:00
|
|
|
|
2019-09-16 16:58:51 +08:00
|
|
|
// Start iterating over the files in the folder directory.
|
2019-09-16 16:58:26 +08:00
|
|
|
HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd);
|
2023-09-25 21:08:29 +08:00
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
2019-09-15 23:38:31 +08:00
|
|
|
do // Managed to locate and create an handle to that folder.
|
|
|
|
{
|
2019-09-21 23:16:38 +08:00
|
|
|
if (ffd.cFileName[0] != '.')
|
|
|
|
counter++;
|
2021-01-06 16:55:46 +08:00
|
|
|
} while (::FindNextFileA(hFind, &ffd) != 0);
|
2019-09-15 23:38:31 +08:00
|
|
|
::FindClose(hFind);
|
2023-09-25 21:08:29 +08:00
|
|
|
} else {
|
2019-09-15 23:38:31 +08:00
|
|
|
throw std::runtime_error("Failed open folder " + folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter;
|
|
|
|
}
|
2019-09-15 23:34:29 +08:00
|
|
|
#else
|
|
|
|
// Based on: https://stackoverflow.com/a/2802255/192001
|
2023-09-25 21:08:29 +08:00
|
|
|
std::size_t count_files(const std::string &folder) {
|
2019-09-15 23:34:29 +08:00
|
|
|
size_t counter = 0;
|
|
|
|
DIR *dp = opendir(folder.c_str());
|
2023-09-25 21:08:29 +08:00
|
|
|
if (dp == nullptr) {
|
2019-09-15 23:34:29 +08:00
|
|
|
throw std::runtime_error("Failed open folder " + folder);
|
|
|
|
}
|
|
|
|
|
2020-04-08 23:17:21 +08:00
|
|
|
struct dirent *ep = nullptr;
|
2023-09-25 21:08:29 +08:00
|
|
|
while ((ep = readdir(dp)) != nullptr) {
|
2019-09-15 23:34:29 +08:00
|
|
|
if (ep->d_name[0] != '.')
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
(void)closedir(dp);
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
#endif
|