diff --git a/src/Base64.h b/include/support/Base64.h similarity index 83% rename from src/Base64.h rename to include/support/Base64.h index a86e699be2..40cb2528f0 100644 --- a/src/Base64.h +++ b/include/support/Base64.h @@ -5,19 +5,19 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_BASE64_H_ -#define NT_BASE64_H_ +#ifndef WPIUTIL_SUPPORT_BASE64_H_ +#define WPIUTIL_SUPPORT_BASE64_H_ #include #include #include "llvm/StringRef.h" -namespace nt { +namespace wpi { std::size_t Base64Decode(llvm::StringRef encoded, std::string* plain); void Base64Encode(llvm::StringRef plain, std::string* encoded); -} // namespace nt +} // namespace wpi -#endif // NT_BASE64_H_ +#endif // WPIUTIL_SUPPORT_BASE64_H_ diff --git a/src/support/ConcurrentQueue.h b/include/support/ConcurrentQueue.h similarity index 90% rename from src/support/ConcurrentQueue.h rename to include/support/ConcurrentQueue.h index fa99477511..abe9580f7f 100644 --- a/src/support/ConcurrentQueue.h +++ b/include/support/ConcurrentQueue.h @@ -4,14 +4,16 @@ // - see < http://opensource.org/licenses/BSD-2-Clause> // -#ifndef NT_SUPPORT_CONCURRENT_QUEUE_H_ -#define NT_SUPPORT_CONCURRENT_QUEUE_H_ +#ifndef WPIUTIL_SUPPORT_CONCURRENT_QUEUE_H_ +#define WPIUTIL_SUPPORT_CONCURRENT_QUEUE_H_ #include #include #include #include +namespace wpi { + template class ConcurrentQueue { public: @@ -76,4 +78,6 @@ class ConcurrentQueue { std::condition_variable cond_; }; -#endif // NT_SUPPORT_CONCURRENT_QUEUE_H_ +} // namespace wpi + +#endif // WPIUTIL_SUPPORT_CONCURRENT_QUEUE_H_ diff --git a/include/support/Logger.h b/include/support/Logger.h new file mode 100644 index 0000000000..44436c70b3 --- /dev/null +++ b/include/support/Logger.h @@ -0,0 +1,82 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2015. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#ifndef WPIUTIL_SUPPORT_LOGGER_H_ +#define WPIUTIL_SUPPORT_LOGGER_H_ + +#include +#include +#include + +namespace wpi { + +enum LogLevel { + WPI_LOG_CRITICAL = 50, + WPI_LOG_ERROR = 40, + WPI_LOG_WARNING = 30, + WPI_LOG_INFO = 20, + WPI_LOG_DEBUG = 10, + WPI_LOG_DEBUG1 = 9, + WPI_LOG_DEBUG2 = 8, + WPI_LOG_DEBUG3 = 7, + WPI_LOG_DEBUG4 = 6 +}; + +class Logger { + public: + typedef std::function LogFunc; + + void SetLogger(LogFunc func) { m_func = func; } + + void set_min_level(unsigned int level) { m_min_level = level; } + unsigned int min_level() const { return m_min_level; } + + void Log(unsigned int level, const char* file, unsigned int line, + const char* msg) { + if (!m_func || level < m_min_level) return; + m_func(level, file, line, msg); + } + + bool HasLogger() const { return m_func != nullptr; } + + private: + LogFunc m_func; + unsigned int m_min_level = 20; +}; + +#define WPI_LOG(logger_inst, level, x) \ + do { \ + ::wpi::Logger& WPI_logger_ = logger_inst; \ + if (WPI_logger_.min_level() <= level && WPI_logger_.HasLogger()) { \ + std::ostringstream oss; \ + oss << x; \ + WPI_logger_.Log(level, __FILE__, __LINE__, oss.str().c_str()); \ + } \ + } while (0) + +#define WPI_ERROR(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_ERROR, x) +#define WPI_WARNING(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_WARNING, x) +#define WPI_INFO(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_INFO, x) + +#ifdef NDEBUG +#define WPI_DEBUG(inst, x) do {} while (0) +#define WPI_DEBUG1(inst, x) do {} while (0) +#define WPI_DEBUG2(inst, x) do {} while (0) +#define WPI_DEBUG3(inst, x) do {} while (0) +#define WPI_DEBUG4(inst, x) do {} while (0) +#else +#define WPI_DEBUG(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG, x) +#define WPI_DEBUG1(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG1, x) +#define WPI_DEBUG2(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG2, x) +#define WPI_DEBUG3(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG3, x) +#define WPI_DEBUG4(inst, x) WPI_LOG(inst, ::wpi::WPI_LOG_DEBUG4, x) +#endif + +} // namespace wpi + +#endif // WPIUTIL_SUPPORT_LOGGER_H_ diff --git a/src/SafeThread.h b/include/support/SafeThread.h similarity index 94% rename from src/SafeThread.h rename to include/support/SafeThread.h index a4973bbc83..2edbe7ae3c 100644 --- a/src/SafeThread.h +++ b/include/support/SafeThread.h @@ -5,15 +5,15 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_SAFETHREAD_H_ -#define NT_SAFETHREAD_H_ +#ifndef WPIUTIL_SUPPORT_SAFETHREAD_H_ +#define WPIUTIL_SUPPORT_SAFETHREAD_H_ #include #include #include #include -namespace nt { +namespace wpi { // Base class for SafeThreadOwner threads. class SafeThread { @@ -88,6 +88,6 @@ class SafeThreadOwner : public detail::SafeThreadOwnerBase { Proxy GetThread() { return Proxy(detail::SafeThreadOwnerBase::GetThread()); } }; -} // namespace nt +} // namespace wpi -#endif // NT_SAFETHREAD_H_ +#endif // WPIUTIL_SUPPORT_SAFETHREAD_H_ diff --git a/src/atomic_static.h b/include/support/atomic_static.h similarity index 93% rename from src/atomic_static.h rename to include/support/atomic_static.h index b00ccdac0e..146a1de478 100644 --- a/src/atomic_static.h +++ b/include/support/atomic_static.h @@ -5,8 +5,8 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_ATOMIC_STATIC_H_ -#define NT_ATOMIC_STATIC_H_ +#ifndef WPIUTIL_SUPPORT_ATOMIC_STATIC_H_ +#define WPIUTIL_SUPPORT_ATOMIC_STATIC_H_ #if !defined(_MSC_VER) || (_MSC_VER >= 1900) @@ -46,4 +46,4 @@ #endif -#endif // NT_ATOMIC_STATIC_H_ +#endif // WPIUTIL_SUPPORT_ATOMIC_STATIC_H_ diff --git a/src/leb128.h b/include/support/leb128.h similarity index 85% rename from src/leb128.h rename to include/support/leb128.h index 73ca24526d..de08a23a32 100644 --- a/src/leb128.h +++ b/include/support/leb128.h @@ -5,14 +5,14 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_LEB128_H_ -#define NT_LEB128_H_ +#ifndef WPIUTIL_SUPPORT_LEB128_H_ +#define WPIUTIL_SUPPORT_LEB128_H_ #include #include "llvm/SmallVector.h" -namespace nt { +namespace wpi { class raw_istream; @@ -21,6 +21,6 @@ std::size_t WriteUleb128(llvm::SmallVectorImpl& dest, unsigned long val); std::size_t ReadUleb128(const char* addr, unsigned long* ret); bool ReadUleb128(raw_istream& is, unsigned long* ret); -} // namespace nt +} // namespace wpi -#endif // NT_LEB128_H_ +#endif // WPIUTIL_SUPPORT_LEB128_H_ diff --git a/src/raw_istream.h b/include/support/raw_istream.h similarity index 87% rename from src/raw_istream.h rename to include/support/raw_istream.h index f8adc23403..aeca34fe88 100644 --- a/src/raw_istream.h +++ b/include/support/raw_istream.h @@ -5,12 +5,12 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_RAW_ISTREAM_H_ -#define NT_RAW_ISTREAM_H_ +#ifndef WPIUTIL_SUPPORT_RAW_ISTREAM_H_ +#define WPIUTIL_SUPPORT_RAW_ISTREAM_H_ #include -namespace nt { +namespace wpi { class raw_istream { public: @@ -35,6 +35,6 @@ class raw_mem_istream : public raw_istream { std::size_t m_left; }; -} // namespace nt +} // namespace wpi -#endif // NT_RAW_ISTREAM_H_ +#endif // WPIUTIL_SUPPORT_RAW_ISTREAM_H_ diff --git a/src/raw_socket_istream.h b/include/support/raw_socket_istream.h similarity index 80% rename from src/raw_socket_istream.h rename to include/support/raw_socket_istream.h index 91bcc1fae2..78b0517325 100644 --- a/src/raw_socket_istream.h +++ b/include/support/raw_socket_istream.h @@ -5,14 +5,13 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_RAW_SOCKET_ISTREAM_H_ -#define NT_RAW_SOCKET_ISTREAM_H_ - -#include "raw_istream.h" +#ifndef WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_ +#define WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_ +#include "support/raw_istream.h" #include "tcpsockets/NetworkStream.h" -namespace nt { +namespace wpi { class raw_socket_istream : public raw_istream { public: @@ -27,6 +26,6 @@ class raw_socket_istream : public raw_istream { int m_timeout; }; -} // namespace nt +} // namespace wpi -#endif // NT_RAW_SOCKET_ISTREAM_H_ +#endif // WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_ diff --git a/src/support/timestamp.h b/include/support/timestamp.h similarity index 76% rename from src/support/timestamp.h rename to include/support/timestamp.h index 215aa88429..526d6e5dcd 100644 --- a/src/support/timestamp.h +++ b/include/support/timestamp.h @@ -4,25 +4,25 @@ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef NT_SUPPORT_TIMESTAMP_H_ -#define NT_SUPPORT_TIMESTAMP_H_ +#ifndef WPIUTIL_SUPPORT_TIMESTAMP_H_ +#define WPIUTIL_SUPPORT_TIMESTAMP_H_ #ifdef __cplusplus extern "C" { #endif -unsigned long long NT_Now(void); +unsigned long long WPI_Now(void); #ifdef __cplusplus } #endif #ifdef __cplusplus -namespace nt { +namespace wpi { unsigned long long Now(); -} // namespace nt +} // namespace wpi #endif -#endif // NT_SUPPORT_TIMESTAMP_H_ +#endif // WPIUTIL_SUPPORT_TIMESTAMP_H_ diff --git a/src/tcpsockets/NetworkAcceptor.h b/include/tcpsockets/NetworkAcceptor.h similarity index 79% rename from src/tcpsockets/NetworkAcceptor.h rename to include/tcpsockets/NetworkAcceptor.h index 4702b7fafb..f4d91204b3 100644 --- a/src/tcpsockets/NetworkAcceptor.h +++ b/include/tcpsockets/NetworkAcceptor.h @@ -5,10 +5,12 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef TCPSOCKETS_NETWORKACCEPTOR_H_ -#define TCPSOCKETS_NETWORKACCEPTOR_H_ +#ifndef WPIUTIL_TCPSOCKETS_NETWORKACCEPTOR_H_ +#define WPIUTIL_TCPSOCKETS_NETWORKACCEPTOR_H_ -#include "NetworkStream.h" +#include "tcpsockets/NetworkStream.h" + +namespace wpi { class NetworkAcceptor { public: @@ -23,4 +25,6 @@ class NetworkAcceptor { NetworkAcceptor& operator=(const NetworkAcceptor&) = delete; }; -#endif // TCPSOCKETS_NETWORKACCEPTOR_H_ +} // namespace wpi + +#endif // WPIUTIL_TCPSOCKETS_NETWORKACCEPTOR_H_ diff --git a/src/tcpsockets/NetworkStream.h b/include/tcpsockets/NetworkStream.h similarity index 87% rename from src/tcpsockets/NetworkStream.h rename to include/tcpsockets/NetworkStream.h index 63aedb4f2f..f7dfe4533e 100644 --- a/src/tcpsockets/NetworkStream.h +++ b/include/tcpsockets/NetworkStream.h @@ -5,13 +5,15 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef TCPSOCKETS_NETWORKSTREAM_H_ -#define TCPSOCKETS_NETWORKSTREAM_H_ +#ifndef WPIUTIL_TCPSOCKETS_NETWORKSTREAM_H_ +#define WPIUTIL_TCPSOCKETS_NETWORKSTREAM_H_ #include #include "llvm/StringRef.h" +namespace wpi { + class NetworkStream { public: NetworkStream() = default; @@ -36,4 +38,6 @@ class NetworkStream { NetworkStream& operator=(const NetworkStream&) = delete; }; -#endif // TCPSOCKETS_NETWORKSTREAM_H_ +} // namespace wpi + +#endif // WPIUTIL_TCPSOCKETS_NETWORKSTREAM_H_ diff --git a/src/tcpsockets/SocketError.h b/include/tcpsockets/SocketError.h similarity index 83% rename from src/tcpsockets/SocketError.h rename to include/tcpsockets/SocketError.h index 267e8dae80..3d87b2d5c9 100644 --- a/src/tcpsockets/SocketError.h +++ b/include/tcpsockets/SocketError.h @@ -5,8 +5,8 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#ifndef TCPSOCKETS_SOCKETERROR_H_ -#define TCPSOCKETS_SOCKETERROR_H_ +#ifndef WPIUTIL_TCPSOCKETS_SOCKETERROR_H_ +#define WPIUTIL_TCPSOCKETS_SOCKETERROR_H_ #include @@ -16,7 +16,7 @@ #include #endif -namespace tcpsockets { +namespace wpi { static inline int SocketErrno() { #ifdef _WIN32 @@ -32,6 +32,6 @@ static inline std::string SocketStrerror() { return SocketStrerror(SocketErrno()); } -} // namespace tcpsockets +} // namespace wpi -#endif // TCPSOCKETS_SOCKETERROR_H_ +#endif // WPIUTIL_TCPSOCKETS_SOCKETERROR_H_ diff --git a/src/tcpsockets/TCPAcceptor.h b/include/tcpsockets/TCPAcceptor.h similarity index 77% rename from src/tcpsockets/TCPAcceptor.h rename to include/tcpsockets/TCPAcceptor.h index d6a6ccc366..99403a3aca 100644 --- a/src/tcpsockets/TCPAcceptor.h +++ b/include/tcpsockets/TCPAcceptor.h @@ -21,15 +21,19 @@ limitations under the License. */ -#ifndef TCPSOCKETS_TCPACCEPTOR_H_ -#define TCPSOCKETS_TCPACCEPTOR_H_ +#ifndef WPIUTIL_TCPSOCKETS_TCPACCEPTOR_H_ +#define WPIUTIL_TCPSOCKETS_TCPACCEPTOR_H_ #include #include #include -#include "NetworkAcceptor.h" -#include "TCPStream.h" +#include "tcpsockets/NetworkAcceptor.h" +#include "tcpsockets/TCPStream.h" + +namespace wpi { + +class Logger; class TCPAcceptor : public NetworkAcceptor { int m_lsd; @@ -37,9 +41,10 @@ class TCPAcceptor : public NetworkAcceptor { std::string m_address; bool m_listening; std::atomic_bool m_shutdown; + Logger& m_logger; public: - TCPAcceptor(int port, const char* address); + TCPAcceptor(int port, const char* address, Logger& logger); ~TCPAcceptor(); int start() override; @@ -47,4 +52,6 @@ class TCPAcceptor : public NetworkAcceptor { std::unique_ptr accept() override; }; -#endif +} // namespace wpi + +#endif // WPIUTIL_TCPSOCKETS_TCPACCEPTOR_H_ diff --git a/src/tcpsockets/TCPConnector.h b/include/tcpsockets/TCPConnector.h similarity index 77% rename from src/tcpsockets/TCPConnector.h rename to include/tcpsockets/TCPConnector.h index ebac8590ec..7afb0446af 100644 --- a/src/tcpsockets/TCPConnector.h +++ b/include/tcpsockets/TCPConnector.h @@ -21,17 +21,24 @@ limitations under the License */ -#ifndef TCPSOCKETS_TCPCONNECTOR_H_ -#define TCPSOCKETS_TCPCONNECTOR_H_ +#ifndef WPIUTIL_TCPSOCKETS_TCPCONNECTOR_H_ +#define WPIUTIL_TCPSOCKETS_TCPCONNECTOR_H_ #include -#include "NetworkStream.h" +#include "tcpsockets/NetworkStream.h" + +namespace wpi { + +class Logger; class TCPConnector { public: static std::unique_ptr connect(const char* server, int port, + Logger& logger, int timeout = 0); }; -#endif +} // namespace wpi + +#endif // WPIUTIL_TCPSOCKETS_TCPCONNECTOR_H_ diff --git a/src/tcpsockets/TCPStream.h b/include/tcpsockets/TCPStream.h similarity index 87% rename from src/tcpsockets/TCPStream.h rename to include/tcpsockets/TCPStream.h index 21ef6fdbed..e7bb2f0d1a 100644 --- a/src/tcpsockets/TCPStream.h +++ b/include/tcpsockets/TCPStream.h @@ -21,8 +21,8 @@ limitations under the License. */ -#ifndef TCPSOCKETS_TCPSTREAM_H_ -#define TCPSOCKETS_TCPSTREAM_H_ +#ifndef WPIUTIL_TCPSOCKETS_TCPSTREAM_H_ +#define WPIUTIL_TCPSOCKETS_TCPSTREAM_H_ #include #include @@ -33,7 +33,11 @@ #include #endif -#include "NetworkStream.h" +#include "tcpsockets/NetworkStream.h" + +struct sockaddr_in; + +namespace wpi { class TCPStream : public NetworkStream { int m_sd; @@ -60,8 +64,10 @@ class TCPStream : public NetworkStream { private: bool WaitForReadEvent(int timeout); - TCPStream(int sd, struct sockaddr_in* address); + TCPStream(int sd, sockaddr_in* address); TCPStream() = delete; }; +} // namespace wpi + #endif diff --git a/java/lib/NetworkTablesJNI.cpp b/java/lib/NetworkTablesJNI.cpp index 5da7c81dc6..8d52f595fe 100644 --- a/java/lib/NetworkTablesJNI.cpp +++ b/java/lib/NetworkTablesJNI.cpp @@ -9,8 +9,8 @@ #include "edu_wpi_first_wpilibj_networktables_NetworkTablesJNI.h" #include "ntcore.h" -#include "atomic_static.h" -#include "SafeThread.h" +#include "support/atomic_static.h" +#include "support/SafeThread.h" #include "llvm/ConvertUTF.h" #include "llvm/SmallString.h" #include "llvm/SmallVector.h" @@ -1427,7 +1427,7 @@ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_networktables_NetworkTablesJN // Instead, this class attaches just once. When a hardware notification // occurs, a condition variable wakes up this thread and this thread actually // makes the call into Java. -class LoggerThreadJNI : public nt::SafeThread { +class LoggerThreadJNI : public wpi::SafeThread { public: void Main(); @@ -1445,7 +1445,7 @@ class LoggerThreadJNI : public nt::SafeThread { jmethodID m_mid; }; -class LoggerJNI : public nt::SafeThreadOwner { +class LoggerJNI : public wpi::SafeThreadOwner { public: static LoggerJNI& GetInstance() { ATOMIC_STATIC(LoggerJNI, instance); diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 8d17a1635a..470122d3f6 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -10,26 +10,28 @@ #include #include +#include "Log.h" #include "tcpsockets/TCPAcceptor.h" #include "tcpsockets/TCPConnector.h" -#include "Log.h" using namespace nt; ATOMIC_STATIC_INIT(Dispatcher) -void Dispatcher::StartServer(StringRef persist_filename, +void Dispatcher::StartServer(llvm::StringRef persist_filename, const char* listen_address, unsigned int port) { - DispatcherBase::StartServer(persist_filename, - std::unique_ptr(new TCPAcceptor( - static_cast(port), listen_address))); + DispatcherBase::StartServer( + persist_filename, + std::unique_ptr(new wpi::TCPAcceptor( + static_cast(port), listen_address, Logger::GetInstance()))); } void Dispatcher::StartClient(const char* server_name, unsigned int port) { std::string server_name_copy(server_name); - DispatcherBase::StartClient([=]() -> std::unique_ptr { - return TCPConnector::connect(server_name_copy.c_str(), - static_cast(port), 1); + DispatcherBase::StartClient([=]() -> std::unique_ptr { + return wpi::TCPConnector::connect(server_name_copy.c_str(), + static_cast(port), + Logger::GetInstance(), 1); }); } @@ -39,9 +41,10 @@ void Dispatcher::StartClient( for (const auto& server : servers) { std::string server_name(server.first); unsigned int port = server.second; - connectors.emplace_back([=]() -> std::unique_ptr { - return TCPConnector::connect(server_name.c_str(), - static_cast(port), 1); + connectors.emplace_back([=]() -> std::unique_ptr { + return wpi::TCPConnector::connect(server_name.c_str(), + static_cast(port), + Logger::GetInstance(), 1); }); } DispatcherBase::StartClient(std::move(connectors)); @@ -61,8 +64,9 @@ DispatcherBase::~DispatcherBase() { Stop(); } -void DispatcherBase::StartServer(StringRef persist_filename, - std::unique_ptr acceptor) { +void DispatcherBase::StartServer( + StringRef persist_filename, + std::unique_ptr acceptor) { { std::lock_guard lock(m_user_mutex); if (m_active) return; diff --git a/src/Dispatcher.h b/src/Dispatcher.h index 12e5507667..57e7ecfea3 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -19,25 +19,27 @@ #include "llvm/StringRef.h" -#include "atomic_static.h" +#include "support/atomic_static.h" #include "NetworkConnection.h" #include "Notifier.h" #include "Storage.h" +namespace wpi { class NetworkAcceptor; class NetworkStream; +} namespace nt { class DispatcherBase { friend class DispatcherTest; public: - typedef std::function()> Connector; + typedef std::function()> Connector; virtual ~DispatcherBase(); - void StartServer(StringRef persist_filename, - std::unique_ptr acceptor); + void StartServer(llvm::StringRef persist_filename, + std::unique_ptr acceptor); void StartClient(Connector connector); void StartClient(std::vector&& connectors); void Stop(); @@ -81,7 +83,7 @@ class DispatcherBase { std::thread m_dispatch_thread; std::thread m_clientserver_thread; - std::unique_ptr m_server_acceptor; + std::unique_ptr m_server_acceptor; std::vector m_client_connectors; // Mutex for user-accessible items diff --git a/src/Log.cpp b/src/Log.cpp index 6f71348b93..826102cced 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -61,6 +61,6 @@ static void def_log_func(unsigned int level, const char* file, #endif } -Logger::Logger() : m_func(def_log_func) {} +Logger::Logger() { SetLogger(def_log_func); } Logger::~Logger() {} diff --git a/src/Log.h b/src/Log.h index dd9e125e15..08431511ff 100644 --- a/src/Log.h +++ b/src/Log.h @@ -12,12 +12,12 @@ #include #include -#include "atomic_static.h" -#include "ntcore_c.h" +#include "support/atomic_static.h" +#include "support/Logger.h" namespace nt { -class Logger { +class Logger : public wpi::Logger { public: static Logger& GetInstance() { ATOMIC_STATIC(Logger, instance); @@ -25,59 +25,24 @@ class Logger { } ~Logger(); - typedef std::function LogFunc; - - void SetLogger(LogFunc func) { m_func = func; } - - void set_min_level(unsigned int level) { m_min_level = level; } - unsigned int min_level() const { return m_min_level; } - - void Log(unsigned int level, const char* file, unsigned int line, - const char* msg) { - if (!m_func || level < m_min_level) return; - m_func(level, file, line, msg); - } - - bool HasLogger() const { return m_func != nullptr; } - private: Logger(); - LogFunc m_func; - unsigned int m_min_level = 20; - ATOMIC_STATIC_DECL(Logger) }; -#define LOG(level, x) \ - do { \ - nt::Logger& logger = nt::Logger::GetInstance(); \ - if (logger.min_level() <= level && logger.HasLogger()) { \ - std::ostringstream oss; \ - oss << x; \ - logger.Log(level, __FILE__, __LINE__, oss.str().c_str()); \ - } \ - } while (0) +#define LOG(level, x) WPI_LOG(nt::Logger::GetInstance(), level, x) #undef ERROR -#define ERROR(x) LOG(NT_LOG_ERROR, x) -#define WARNING(x) LOG(NT_LOG_WARNING, x) -#define INFO(x) LOG(NT_LOG_INFO, x) +#define ERROR(x) WPI_ERROR(nt::Logger::GetInstance(), x) +#define WARNING(x) WPI_WARNING(nt::Logger::GetInstance(), x) +#define INFO(x) WPI_INFO(nt::Logger::GetInstance(), x) -#ifdef NDEBUG -#define DEBUG(x) do {} while (0) -#define DEBUG1(x) do {} while (0) -#define DEBUG2(x) do {} while (0) -#define DEBUG3(x) do {} while (0) -#define DEBUG4(x) do {} while (0) -#else -#define DEBUG(x) LOG(NT_LOG_DEBUG, x) -#define DEBUG1(x) LOG(NT_LOG_DEBUG1, x) -#define DEBUG2(x) LOG(NT_LOG_DEBUG2, x) -#define DEBUG3(x) LOG(NT_LOG_DEBUG3, x) -#define DEBUG4(x) LOG(NT_LOG_DEBUG4, x) -#endif +#define DEBUG(x) WPI_DEBUG(nt::Logger::GetInstance(), x) +#define DEBUG1(x) WPI_DEBUG1(nt::Logger::GetInstance(), x) +#define DEBUG2(x) WPI_DEBUG2(nt::Logger::GetInstance(), x) +#define DEBUG3(x) WPI_DEBUG3(nt::Logger::GetInstance(), x) +#define DEBUG4(x) WPI_DEBUG4(nt::Logger::GetInstance(), x) } // namespace nt diff --git a/src/NetworkConnection.cpp b/src/NetworkConnection.cpp index b2b741eb6d..ddf882ed25 100644 --- a/src/NetworkConnection.cpp +++ b/src/NetworkConnection.cpp @@ -7,11 +7,11 @@ #include "NetworkConnection.h" +#include "support/raw_socket_istream.h" #include "support/timestamp.h" #include "tcpsockets/NetworkStream.h" #include "Log.h" #include "Notifier.h" -#include "raw_socket_istream.h" #include "WireDecoder.h" #include "WireEncoder.h" @@ -19,7 +19,7 @@ using namespace nt; std::atomic_uint NetworkConnection::s_uid; -NetworkConnection::NetworkConnection(std::unique_ptr stream, +NetworkConnection::NetworkConnection(std::unique_ptr stream, Notifier& notifier, HandshakeFunc handshake, Message::GetEntryTypeFunc get_entry_type) @@ -106,7 +106,7 @@ void NetworkConnection::set_remote_id(StringRef remote_id) { } void NetworkConnection::ReadThreadMain() { - raw_socket_istream is(*m_stream); + wpi::raw_socket_istream is(*m_stream); WireDecoder decoder(is, m_proto_rev); m_state = static_cast(kHandshake); @@ -179,7 +179,7 @@ void NetworkConnection::WriteThreadMain() { msg->Write(encoder); } } - NetworkStream::Error err; + wpi::NetworkStream::Error err; if (!m_stream) break; if (encoder.size() == 0) continue; if (m_stream->send(encoder.data(), encoder.size(), &err) == 0) break; diff --git a/src/NetworkConnection.h b/src/NetworkConnection.h index 2f1073cabb..80465cf73e 100644 --- a/src/NetworkConnection.h +++ b/src/NetworkConnection.h @@ -17,7 +17,9 @@ #include "Message.h" #include "ntcore_cpp.h" +namespace wpi { class NetworkStream; +} namespace nt { @@ -35,9 +37,9 @@ class NetworkConnection { typedef std::function msg, NetworkConnection* conn)> ProcessIncomingFunc; typedef std::vector> Outgoing; - typedef ConcurrentQueue OutgoingQueue; + typedef wpi::ConcurrentQueue OutgoingQueue; - NetworkConnection(std::unique_ptr stream, + NetworkConnection(std::unique_ptr stream, Notifier& notifier, HandshakeFunc handshake, Message::GetEntryTypeFunc get_entry_type); @@ -54,7 +56,7 @@ class NetworkConnection { ConnectionInfo info() const; bool active() const { return m_active; } - NetworkStream& stream() { return *m_stream; } + wpi::NetworkStream& stream() { return *m_stream; } void QueueOutgoing(std::shared_ptr msg); void PostOutgoing(bool keep_alive); @@ -82,7 +84,7 @@ class NetworkConnection { static std::atomic_uint s_uid; unsigned int m_uid; - std::unique_ptr m_stream; + std::unique_ptr m_stream; Notifier& m_notifier; OutgoingQueue m_outgoing; HandshakeFunc m_handshake; diff --git a/src/Notifier.cpp b/src/Notifier.cpp index 3f68164c8b..874ba336ed 100644 --- a/src/Notifier.cpp +++ b/src/Notifier.cpp @@ -60,7 +60,7 @@ class UidVector { } // anonymous namespace -class Notifier::Thread : public SafeThread { +class Notifier::Thread : public wpi::SafeThread { public: Thread(std::function on_start, std::function on_exit) : m_on_start(on_start), m_on_exit(on_exit) {} @@ -83,7 +83,7 @@ class Notifier::Thread : public SafeThread { UidVector m_conn_listeners; struct EntryNotification { - EntryNotification(StringRef name_, std::shared_ptr value_, + EntryNotification(llvm::StringRef name_, std::shared_ptr value_, unsigned int flags_, EntryListenerCallback only_) : name(name_), value(value_), diff --git a/src/Notifier.h b/src/Notifier.h index 4a878ad718..e1a382a5b0 100644 --- a/src/Notifier.h +++ b/src/Notifier.h @@ -10,9 +10,9 @@ #include -#include "atomic_static.h" +#include "support/atomic_static.h" +#include "support/SafeThread.h" #include "ntcore_cpp.h" -#include "SafeThread.h" namespace nt { @@ -34,7 +34,7 @@ class Notifier { void SetOnStart(std::function on_start) { m_on_start = on_start; } void SetOnExit(std::function on_exit) { m_on_exit = on_exit; } - unsigned int AddEntryListener(StringRef prefix, + unsigned int AddEntryListener(llvm::StringRef prefix, EntryListenerCallback callback, unsigned int flags); void RemoveEntryListener(unsigned int entry_listener_uid); @@ -52,7 +52,7 @@ class Notifier { Notifier(); class Thread; - SafeThreadOwner m_owner; + wpi::SafeThreadOwner m_owner; std::atomic_bool m_local_notifiers; diff --git a/src/RpcServer.cpp b/src/RpcServer.cpp index 669931f0c8..f43634364b 100644 --- a/src/RpcServer.cpp +++ b/src/RpcServer.cpp @@ -15,7 +15,7 @@ using namespace nt; ATOMIC_STATIC_INIT(RpcServer) -class RpcServer::Thread : public SafeThread { +class RpcServer::Thread : public wpi::SafeThread { public: Thread(std::function on_start, std::function on_exit) : m_on_start(on_start), m_on_exit(on_exit) {} diff --git a/src/RpcServer.h b/src/RpcServer.h index 8bae64bba6..9a6eedfac9 100644 --- a/src/RpcServer.h +++ b/src/RpcServer.h @@ -15,10 +15,10 @@ #include #include "llvm/DenseMap.h" -#include "atomic_static.h" +#include "support/atomic_static.h" +#include "support/SafeThread.h" #include "Message.h" #include "ntcore_cpp.h" -#include "SafeThread.h" namespace nt { @@ -51,7 +51,7 @@ class RpcServer { RpcServer(); class Thread; - SafeThreadOwner m_owner; + wpi::SafeThreadOwner m_owner; struct RpcCall { RpcCall(StringRef name_, std::shared_ptr msg_, RpcCallback func_, diff --git a/src/Storage.cpp b/src/Storage.cpp index 75b8b8d174..4453e03abf 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -12,7 +12,7 @@ #include #include "llvm/StringExtras.h" -#include "Base64.h" +#include "support/Base64.h" #include "Log.h" #include "NetworkConnection.h" @@ -853,7 +853,7 @@ static void SavePersistentImpl( WriteString(os, v->GetString()); break; case NT_RAW: - Base64Encode(v->GetRaw(), &base64_encoded); + wpi::Base64Encode(v->GetRaw(), &base64_encoded); os << base64_encoded; break; case NT_BOOLEAN_ARRAY: { @@ -1139,7 +1139,7 @@ bool Storage::LoadPersistent( break; } case NT_RAW: - Base64Decode(line, &str); + wpi::Base64Decode(line, &str); value = Value::MakeRaw(std::move(str)); break; case NT_BOOLEAN_ARRAY: { diff --git a/src/Storage.h b/src/Storage.h index 92fcbddde8..e317c2c668 100644 --- a/src/Storage.h +++ b/src/Storage.h @@ -19,7 +19,7 @@ #include "llvm/DenseMap.h" #include "llvm/StringMap.h" -#include "atomic_static.h" +#include "support/atomic_static.h" #include "Message.h" #include "Notifier.h" #include "ntcore_cpp.h" diff --git a/src/Value.cpp b/src/Value.cpp index 8095fa9dff..a16f1726bb 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -13,12 +13,12 @@ using namespace nt; Value::Value() { m_val.type = NT_UNASSIGNED; - m_val.last_change = Now(); + m_val.last_change = wpi::Now(); } Value::Value(NT_Type type, const private_init&) { m_val.type = type; - m_val.last_change = Now(); + m_val.last_change = wpi::Now(); if (m_val.type == NT_BOOLEAN_ARRAY) m_val.data.arr_boolean.arr = nullptr; else if (m_val.type == NT_DOUBLE_ARRAY) diff --git a/src/WireDecoder.cpp b/src/WireDecoder.cpp index 138225e2d7..a08e5e0bcc 100644 --- a/src/WireDecoder.cpp +++ b/src/WireDecoder.cpp @@ -13,7 +13,7 @@ #include #include "llvm/MathExtras.h" -#include "leb128.h" +#include "support/leb128.h" using namespace nt; @@ -45,7 +45,8 @@ static double ReadDouble(const char*& buf) { return llvm::BitsToDouble(val); } -WireDecoder::WireDecoder(raw_istream& is, unsigned int proto_rev) : m_is(is) { +WireDecoder::WireDecoder(wpi::raw_istream& is, unsigned int proto_rev) + : m_is(is) { // Start with a 1K temporary buffer. Use malloc instead of new so we can // realloc. m_allocated = 1024; diff --git a/src/WireDecoder.h b/src/WireDecoder.h index c520be7b71..1063072c88 100644 --- a/src/WireDecoder.h +++ b/src/WireDecoder.h @@ -11,9 +11,9 @@ #include #include "nt_Value.h" -#include "leb128.h" +#include "support/leb128.h" +#include "support/raw_istream.h" //#include "Log.h" -#include "raw_istream.h" namespace nt { @@ -26,7 +26,7 @@ namespace nt { */ class WireDecoder { public: - explicit WireDecoder(raw_istream& is, unsigned int proto_rev); + explicit WireDecoder(wpi::raw_istream& is, unsigned int proto_rev); ~WireDecoder(); void set_proto_rev(unsigned int proto_rev) { m_proto_rev = proto_rev; } @@ -113,7 +113,7 @@ class WireDecoder { /* Reads an ULEB128-encoded unsigned integer. */ bool ReadUleb128(unsigned long* val) { - return nt::ReadUleb128(m_is, val); + return wpi::ReadUleb128(m_is, val); } bool ReadType(NT_Type* type); @@ -135,7 +135,7 @@ class WireDecoder { void Realloc(std::size_t len); /* input stream */ - raw_istream& m_is; + wpi::raw_istream& m_is; /* temporary buffer */ char* m_buf; diff --git a/src/WireEncoder.cpp b/src/WireEncoder.cpp index 610a53f5b5..d947dfdc0a 100644 --- a/src/WireEncoder.cpp +++ b/src/WireEncoder.cpp @@ -13,7 +13,7 @@ #include #include "llvm/MathExtras.h" -#include "leb128.h" +#include "support/leb128.h" using namespace nt; @@ -38,7 +38,7 @@ void WireEncoder::WriteDouble(double val) { } void WireEncoder::WriteUleb128(unsigned long val) { - nt::WriteUleb128(m_data, val); + wpi::WriteUleb128(m_data, val); } void WireEncoder::WriteType(NT_Type type) { @@ -191,7 +191,7 @@ std::size_t WireEncoder::GetStringSize(llvm::StringRef str) const { if (len > 0xffff) len = 0xffff; // Limited to 64K length; truncate return 2 + len; } - return SizeUleb128(str.size()) + str.size(); + return wpi::SizeUleb128(str.size()) + str.size(); } void WireEncoder::WriteString(llvm::StringRef str) { diff --git a/src/ntcore_c.cpp b/src/ntcore_c.cpp index 395dcdda0d..8050e22183 100644 --- a/src/ntcore_c.cpp +++ b/src/ntcore_c.cpp @@ -10,6 +10,7 @@ #include #include +#include "support/timestamp.h" #include "Value_internal.h" using namespace nt; @@ -405,6 +406,10 @@ const char *NT_LoadPersistent(const char *filename, * Utility Functions */ +unsigned long long NT_Now() { + return wpi::Now(); +} + void NT_SetLogger(NT_LogFunc func, unsigned int min_level) { nt::SetLogger(func, min_level); } diff --git a/src/ntcore_cpp.cpp b/src/ntcore_cpp.cpp index 9eae17306b..af66b21cce 100644 --- a/src/ntcore_cpp.cpp +++ b/src/ntcore_cpp.cpp @@ -11,8 +11,9 @@ #include #include -#include "Dispatcher.h" +#include "support/timestamp.h" #include "Log.h" +#include "Dispatcher.h" #include "Notifier.h" #include "RpcServer.h" #include "Storage.h" @@ -168,7 +169,7 @@ std::string PackRpcDefinition(const RpcDefinition& def) { } bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def) { - raw_mem_istream is(packed.data(), packed.size()); + wpi::raw_mem_istream is(packed.data(), packed.size()); WireDecoder dec(is, 0x0300); if (!dec.Read8(&def->version)) return false; if (!dec.ReadString(&def->name)) return false; @@ -211,7 +212,7 @@ std::string PackRpcValues(ArrayRef> values) { std::vector> UnpackRpcValues(StringRef packed, ArrayRef types) { - raw_mem_istream is(packed.data(), packed.size()); + wpi::raw_mem_istream is(packed.data(), packed.size()); WireDecoder dec(is, 0x0300); std::vector> vec; for (auto type : types) { @@ -281,6 +282,10 @@ const char* LoadPersistent( return Storage::GetInstance().LoadPersistent(filename, warn); } +unsigned long long Now() { + return wpi::Now(); +} + void SetLogger(LogFunc func, unsigned int min_level) { Logger& logger = Logger::GetInstance(); logger.SetLogger(func); diff --git a/src/Base64.cpp b/src/support/Base64.cpp similarity index 99% rename from src/Base64.cpp rename to src/support/Base64.cpp index 17aa125e3f..88fe28a47b 100644 --- a/src/Base64.cpp +++ b/src/support/Base64.cpp @@ -62,9 +62,9 @@ * */ -#include "Base64.h" +#include "support/Base64.h" -namespace nt { +namespace wpi { // aaaack but it's fast and const should make it shared text page. static const unsigned char pr2six[256] = @@ -149,4 +149,4 @@ void Base64Encode(llvm::StringRef plain, std::string* encoded) { } } -} // namespace nt +} // namespace wpi diff --git a/src/SafeThread.cpp b/src/support/SafeThread.cpp similarity index 94% rename from src/SafeThread.cpp rename to src/support/SafeThread.cpp index 3ef6375bf1..434d933b8b 100644 --- a/src/SafeThread.cpp +++ b/src/support/SafeThread.cpp @@ -5,9 +5,9 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "SafeThread.h" +#include "support/SafeThread.h" -using namespace nt; +using namespace wpi; void detail::SafeThreadOwnerBase::Start(SafeThread* thr) { SafeThread* curthr = nullptr; diff --git a/src/leb128.cpp b/src/support/leb128.cpp similarity index 96% rename from src/leb128.cpp rename to src/support/leb128.cpp index 3e99842bec..002ee43673 100644 --- a/src/leb128.cpp +++ b/src/support/leb128.cpp @@ -5,11 +5,11 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "leb128.h" +#include "support/leb128.h" -#include "raw_istream.h" +#include "support/raw_istream.h" -namespace nt { +namespace wpi { /** * Get size of unsigned LEB128 data @@ -116,4 +116,4 @@ bool ReadUleb128(raw_istream& is, unsigned long* ret) { return true; } -} // namespace nt +} // namespace wpi diff --git a/src/raw_istream.cpp b/src/support/raw_istream.cpp similarity index 92% rename from src/raw_istream.cpp rename to src/support/raw_istream.cpp index f300b9e418..13af6b97a8 100644 --- a/src/raw_istream.cpp +++ b/src/support/raw_istream.cpp @@ -5,11 +5,11 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "raw_istream.h" +#include "support/raw_istream.h" #include -using namespace nt; +using namespace wpi; bool raw_mem_istream::read(void* data, std::size_t len) { if (len > m_left) return false; diff --git a/src/raw_socket_istream.cpp b/src/support/raw_socket_istream.cpp similarity index 93% rename from src/raw_socket_istream.cpp rename to src/support/raw_socket_istream.cpp index a8e71c5f9c..a4dd1e4d5a 100644 --- a/src/raw_socket_istream.cpp +++ b/src/support/raw_socket_istream.cpp @@ -5,9 +5,9 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "raw_socket_istream.h" +#include "support/raw_socket_istream.h" -using namespace nt; +using namespace wpi; bool raw_socket_istream::read(void* data, std::size_t len) { char* cdata = static_cast(data); diff --git a/src/support/timestamp.cpp b/src/support/timestamp.cpp index 6dd4387f1c..9b21e5e2d7 100644 --- a/src/support/timestamp.cpp +++ b/src/support/timestamp.cpp @@ -4,7 +4,7 @@ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "timestamp.h" +#include "support/timestamp.h" #ifdef _WIN32 #include @@ -70,7 +70,7 @@ static const unsigned long long offset_val = timestamp(); static const unsigned long long frequency_val = update_frequency(); #endif -unsigned long long nt::Now() { +unsigned long long wpi::Now() { #ifdef _WIN32 assert(offset_val > 0u); assert(frequency_val > 0u); @@ -84,6 +84,6 @@ unsigned long long nt::Now() { #endif } -unsigned long long NT_Now() { - return nt::Now(); +unsigned long long WPI_Now() { + return wpi::Now(); } diff --git a/src/tcpsockets/SocketError.cpp b/src/tcpsockets/SocketError.cpp index 9619edd4a9..a2628fdd2b 100644 --- a/src/tcpsockets/SocketError.cpp +++ b/src/tcpsockets/SocketError.cpp @@ -5,7 +5,7 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "SocketError.h" +#include "tcpsockets/SocketError.h" #ifdef _WIN32 #include @@ -13,7 +13,7 @@ #include #endif -namespace tcpsockets { +namespace wpi { std::string SocketStrerror(int code) { #ifdef _WIN32 @@ -28,4 +28,4 @@ std::string SocketStrerror(int code) { #endif } -} // namespace tcpsockets +} // namespace wpi diff --git a/src/tcpsockets/TCPAcceptor.cpp b/src/tcpsockets/TCPAcceptor.cpp index e0651cfa37..9d8039ccab 100644 --- a/src/tcpsockets/TCPAcceptor.cpp +++ b/src/tcpsockets/TCPAcceptor.cpp @@ -21,7 +21,7 @@ limitations under the License. */ -#include "TCPAcceptor.h" +#include "tcpsockets/TCPAcceptor.h" #include #include @@ -36,16 +36,17 @@ #endif #include "llvm/SmallString.h" -#include "../Log.h" -#include "SocketError.h" +#include "support/Logger.h" +#include "tcpsockets/SocketError.h" -using namespace tcpsockets; +using namespace wpi; -TCPAcceptor::TCPAcceptor(int port, const char* address) +TCPAcceptor::TCPAcceptor(int port, const char* address, Logger& logger) : m_lsd(0), m_port(port), m_address(address), - m_listening(false) { + m_listening(false), + m_logger(logger) { m_shutdown = false; #ifdef _WIN32 WSAData wsaData; @@ -73,7 +74,7 @@ int TCPAcceptor::start() { m_lsd = socket(PF_INET, SOCK_STREAM, 0); if (m_lsd < 0) { - ERROR("could not create socket"); + WPI_ERROR(m_logger, "could not create socket"); return -1; } struct sockaddr_in address; @@ -99,13 +100,13 @@ int TCPAcceptor::start() { int result = bind(m_lsd, (struct sockaddr*)&address, sizeof(address)); if (result != 0) { - ERROR("bind() failed: " << SocketStrerror()); + WPI_ERROR(m_logger, "bind() failed: " << SocketStrerror()); return result; } result = listen(m_lsd, 5); if (result != 0) { - ERROR("listen() failed: " << SocketStrerror()); + WPI_ERROR(m_logger, "listen() failed: " << SocketStrerror()); return result; } m_listening = true; @@ -172,7 +173,8 @@ std::unique_ptr TCPAcceptor::accept() { std::memset(&address, 0, sizeof(address)); int sd = ::accept(m_lsd, (struct sockaddr*)&address, &len); if (sd < 0) { - if (!m_shutdown) ERROR("accept() failed: " << SocketStrerror()); + if (!m_shutdown) + WPI_ERROR(m_logger, "accept() failed: " << SocketStrerror()); return nullptr; } if (m_shutdown) { diff --git a/src/tcpsockets/TCPConnector.cpp b/src/tcpsockets/TCPConnector.cpp index 82e03ebb4a..8800acd7dd 100644 --- a/src/tcpsockets/TCPConnector.cpp +++ b/src/tcpsockets/TCPConnector.cpp @@ -21,7 +21,7 @@ limitations under the License */ -#include "TCPConnector.h" +#include "tcpsockets/TCPConnector.h" #include #include @@ -38,13 +38,13 @@ #include #endif -#include "TCPStream.h" +#include "tcpsockets/TCPStream.h" #include "llvm/SmallString.h" -#include "../Log.h" -#include "SocketError.h" +#include "support/Logger.h" +#include "tcpsockets/SocketError.h" -using namespace tcpsockets; +using namespace wpi; static int ResolveHostName(const char* hostname, struct in_addr* addr) { struct addrinfo hints; @@ -68,7 +68,8 @@ static int ResolveHostName(const char* hostname, struct in_addr* addr) { } std::unique_ptr TCPConnector::connect(const char* server, - int port, int timeout) { + int port, Logger& logger, + int timeout) { #ifdef _WIN32 struct WSAHelper { WSAHelper() { @@ -90,7 +91,7 @@ std::unique_ptr TCPConnector::connect(const char* server, addr_copy.push_back('\0'); int size = sizeof(address); if (WSAStringToAddress(addr_copy.data(), PF_INET, nullptr, (struct sockaddr*)&address, &size) != 0) { - ERROR("could not resolve " << server << " address"); + WPI_ERROR(logger, "could not resolve " << server << " address"); return nullptr; } #else @@ -102,11 +103,11 @@ std::unique_ptr TCPConnector::connect(const char* server, if (timeout == 0) { int sd = socket(AF_INET, SOCK_STREAM, 0); if (sd < 0) { - ERROR("could not create socket"); + WPI_ERROR(logger, "could not create socket"); return nullptr; } if (::connect(sd, (struct sockaddr*)&address, sizeof(address)) != 0) { - ERROR("connect() to " << server << " port " << port << " failed: " << SocketStrerror()); + WPI_ERROR(logger, "connect() to " << server << " port " << port << " failed: " << SocketStrerror()); #ifdef _WIN32 closesocket(sd); #else @@ -122,7 +123,7 @@ std::unique_ptr TCPConnector::connect(const char* server, socklen_t len; int result = -1, valopt, sd = socket(AF_INET, SOCK_STREAM, 0); if (sd < 0) { - ERROR("could not create socket"); + WPI_ERROR(logger, "could not create socket"); return nullptr; } @@ -154,15 +155,15 @@ std::unique_ptr TCPConnector::connect(const char* server, len = sizeof(int); getsockopt(sd, SOL_SOCKET, SO_ERROR, (char*)(&valopt), &len); if (valopt) { - ERROR("select() to " << server << " port " << port << " error " << valopt << " - " << SocketStrerror(valopt)); + WPI_ERROR(logger, "select() to " << server << " port " << port << " error " << valopt << " - " << SocketStrerror(valopt)); } // connection established else result = 0; } else - INFO("connect() to " << server << " port " << port << " timed out"); + WPI_INFO(logger, "connect() to " << server << " port " << port << " timed out"); } else - ERROR("connect() to " << server << " port " << port << " error " << SocketErrno() << " - " << SocketStrerror()); + WPI_ERROR(logger, "connect() to " << server << " port " << port << " error " << SocketErrno() << " - " << SocketStrerror()); } // Return socket to blocking mode diff --git a/src/tcpsockets/TCPStream.cpp b/src/tcpsockets/TCPStream.cpp index 3149be6f55..c87aeabb52 100644 --- a/src/tcpsockets/TCPStream.cpp +++ b/src/tcpsockets/TCPStream.cpp @@ -21,7 +21,7 @@ limitations under the License. */ -#include "TCPStream.h" +#include "tcpsockets/TCPStream.h" #ifdef _WIN32 #include @@ -31,13 +31,15 @@ #include #endif -TCPStream::TCPStream(int sd, struct sockaddr_in* address) : m_sd(sd) { +using namespace wpi; + +TCPStream::TCPStream(int sd, sockaddr_in* address) : m_sd(sd) { char ip[50]; #ifdef _WIN32 unsigned long size = sizeof(ip) - 1; - WSAAddressToString((struct sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size); + WSAAddressToString((sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size); #else - inet_ntop(PF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, + inet_ntop(PF_INET, (in_addr*)&(address->sin_addr.s_addr), ip, sizeof(ip) - 1); #endif m_peerIP = ip; diff --git a/test/unit/Base64Test.cpp b/test/unit/Base64Test.cpp index b83bb137fd..972ca487ad 100644 --- a/test/unit/Base64Test.cpp +++ b/test/unit/Base64Test.cpp @@ -5,11 +5,11 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "Base64.h" +#include "support/Base64.h" #include "gtest/gtest.h" -namespace nt { +namespace wpi { struct Base64TestParam { int plain_len; @@ -73,4 +73,4 @@ static Base64TestParam standard[] = { INSTANTIATE_TEST_CASE_P(Base64Standard, Base64Test, ::testing::ValuesIn(standard)); -} // namespace nt +} // namespace wpi diff --git a/test/unit/WireDecoderTest.cpp b/test/unit/WireDecoderTest.cpp index b1cb3ee900..0f4d9eb8ac 100644 --- a/test/unit/WireDecoderTest.cpp +++ b/test/unit/WireDecoderTest.cpp @@ -63,21 +63,21 @@ class WireDecoderTest : public ::testing::Test { }; TEST_F(WireDecoderTest, Construct) { - raw_mem_istream is("", 0); + wpi::raw_mem_istream is("", 0); WireDecoder d(is, 0x0300u); EXPECT_EQ(nullptr, d.error()); EXPECT_EQ(0x0300u, d.proto_rev()); } TEST_F(WireDecoderTest, SetProtoRev) { - raw_mem_istream is("", 0); + wpi::raw_mem_istream is("", 0); WireDecoder d(is, 0x0300u); d.set_proto_rev(0x0200u); EXPECT_EQ(0x0200u, d.proto_rev()); } TEST_F(WireDecoderTest, Read8) { - raw_mem_istream is("\x05\x01\x00", 3); + wpi::raw_mem_istream is("\x05\x01\x00", 3); WireDecoder d(is, 0x0300u); unsigned int val; ASSERT_TRUE(d.Read8(&val)); @@ -91,7 +91,7 @@ TEST_F(WireDecoderTest, Read8) { } TEST_F(WireDecoderTest, Read16) { - raw_mem_istream is("\x00\x05\x00\x01\x45\x67\x00\x00", 8); + wpi::raw_mem_istream is("\x00\x05\x00\x01\x45\x67\x00\x00", 8); WireDecoder d(is, 0x0300u); unsigned int val; ASSERT_TRUE(d.Read16(&val)); @@ -107,7 +107,7 @@ TEST_F(WireDecoderTest, Read16) { } TEST_F(WireDecoderTest, Read32) { - raw_mem_istream is( + wpi::raw_mem_istream is( "\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\xab\xcd" "\x12\x34\x56\x78\x00\x00\x00\x00", 20); @@ -130,7 +130,7 @@ TEST_F(WireDecoderTest, Read32) { TEST_F(WireDecoderTest, ReadDouble) { // values except min and max from // http://www.binaryconvert.com/result_double.html - raw_mem_istream is( + wpi::raw_mem_istream is( "\x00\x00\x00\x00\x00\x00\x00\x00" "\x41\x0c\x13\x80\x00\x00\x00\x00" "\x7f\xf0\x00\x00\x00\x00\x00\x00" @@ -154,7 +154,7 @@ TEST_F(WireDecoderTest, ReadDouble) { } TEST_F(WireDecoderTest, ReadUleb128) { - raw_mem_istream is("\x00\x7f\x80\x01\x80", 5); + wpi::raw_mem_istream is("\x00\x7f\x80\x01\x80", 5); WireDecoder d(is, 0x0300u); unsigned long val; ASSERT_TRUE(d.ReadUleb128(&val)); @@ -168,7 +168,7 @@ TEST_F(WireDecoderTest, ReadUleb128) { } TEST_F(WireDecoderTest, ReadType) { - raw_mem_istream is("\x00\x01\x02\x03\x10\x11\x12\x20", 8); + wpi::raw_mem_istream is("\x00\x01\x02\x03\x10\x11\x12\x20", 8); WireDecoder d(is, 0x0300u); NT_Type val; ASSERT_TRUE(d.ReadType(&val)); @@ -192,7 +192,7 @@ TEST_F(WireDecoderTest, ReadType) { } TEST_F(WireDecoderTest, ReadTypeError) { - raw_mem_istream is("\x30", 1); + wpi::raw_mem_istream is("\x30", 1); WireDecoder d(is, 0x0200u); NT_Type val; ASSERT_FALSE(d.ReadType(&val)); @@ -201,7 +201,7 @@ TEST_F(WireDecoderTest, ReadTypeError) { } TEST_F(WireDecoderTest, Reset) { - raw_mem_istream is("\x30", 1); + wpi::raw_mem_istream is("\x30", 1); WireDecoder d(is, 0x0200u); NT_Type val; ASSERT_FALSE(d.ReadType(&val)); @@ -212,7 +212,7 @@ TEST_F(WireDecoderTest, Reset) { } TEST_F(WireDecoderTest, ReadBooleanValue2) { - raw_mem_istream is("\x01\x00", 2); + wpi::raw_mem_istream is("\x01\x00", 2); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_BOOLEAN); ASSERT_TRUE(bool(val)); @@ -228,7 +228,7 @@ TEST_F(WireDecoderTest, ReadBooleanValue2) { } TEST_F(WireDecoderTest, ReadDoubleValue2) { - raw_mem_istream is( + wpi::raw_mem_istream is( "\x3f\xf0\x00\x00\x00\x00\x00\x00" "\x3f\xf0\x00\x00\x00\x00\x00\x00", 16); @@ -246,7 +246,7 @@ TEST_F(WireDecoderTest, ReadDoubleValue2) { } TEST_F(WireDecoderTest, ReadStringValue2) { - raw_mem_istream is("\x00\x05hello\x00\x03" "bye\x55", 13); + wpi::raw_mem_istream is("\x00\x05hello\x00\x03" "bye\x55", 13); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_STRING); ASSERT_TRUE(bool(val)); @@ -266,7 +266,7 @@ TEST_F(WireDecoderTest, ReadStringValue2) { } TEST_F(WireDecoderTest, ReadBooleanArrayValue2) { - raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8); + wpi::raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_BOOLEAN_ARRAY); ASSERT_TRUE(bool(val)); @@ -285,7 +285,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue2) { std::string s; s.push_back('\xff'); s.append(255, '\x00'); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_BOOLEAN_ARRAY); ASSERT_TRUE(bool(val)); @@ -296,7 +296,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue2) { } TEST_F(WireDecoderTest, ReadDoubleArrayValue2) { - raw_mem_istream is( + wpi::raw_mem_istream is( "\x02\x3f\xe0\x00\x00\x00\x00\x00\x00" "\x3f\xd0\x00\x00\x00\x00\x00\x00\x55", 18); @@ -317,7 +317,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue2) { std::string s; s.push_back('\xff'); s.append(255*8, '\x00'); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_DOUBLE_ARRAY); ASSERT_TRUE(bool(val)); @@ -328,7 +328,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue2) { } TEST_F(WireDecoderTest, ReadStringArrayValue2) { - raw_mem_istream is("\x02\x00\x05hello\x00\x07goodbye\x55", 18); + wpi::raw_mem_istream is("\x02\x00\x05hello\x00\x07goodbye\x55", 18); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_STRING_ARRAY); ASSERT_TRUE(bool(val)); @@ -347,7 +347,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue2) { s.push_back('\xff'); for (int i=0; i<255; ++i) s.append("\x00\x01h", 3); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0200u); auto val = d.ReadValue(NT_STRING_ARRAY); ASSERT_TRUE(bool(val)); @@ -358,7 +358,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue2) { } TEST_F(WireDecoderTest, ReadValueError2) { - raw_mem_istream is("", 0); + wpi::raw_mem_istream is("", 0); WireDecoder d(is, 0x0200u); ASSERT_FALSE(d.ReadValue(NT_UNASSIGNED)); // unassigned ASSERT_NE(nullptr, d.error()); @@ -373,7 +373,7 @@ TEST_F(WireDecoderTest, ReadValueError2) { } TEST_F(WireDecoderTest, ReadBooleanValue3) { - raw_mem_istream is("\x01\x00", 2); + wpi::raw_mem_istream is("\x01\x00", 2); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_BOOLEAN); ASSERT_TRUE(bool(val)); @@ -389,7 +389,7 @@ TEST_F(WireDecoderTest, ReadBooleanValue3) { } TEST_F(WireDecoderTest, ReadDoubleValue3) { - raw_mem_istream is( + wpi::raw_mem_istream is( "\x3f\xf0\x00\x00\x00\x00\x00\x00" "\x3f\xf0\x00\x00\x00\x00\x00\x00", 16); @@ -407,7 +407,7 @@ TEST_F(WireDecoderTest, ReadDoubleValue3) { } TEST_F(WireDecoderTest, ReadStringValue3) { - raw_mem_istream is("\x05hello\x03" "bye\x55", 11); + wpi::raw_mem_istream is("\x05hello\x03" "bye\x55", 11); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_STRING); ASSERT_TRUE(bool(val)); @@ -427,7 +427,7 @@ TEST_F(WireDecoderTest, ReadStringValue3) { } TEST_F(WireDecoderTest, ReadRawValue3) { - raw_mem_istream is("\x05hello\x03" "bye\x55", 11); + wpi::raw_mem_istream is("\x05hello\x03" "bye\x55", 11); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_RAW); ASSERT_TRUE(bool(val)); @@ -447,7 +447,7 @@ TEST_F(WireDecoderTest, ReadRawValue3) { } TEST_F(WireDecoderTest, ReadBooleanArrayValue3) { - raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8); + wpi::raw_mem_istream is("\x03\x00\x01\x00\x02\x01\x00\xff", 8); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_BOOLEAN_ARRAY); ASSERT_TRUE(bool(val)); @@ -466,7 +466,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue3) { std::string s; s.push_back('\xff'); s.append(255, '\x00'); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_BOOLEAN_ARRAY); ASSERT_TRUE(bool(val)); @@ -477,7 +477,7 @@ TEST_F(WireDecoderTest, ReadBooleanArrayBigValue3) { } TEST_F(WireDecoderTest, ReadDoubleArrayValue3) { - raw_mem_istream is( + wpi::raw_mem_istream is( "\x02\x3f\xe0\x00\x00\x00\x00\x00\x00" "\x3f\xd0\x00\x00\x00\x00\x00\x00\x55", 18); @@ -498,7 +498,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue3) { std::string s; s.push_back('\xff'); s.append(255*8, '\x00'); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_DOUBLE_ARRAY); ASSERT_TRUE(bool(val)); @@ -509,7 +509,7 @@ TEST_F(WireDecoderTest, ReadDoubleArrayBigValue3) { } TEST_F(WireDecoderTest, ReadStringArrayValue3) { - raw_mem_istream is("\x02\x05hello\x07goodbye\x55", 16); + wpi::raw_mem_istream is("\x02\x05hello\x07goodbye\x55", 16); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_STRING_ARRAY); ASSERT_TRUE(bool(val)); @@ -528,7 +528,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue3) { s.push_back('\xff'); for (int i=0; i<255; ++i) s.append("\x01h", 2); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0300u); auto val = d.ReadValue(NT_STRING_ARRAY); ASSERT_TRUE(bool(val)); @@ -539,7 +539,7 @@ TEST_F(WireDecoderTest, ReadStringArrayBigValue3) { } TEST_F(WireDecoderTest, ReadValueError3) { - raw_mem_istream is("", 0); + wpi::raw_mem_istream is("", 0); WireDecoder d(is, 0x0200u); ASSERT_FALSE(d.ReadValue(NT_UNASSIGNED)); // unassigned ASSERT_NE(nullptr, d.error()); @@ -554,7 +554,7 @@ TEST_F(WireDecoderTest, ReadString2) { s.append("\xff\xff", 2); s += s_big2; s.push_back('\x55'); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0200u); std::string outs; ASSERT_TRUE(d.ReadString(&outs)); @@ -581,7 +581,7 @@ TEST_F(WireDecoderTest, ReadString3) { s.append("\x81\x80\x04", 3); s += s_big3; s.push_back('\x55'); - raw_mem_istream is(s.data(), s.size()); + wpi::raw_mem_istream is(s.data(), s.size()); WireDecoder d(is, 0x0300u); std::string outs; ASSERT_TRUE(d.ReadString(&outs)); diff --git a/test/unit/leb128Test.cpp b/test/unit/leb128Test.cpp index 27734df486..00a79eebd2 100644 --- a/test/unit/leb128Test.cpp +++ b/test/unit/leb128Test.cpp @@ -14,7 +14,7 @@ // //===----------------------------------------------------------------------===// -#include "leb128.h" +#include "support/leb128.h" #include "gtest/gtest.h" @@ -24,9 +24,9 @@ #include "llvm/SmallString.h" #include "llvm/StringRef.h" -#include "raw_istream.h" +#include "support/raw_istream.h" -namespace nt { +namespace wpi { TEST(LEB128Test, WriteUleb128) { #define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \ @@ -111,4 +111,4 @@ TEST(LEB128Test, SizeUleb128) { EXPECT_EQ(5u, SizeUleb128(UINT32_MAX)); } -} // namespace nt +} // namespace wpi