diff options
-rw-r--r-- | contrib/Log.cpp | 5 | ||||
-rw-r--r-- | contrib/Socket.cpp | 9 | ||||
-rw-r--r-- | contrib/Socket.h | 3 |
3 files changed, 16 insertions, 1 deletions
diff --git a/contrib/Log.cpp b/contrib/Log.cpp index abbd69a..089e822 100644 --- a/contrib/Log.cpp +++ b/contrib/Log.cpp @@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <iomanip> #include <list> #include <cstdarg> #include <cinttypes> @@ -119,7 +120,9 @@ void Logger::io_process() } if (m.level != log_level_t::trace) { - std::cerr << levels_as_str[m.level] << " " << message << std::endl; + using namespace std::chrono; + time_t t = system_clock::to_time_t(system_clock::now()); + cerr << put_time(std::gmtime(&t), "%Y-%m-%dZ%H:%M:%S") << " " << levels_as_str[m.level] << " " << message << endl; } } } diff --git a/contrib/Socket.cpp b/contrib/Socket.cpp index d12c970..1ff6418 100644 --- a/contrib/Socket.cpp +++ b/contrib/Socket.cpp @@ -259,6 +259,15 @@ void UDPSocket::send(const std::vector<uint8_t>& data, InetAddress destination) } } +void UDPSocket::send(const std::string& data, InetAddress destination) +{ + const int ret = sendto(m_sock, data.data(), data.size(), 0, + destination.as_sockaddr(), sizeof(*destination.as_sockaddr())); + if (ret == SOCKET_ERROR && errno != ECONNREFUSED) { + throw runtime_error(string("Can't send UDP packet: ") + strerror(errno)); + } +} + void UDPSocket::joinGroup(const char* groupname, const char* if_addr) { ip_mreqn group; diff --git a/contrib/Socket.h b/contrib/Socket.h index 08607a5..f5143a0 100644 --- a/contrib/Socket.h +++ b/contrib/Socket.h @@ -115,6 +115,7 @@ class UDPSocket void close(void); void send(UDPPacket& packet); void send(const std::vector<uint8_t>& data, InetAddress destination); + void send(const std::string& data, InetAddress destination); UDPPacket receive(size_t max_size); void joinGroup(const char* groupname, const char* if_addr = nullptr); void setMulticastSource(const char* source_addr); @@ -198,6 +199,8 @@ class TCPSocket { */ ssize_t recv(void *buffer, size_t length, int flags, int timeout_ms); + SOCKET get_sockfd() const { return m_sock; } + private: explicit TCPSocket(int sockfd); explicit TCPSocket(int sockfd, InetAddress remote_address); |