Skip to content

Commit

Permalink
MinGW-w64 build v.1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaRitz committed Sep 9, 2020
1 parent 95cec15 commit e438e88
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 92 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ add_subdirectory(lib)
add_subdirectory(src)
add_subdirectory(test)

target_link_libraries(tello PUBLIC spdlog)
target_link_libraries(tello PRIVATE ws2_32 ctpl)
target_link_libraries(tello PRIVATE ws2_32 ctpl spdlog)
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ Note: The target 'tello_test' cannot be built with this option on,<br>
because there are some usages, which are not exported (e.g. command_test.cpp).<br>
Use the target 'shared_linking_test' instead.

### OS & Compiler
OS
- Windows

Compiler
- nmake
- mingw/g++

## Third-party libs
- Logging: spdlog (https://github.com/gabime/spdlog)
- Thread-pooling: ctpl (https://github.com/vit-vit/CTPL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once

#include <string>
#include <spdlog/spdlog.h>
#include <spdlog/logger.h>
#include "../macro_definition.hpp"

using std::string;
using spdlog::logger;

namespace tello {

class Logger;

struct EXPORT LoggerSettings {
public:
LoggerSettings(string commandFileLocation, string videoFileLocation,
Expand All @@ -25,16 +25,13 @@ namespace tello {
STATUS
};

class EXPORT Logger {
class EXPORT LoggerInterface {
public:
Logger() = delete;
[[nodiscard]]
static std::shared_ptr<logger> get(const LoggerType& loggerType);
static void initialize(const LoggerSettings& settings);
LoggerInterface() = delete;

private:
static std::shared_ptr<logger> _commandLogger;
static std::shared_ptr<logger> _videoLogger;
static std::shared_ptr<logger> _statusLogger;
static void info(const LoggerType& loggerType, const string& message, const string& params...);
static void error(const LoggerType& loggerType, const string& message, const string& params...);
static void warn(const LoggerType& loggerType, const string& message, const string& params...);
static void initialize(const LoggerSettings& settings);
};
}
1 change: 1 addition & 0 deletions src/tello/command/validator_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <vector>
#include <stdexcept>

using std::string;
using std::vector;
Expand Down
18 changes: 9 additions & 9 deletions src/tello/connection/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ bool tello::Network::connect() {
_connectionMutex.lock();
_commandConnection = command.value();
_connectionMutex.unlock();
Logger::get(LoggerType::COMMAND)->info(string("Command-Port connected"));
LoggerInterface::info(LoggerType::COMMAND, string("Command-Port connected"), "");
} else {
Logger::get(LoggerType::COMMAND)->info(string("Command-Port not connected"));
LoggerInterface::info(LoggerType::COMMAND, string("Command-Port not connected"), "");
}

if (status) {
_connectionMutex.lock();
_statusConnection = status.value();
_connectionMutex.unlock();
Logger::get(LoggerType::STATUS)->info(string("Status-Port connected"));
LoggerInterface::info(LoggerType::STATUS, string("Status-Port connected"), "");
} else {
Logger::get(LoggerType::STATUS)->info(string("Status-Port not connected"));
LoggerInterface::info(LoggerType::STATUS, string("Status-Port not connected"), "");
}

if (video) {
_connectionMutex.lock();
_videoConnection = video.value();
_connectionMutex.unlock();
Logger::get(LoggerType::VIDEO)->info(string("Video-Port connected"));
LoggerInterface::info(LoggerType::VIDEO, string("Video-Port connected"), "");
} else {
Logger::get(LoggerType::VIDEO)->info(string("Video-Port not connected"));
LoggerInterface::info(LoggerType::VIDEO, string("Video-Port not connected"), "");
}

return isConnected;
Expand Down Expand Up @@ -86,7 +86,7 @@ void tello::Network::disconnect() {
optional<ConnectionData>
tello::Network::connectToPort(unsigned short port, const ConnectionData& data, const LoggerType& loggerType) {
if (data._fileDescriptor != -1) {
Logger::get(loggerType)->warn(string("Is already connected!"));
LoggerInterface::warn(loggerType, string("Is already connected!"), "");
return std::make_optional<ConnectionData>(data);
}

Expand All @@ -96,10 +96,10 @@ tello::Network::connectToPort(unsigned short port, const ConnectionData& data, c
void tello::Network::disconnect(ConnectionData& connectionData, const LoggerType& loggerType) {
bool disconnected = networkInterface->disconnect(connectionData._fileDescriptor);
if (disconnected) {
Logger::get(loggerType)->info(string("Socket closed"));
LoggerInterface::info(loggerType, string("Socket closed"), "");
_commandConnection._fileDescriptor = -1;
} else {
Logger::get(loggerType)->error(string("Socket not closed"));
LoggerInterface::error(loggerType, string("Socket not closed"), "");
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/tello/connection/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include "tello/native/network_interface.hpp"
#include "tello/video_analyzer.hpp"
#include "../command/command.hpp"
#include "tello/logger/logger.hpp"
#include "tello/logger/logger_interface.hpp"
#include "udp_command_listener.hpp"
#include "../thread/thread_pool.hpp"
#include <tello/tello.hpp>
#include <vector>

using tello::ConnectionData;
Expand All @@ -21,7 +22,7 @@ using std::unique_ptr;
using tello::NetworkInterface;
using tello::VideoAnalyzer;
using tello::Command;
using tello::Logger;
using tello::LoggerInterface;
using tello::UdpCommandListener;
using std::vector;
using tello::threading::Threadpool;
Expand Down Expand Up @@ -84,12 +85,12 @@ namespace tello {
unordered_map<ip_address, future<CommandResponse>> responses{};
string errorMessage = command.validate();
if (!errorMessage.empty()) {
Logger::get(LoggerType::COMMAND)->error(
LoggerInterface::error(LoggerType::COMMAND,
string("Command of type [{}] is not valid"), NAMES.find(command.type())->second);
Logger::get(LoggerType::COMMAND)->error(string("Message is: {}"), errorMessage);
LoggerInterface::error(LoggerType::COMMAND, string("Message is: {}"), errorMessage);

for (auto tello : tellos) {
CommandResponse& response = CommandResponse{Status::FAIL};
CommandResponse response = CommandResponse{Status::FAIL};
promise<CommandResponse> errorProm{};
errorProm.set_value(response);
responses[tello.first] = std::move(errorProm.get_future());
Expand All @@ -106,7 +107,7 @@ namespace tello {
commandString);
_connectionMutex.unlock_shared();
if (result == SEND_ERROR_CODE) {
Logger::get(LoggerType::COMMAND)->info(
LoggerInterface::info(LoggerType::COMMAND,
string("Command of type [{}] is not sent cause of socket error!"),
NAMES.find(command.type())->second);

Expand All @@ -117,14 +118,15 @@ namespace tello {

tellos.erase(tello->first);
} else {
Logger::get(LoggerType::COMMAND)->info(
string("Command of type [{0}] is sent to {1:x}!"), NAMES.find(command.type())->second,
tello->second->_clientaddr._ip);
LoggerInterface::info(LoggerType::COMMAND,
string("Command of type [{0}] is sent to {1}!"), NAMES.find(command.type())->second,
std::to_string(tello->second->_clientaddr._ip));
}
}

if (command.hasResponse()) {
vector<ip_address> openResponses{};
openResponses.reserve(tellos.size());
for(auto aTello : tellos) {
openResponses.push_back(aTello.first);
}
Expand Down
12 changes: 6 additions & 6 deletions src/tello/connection/udp_command_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void tello::UdpCommandListener::listen(const tello::ConnectionData& connectionDa
connectionMutex.unlock_shared();
continue;
} else if (isFirstAccessToFileDescriptor) {
Logger::get(LoggerType::COMMAND)->info(string("Start listen to port {0:d}"),
connectionData._networkData._port);
LoggerInterface::info(LoggerType::COMMAND, string("Start listen to port {0}"),
std::to_string(connectionData._networkData._port));
isFirstAccessToFileDescriptor = false;
}

Expand All @@ -76,8 +76,8 @@ void tello::UdpCommandListener::listen(const tello::ConnectionData& connectionDa
bool correctSender = sender != mapping.end();

if (!correctSender) {
Logger::get(LoggerType::COMMAND)->error(
string("Answer from wrong tello received {0:x}"), senderIp);
LoggerInterface::error(LoggerType::COMMAND,
string("Answer from wrong tello received {0}"), std::to_string(senderIp));
} else {
string answer = networkResponse.response();
shared_ptr<ResponseMapping> response = sender->second.at(0);
Expand All @@ -98,8 +98,8 @@ void tello::UdpCommandListener::listen(const tello::ConnectionData& connectionDa
}
}

Logger::get(LoggerType::COMMAND)->info(string("Stop listen to port {0:d}"),
connectionData._networkData._port);
LoggerInterface::info(LoggerType::COMMAND, string("Stop listen to port {0}"),
std::to_string(connectionData._networkData._port));
}

time_t tello::UdpCommandListener::currentTime() {
Expand Down
4 changes: 2 additions & 2 deletions src/tello/connection/udp_command_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <thread>
#include <future>
#include <string>
#include "tello/logger/logger.hpp"
#include "tello/logger/logger_interface.hpp"
#include "tello/native/network_interface.hpp"
#include <mutex>
#include <shared_mutex>
Expand All @@ -19,7 +19,7 @@ using std::unordered_map;
using std::thread;
using std::promise;
using std::future;
using tello::Logger;
using tello::LoggerInterface;
using tello::LoggerType;
using std::shared_ptr;
using std::vector;
Expand Down
18 changes: 9 additions & 9 deletions src/tello/connection/udp_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <thread>
#include <future>
#include <string>
#include "tello/logger/logger.hpp"
#include "tello/logger/logger_interface.hpp"
#include "tello/native/network_interface.hpp"
#include <mutex>
#include <shared_mutex>
Expand All @@ -15,7 +15,7 @@ using std::unordered_map;
using std::thread;
using std::promise;
using std::future;
using tello::Logger;
using tello::LoggerInterface;
using tello::LoggerType;
using std::shared_ptr;

Expand Down Expand Up @@ -56,12 +56,12 @@ namespace tello {
connectionMutex.unlock_shared();
continue;
} else if (isFirstAccessToFileDescriptor) {
Logger::get(loggerType)->info(string("Start listen to port {0:d}"),
connectionData._networkData._port);
LoggerInterface::info(loggerType, string("Start listen to port {}"),
std::to_string(connectionData._networkData._port));
isFirstAccessToFileDescriptor = false;
}

NetworkResponse& networkResponse = networkInterface->read(connectionData._fileDescriptor);
NetworkResponse networkResponse = networkInterface->read(connectionData._fileDescriptor);
connectionMutex.unlock_shared();

telloMappingMutex.lock_shared();
Expand All @@ -70,14 +70,14 @@ namespace tello {
if (telloIt != telloMapping.end()) {
invoke(networkResponse, telloIt->second);
} else if (networkResponse._length > 0) {
Logger::get(loggerType)->warn(string("Received data {0} from unknown Tello {1:x}"),
networkResponse.response(), networkResponse._sender._ip);
LoggerInterface::warn(loggerType, string("Received data {0} from unknown Tello {1}"),
networkResponse.response(), std::to_string(networkResponse._sender._ip));
}
telloMappingMutex.unlock_shared();
}

Logger::get(loggerType)->info(string("Stop listen to port {0:d}"),
connectionData._networkData._port);
LoggerInterface::info(loggerType, string("Stop listen to port {0}"),
std::to_string(connectionData._networkData._port));
}
};
}
6 changes: 5 additions & 1 deletion src/tello/logger/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
target_sources(${PROJECT_NAME} PUBLIC
${TELLO_INCLUDE}/tello/logger/logger.hpp
${TELLO_INCLUDE}/tello/logger/logger_interface.hpp

PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/logger_interface.cpp
${CMAKE_CURRENT_SOURCE_DIR}/logger.hpp
${CMAKE_CURRENT_SOURCE_DIR}/logger.cpp)
30 changes: 18 additions & 12 deletions src/tello/logger/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#include <tello/logger/logger.hpp>
#include "logger.hpp"
#include <spdlog/logger.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <tello/logger/logger_interface.hpp>

std::shared_ptr<logger> tello::Logger::_commandLogger = spdlog::basic_logger_mt("command_logger_init", "./log/command_log_init.log");
std::shared_ptr<logger> tello::Logger::_statusLogger = spdlog::basic_logger_mt("status_logger_init", "./log/status_log_init.log");
std::shared_ptr<logger> tello::Logger::_videoLogger = spdlog::basic_logger_mt("video_logger_init", "./log/video_log_init.log");

tello::LoggerSettings::LoggerSettings(string commandFileLocation, string videoFileLocation,
string statusFileLocation) : _commandFileLocation(std::move(commandFileLocation)),
_videoFileLocation(std::move(videoFileLocation)),
_statusFileLocation(std::move(statusFileLocation)) {}

void tello::Logger::initialize(const tello::LoggerSettings& settings) {
_commandLogger = spdlog::basic_logger_mt("command_logger", settings._commandFileLocation);
_videoLogger = spdlog::basic_logger_mt("video_logger", settings._videoFileLocation);
Expand All @@ -19,14 +16,23 @@ void tello::Logger::initialize(const tello::LoggerSettings& settings) {
_statusLogger->flush_on(spdlog::level::err);
}

std::shared_ptr<logger> tello::Logger::get(const tello::LoggerType& loggerType) {

if (_commandLogger == nullptr || _videoLogger == nullptr || _statusLogger == nullptr) {
_commandLogger = spdlog::basic_logger_mt("command_logger_temp", "./log/command_log_temp.log");
_videoLogger = spdlog::basic_logger_mt("video_logger_temp", "./log/video_log_temp.log");
_statusLogger = spdlog::basic_logger_mt("status_logger_temp", "./log/status_log_temp.log");
}
void tello::Logger::info(const LoggerType& loggerType, const string& message, const string& params...) {
auto logger = tello::Logger::logger(loggerType);
logger->info(message, params);
}

void tello::Logger::error(const LoggerType& loggerType, const string& message, const string& params...) {
auto logger = tello::Logger::logger(loggerType);
logger->error(message, params);
}

void tello::Logger::warn(const LoggerType& loggerType, const string& message, const string& params...) {
auto logger = tello::Logger::logger(loggerType);
logger->warn(message, params);
}

std::shared_ptr<logger> tello::Logger::logger(const LoggerType& loggerType) {
switch (loggerType) {
case LoggerType::STATUS :
return _statusLogger;
Expand Down
35 changes: 35 additions & 0 deletions src/tello/logger/logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <string>
#include <memory>

using std::string;

namespace spdlog {
class logger;
}

using spdlog::logger;

namespace tello {

enum class LoggerType;
class LoggerSettings;

class Logger {
public:
Logger() = delete;

static void info(const LoggerType& loggerType, const string& message, const string& params...);
static void error(const LoggerType& loggerType, const string& message, const string& params...);
static void warn(const LoggerType& loggerType, const string& message, const string& params...);
static void initialize(const LoggerSettings& settings);

private:
static std::shared_ptr<logger> _commandLogger;
static std::shared_ptr<logger> _videoLogger;
static std::shared_ptr<logger> _statusLogger;

static std::shared_ptr<logger> logger(const LoggerType& loggerType);
};
}
Loading

0 comments on commit e438e88

Please sign in to comment.