Add logger class, fix include orders

This commit is contained in:
John Sennesael 2021-10-20 07:18:20 -05:00
parent 28b5a2457a
commit f7831afc9d
30 changed files with 250 additions and 154 deletions

View File

@ -43,6 +43,7 @@ add_library(usenetsearch
"src/Filter.cpp"
"src/Indexer.cpp"
"src/IoSocket.cpp"
"src/Logger.cpp"
"src/Serialize.cpp"
"src/SSLConnection.cpp"
"src/StringUtils.cpp"

View File

@ -17,14 +17,14 @@
#pragma once
#include <filesystem>
#include <string>
#include <vector>
#include "usenetsearch/Configuration.h"
#include "usenetsearch/Database.h"
#include "usenetsearch/Filter.h"
#include <filesystem>
#include <string>
#include <vector>
namespace usenetsearch {
enum class CommandLineOptionType

View File

@ -17,12 +17,12 @@
#pragma once
#include "usenetsearch/Except.h"
#include <filesystem>
#include <regex>
#include <string>
#include "usenetsearch/Except.h"
namespace usenetsearch {
struct ConfigurationException: public UsenetSearchException

View File

@ -17,6 +17,10 @@
#pragma once
#include "usenetsearch/Filter.h"
#include "usenetsearch/Serialize.h"
#include "usenetsearch/UsenetClient.h"
#include <codecvt>
#include <cstdint>
#include <filesystem>
@ -26,10 +30,6 @@
#include <mutex>
#include <vector>
#include "usenetsearch/Filter.h"
#include "usenetsearch/Serialize.h"
#include "usenetsearch/UsenetClient.h"
namespace usenetsearch {
class Application;

View File

@ -17,13 +17,13 @@
#pragma once
#include <chrono>
#include <string>
#include <vector>
#include "usenetsearch/Except.h"
#include <netdb.h> // struct addrinfo
#include "usenetsearch/Except.h"
#include <chrono>
#include <string>
#include <vector>
namespace usenetsearch {

View File

@ -17,14 +17,14 @@
#pragma once
#include "usenetsearch/Configuration.h"
#include <codecvt>
#include <locale>
#include <regex>
#include <string>
#include <unordered_map>
#include "usenetsearch/Configuration.h"
namespace usenetsearch {
class Filter

View File

@ -17,16 +17,16 @@
#pragma once
#include <codecvt>
#include <cstdint>
#include <locale>
#include <vector>
#include "usenetsearch/Application.h"
#include "usenetsearch/Filter.h"
#include "usenetsearch/UsenetClient.h"
#include "usenetsearch/ThreadPool.h"
#include <codecvt>
#include <cstdint>
#include <locale>
#include <vector>
namespace usenetsearch {
class SearchResult

View File

@ -0,0 +1,90 @@
/*
Copyright© 2021 John Sennesael
UsenetSearch is Free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UsenetSearch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <type_traits>
#include <memory>
#include <stdexcept>
namespace usenetsearch {
/**
* Simple logging class singleton.
*/
class Logger
{
/// Holds a pointer to the singleton instance.
static std::unique_ptr<Logger> m_instance;
public:
/**
* Constructor.
*/
Logger();
/**
* Destructor.
*/
virtual ~Logger() = default;
/**
* Gets an instance to the logger.
*/
static Logger& get();
/**
* Logs a debug message.
*
* @param[in] msg Message to be logged.
*/
void debug(const std::string& msg) const;
/**
* Logs an info message.
*
* @param[in] msg Message to be logged.
*/
void info(const std::string& msg) const;
/**
* Logs an error message.
*
* @param[in] msg Message to be logged.
*/
void error(const std::string& msg) const;
/**
* Logs an error message and throws.
*
* @tparam EXCEPTION Type of exception to throw.
* @param[in] msg Error message to be logged.
*/
template <typename EXCEPTION>
inline void fatal(const std::string& msg) const
{
static_assert(
std::is_base_of<std::exception, EXCEPTION>::value,
"EXCEPTION must be an std::exception or derrived child.");
error(msg);
throw EXCEPTION(msg);
};
};
} // namespace usenetsearch

View File

@ -17,12 +17,12 @@
#pragma once
#include <memory>
#include "usenetsearch/IoSocket.h"
#include "usenetsearch/TcpConnection.h"
#include <openssl/ssl.h>
#include "usenetsearch/IoSocket.h"
#include "usenetsearch/TcpConnection.h"
#include <memory>
namespace usenetsearch {

View File

@ -17,13 +17,14 @@
#pragma once
#include "usenetsearch/Except.h"
#include <cstdint>
#include <locale>
#include <filesystem>
#include <fstream>
#include <vector>
#include "usenetsearch/Except.h"
namespace usenetsearch {
struct ArticleEntry;

View File

@ -17,14 +17,14 @@
#pragma once
#include "usenetsearch/Except.h"
#include <algorithm>
#include <fstream>
#include <functional>
#include <string>
#include <vector>
#include "usenetsearch/Except.h"
namespace usenetsearch {
struct StringException: public UsenetSearchException

View File

@ -17,12 +17,12 @@
#pragma once
#include <chrono>
#include <string>
#include "usenetsearch/Except.h"
#include "usenetsearch/IoSocket.h"
#include <chrono>
#include <string>
namespace usenetsearch {
struct SocketException: public UsenetSearchException

View File

@ -17,6 +17,9 @@
#pragma once
#include "usenetsearch/SSLConnection.h"
#include "usenetsearch/TcpConnection.h"
#include <cstdint>
#include <codecvt>
#include <fstream>
@ -27,9 +30,6 @@
#include <string>
#include <vector>
#include "usenetsearch/SSLConnection.h"
#include "usenetsearch/TcpConnection.h"
namespace usenetsearch {
class Application;

View File

@ -15,15 +15,15 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/Application.h"
#include "usenetsearch/StringUtils.h"
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/Application.h"
namespace usenetsearch {
void Application::AddBooleanOption(

View File

@ -15,15 +15,15 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem>
#include <fstream>
#include <regex>
#include <string>
#include "usenetsearch/Configuration.h"
#include "usenetsearch/ScopeExit.h"
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/Configuration.h"
#include <filesystem>
#include <fstream>
#include <regex>
#include <string>
namespace usenetsearch {

View File

@ -15,6 +15,14 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/Database.h"
#include "usenetsearch/Application.h"
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/UsenetClient.h"
#include "usenetsearch/ScopeExit.h"
#include "usenetsearch/Serialize.h"
#include <iomanip>
#include <iostream>
#include <chrono>
@ -24,14 +32,6 @@
#include <thread>
#include <vector>
#include "usenetsearch/Application.h"
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/UsenetClient.h"
#include "usenetsearch/ScopeExit.h"
#include "usenetsearch/Serialize.h"
#include "usenetsearch/Database.h"
namespace usenetsearch {
// Database class --------------------------------------------------------------

View File

@ -15,11 +15,11 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/Dns.h"
#include <cerrno>
#include <thread>
#include "usenetsearch/Dns.h"
namespace usenetsearch {
std::vector<struct addrinfo> DnsResolve(

View File

@ -15,13 +15,13 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <iostream>
#include <regex>
#include "usenetsearch/Filter.h"
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/Filter.h"
#include <algorithm>
#include <iostream>
#include <regex>
namespace usenetsearch {

View File

@ -15,11 +15,12 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "usenetsearch/Indexer.h"
#include "usenetsearch/Logger.h"
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/Indexer.h"
#include <iostream>
namespace usenetsearch {
@ -197,9 +198,6 @@ void Indexer::Index(const std::vector<NntpListEntry>& newsgroups)
dbref.get().SetLastIndexedArticle(
groupID, lastArticle
);
std::cout << "Setting last indexed article for newsgroup ID "
<< groupID << " to: "
<< lastArticle << std::endl;
}
std::cout << ".";
std::cout.flush();

View File

@ -15,11 +15,11 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/IoSocket.h"
#include <chrono>
#include <string>
#include "usenetsearch/IoSocket.h"
namespace usenetsearch {
std::chrono::milliseconds IoSocket::ConnectionTimeout() const

54
src/Logger.cpp Normal file
View File

@ -0,0 +1,54 @@
/*
Copyright© 2021 John Sennesael
UsenetSearch is Free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UsenetSearch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/Logger.h"
#include <string>
#include <iostream>
#include <memory>
namespace usenetsearch {
std::unique_ptr<Logger> Logger::m_instance;
Logger::Logger() {}
Logger& Logger::get()
{
if (m_instance == nullptr)
{
m_instance = std::make_unique<Logger>();
}
return *m_instance;
}
void Logger::debug(const std::string& msg) const
{
std::cout << "[debug] " << msg << std::endl << std::flush;
}
void Logger::info(const std::string& msg) const
{
std::cout << "[info] " << msg << std::endl << std::flush;
}
void Logger::error(const std::string& msg) const
{
std::cerr << "[error] " << msg << std::endl << std::flush;
}
} // namespace usenetsearch

View File

@ -15,14 +15,14 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cerrno>
#include <cstring>
#include <memory>
#include "usenetsearch/SSLConnection.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "usenetsearch/SSLConnection.h"
#include <cerrno>
#include <cstring>
#include <memory>
namespace usenetsearch {

View File

@ -13,13 +13,11 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cerrno>
#include <codecvt>
#include <cstdint>
#include <cstring>
#include <locale>
#include <fstream>
#include <vector>
#include "usenetsearch/Serialize.h"
#include "usenetsearch/Database.h"
#include "usenetsearch/ScopeExit.h"
#include "usenetsearch/UsenetClient.h"
#include <fcntl.h>
#include <sys/file.h>
@ -27,11 +25,13 @@
#include <sys/stat.h>
#include <unistd.h>
#include "usenetsearch/Database.h"
#include "usenetsearch/ScopeExit.h"
#include "usenetsearch/UsenetClient.h"
#include "usenetsearch/Serialize.h"
#include <cerrno>
#include <codecvt>
#include <cstdint>
#include <cstring>
#include <locale>
#include <fstream>
#include <vector>
namespace usenetsearch {

View File

@ -15,6 +15,10 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/StringUtils.h"
#include <openssl/md5.h>
#include <algorithm>
#include <codecvt>
#include <cstring>
@ -24,10 +28,6 @@
#include <string>
#include <vector>
#include <openssl/md5.h>
#include "usenetsearch/StringUtils.h"
namespace usenetsearch {
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;

View File

@ -15,6 +15,12 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/TcpConnection.h"
#include "usenetsearch/Dns.h"
#include <unistd.h> // close(), read(), write()
#include <cerrno>
#include <chrono>
#include <cstdint>
@ -22,12 +28,6 @@
#include <string>
#include <thread>
#include <unistd.h> // close(), read(), write()
#include "usenetsearch/Dns.h"
#include "usenetsearch/TcpConnection.h"
namespace usenetsearch {
TcpConnection::~TcpConnection()

View File

@ -15,6 +15,9 @@
#include "usenetsearch/ThreadPool.h"
#include <memory>
#include <mutex>
namespace usenetsearch {
ThreadPool::ThreadPool()
@ -57,71 +60,19 @@ void ThreadPool::Queue(std::function<void()> fn)
// Wait for an available thread.
while (m_threads.size() >= m_maxThreads)
{
//std::lock_guard<std::mutex> threadsLock(m_threadsMutex);
auto foundThread = m_threads.end();
//auto foundStatus = m_threadStates->end();
for (auto it = m_threads.begin(); it != m_threads.end(); ++it)
{
const auto& t = (*it);
if (!t) continue;
//const auto threadID = t->get_id();
//if (threadID == std::thread::id{}) continue;
//ThreadStates::iterator statusIt;
//{
// std::lock_guard<std::mutex> tsl(m_threadStatesMutex);
// statusIt = m_threadStates->find(threadID);
//}
//if (statusIt == m_threadStates->end()) continue;
//bool state = statusIt->second;
//if (state == true)
//{
if (t->joinable()) t->join();
foundThread = it;
// foundStatus = statusIt;
break;
//}
}
if (foundThread != m_threads.end()) m_threads.erase(foundThread);
/*
{
std::lock_guard<std::mutex> tsl(m_threadStatesMutex);
if (foundStatus != m_threadStates->end()) m_threadStates->erase(foundStatus);
}
*/
}
// Spawn a new thread.
auto thread = std::make_unique<std::thread>(fn);
/*
auto thread = std::make_unique<std::thread>(
[](std::function<void()> func, std::mutex& tsm, std::shared_ptr<ThreadStates> ts)
{
if (ts == nullptr) return;
{
std::lock_guard<std::mutex> tsl(tsm);
const auto threadID = std::this_thread::get_id();
if (threadID == std::thread::id{}) return;
ts->emplace(threadID, false);
}
func();
{
std::lock_guard<std::mutex> tsl(tsm);
const auto threadID = std::this_thread::get_id();
if (threadID != std::thread::id{})
{
auto it = ts->find(threadID);
if (it == ts->end())
{
ts->emplace(threadID, true);
}
else
{
(*ts)[threadID] = true;
}
}
}
}, fn, std::ref(m_threadStatesMutex), m_threadStates);
*/
//std::lock_guard<std::mutex> threadsLock(m_threadsMutex);
m_threads.emplace_back(std::move(thread));
}

View File

@ -15,6 +15,12 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include "usenetsearch/UsenetClient.h"
#include "usenetsearch/Application.h"
#include "usenetsearch/Except.h"
#include "usenetsearch/StringUtils.h"
#include <codecvt>
#include <fstream>
#include <locale>
@ -22,12 +28,6 @@
#include <mutex>
#include <string>
#include "usenetsearch/Application.h"
#include "usenetsearch/Except.h"
#include "usenetsearch/StringUtils.h"
#include "usenetsearch/UsenetClient.h"
namespace usenetsearch {
// UsenetClient class ----------------------------------------------------------

View File

@ -15,12 +15,12 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include "usenetsearch/Application.h"
#include "usenetsearch/StringUtils.h"
#include <iostream>
#include <string>
using namespace usenetsearch;
int main(int argc, char* argv[])

View File

@ -1,9 +1,10 @@
#include <iostream>
#include "usenetsearch/Application.h"
#include "usenetsearch/UsenetClient.h"
#include "usenetsearch/Indexer.h"
#include <iostream>
using namespace usenetsearch;
int main(int argc, char* argv[])

View File

@ -15,13 +15,13 @@
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "usenetsearch/Application.h"
#include "usenetsearch/Except.h"
#include "usenetsearch/Indexer.h"
#include "usenetsearch/UsenetClient.h"
#include <iostream>
using namespace usenetsearch;
int main(int argc, char* argv[])