diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 155d2cf7..f2108f5a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [7.7.0] - 2019-12-24 + +(ws client) all commands use spdlog instead of std::cerr or std::cout for logging + ## [7.6.5] - 2019-12-24 (cobra client) send a websocket ping every 30s to keep the connection opened diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index 66bbffc3..123a217d 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -10,7 +10,6 @@ #include "IXSocketConnect.h" #include "IXUserAgent.h" #include -#include #include #include diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 2c74bfda..6cab4728 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -11,7 +11,7 @@ #include "IXSocketConnect.h" #include "IXSocketFactory.h" #include -#include +#include #include #include @@ -45,13 +45,13 @@ namespace ix void SocketServer::logError(const std::string& str) { std::lock_guard lock(_logMutex); - std::cerr << str << std::endl; + fprintf(stderr, "%s\n", str.c_str()); } void SocketServer::logInfo(const std::string& str) { std::lock_guard lock(_logMutex); - std::cout << str << std::endl; + fprintf(stdout, "%s\n", str.c_str()); } std::pair SocketServer::listen() diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index b39f39b8..97f8246a 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "7.6.5" +#define IX_WEBSOCKET_VERSION "7.8.0" diff --git a/ws/ws.cpp b/ws/ws.cpp index 6a191474..9bf80194 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include #include @@ -39,12 +38,13 @@ int main(int argc, char** argv) // Display command. if (getenv("DEBUG")) { - std::cout << "Command: "; + std::stringstream ss; + ss << "Command: "; for (int i = 0; i < argc; ++i) { - std::cout << argv[i] << " "; + ss << argv[i] << " "; } - std::cout << std::endl; + spdlog::info(ss.str()); } CLI::App app {"ws is a websocket tool"}; @@ -496,11 +496,11 @@ int main(int argc, char** argv) } else if (version) { - std::cout << "ws " << ix::userAgent() << std::endl; + spdlog::info("ws {}", ix::userAgent()); } else { - std::cerr << "A subcommand or --version is required" << std::endl; + spdlog::error("A subcommand or --version is required"); } ix::uninitNetSystem(); diff --git a/ws/ws_autobahn.cpp b/ws/ws_autobahn.cpp index 77171a11..19ffc056 100644 --- a/ws/ws_autobahn.cpp +++ b/ws/ws_autobahn.cpp @@ -32,7 +32,6 @@ #include #include -#include #include #include #include @@ -91,7 +90,7 @@ namespace ix { if (!_quiet) { - std::cerr << msg; + spdlog::info(msg); } } @@ -183,7 +182,7 @@ namespace ix webSocket.setOnMessageCallback([&condition, &success](const ix::WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Close) { - std::cerr << "Report generated" << std::endl; + spdlog::info("Report generated"); condition.notify_one(); } else if (msg->type == ix::WebSocketMessageType::Error) @@ -193,7 +192,7 @@ namespace ix ss << "#retries: " << msg->errorInfo.retries << std::endl; ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str() << std::endl; + spdlog::info(ss.str()); success = false; } @@ -236,7 +235,7 @@ namespace ix ss << "#retries: " << msg->errorInfo.retries << std::endl; ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str() << std::endl; + spdlog::info(ss.str()); condition.notify_one(); } @@ -269,7 +268,7 @@ namespace ix int ws_autobahn_main(const std::string& url, bool quiet) { int testCasesCount = getTestCaseCount(url); - std::cerr << "Test cases count: " << testCasesCount << std::endl; + spdlog::info("Test cases count: {}", testCasesCount); if (testCasesCount == -1) { diff --git a/ws/ws_broadcast_server.cpp b/ws/ws_broadcast_server.cpp index dc3165c2..4c165c82 100644 --- a/ws/ws_broadcast_server.cpp +++ b/ws/ws_broadcast_server.cpp @@ -4,9 +4,10 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ -#include #include #include +#include + namespace ix { @@ -14,7 +15,7 @@ namespace ix const std::string& hostname, const ix::SocketTLSOptions& tlsOptions) { - std::cout << "Listening on " << hostname << ":" << port << std::endl; + spdlog::info("Listening on {}:{}", hostname, port); ix::WebSocketServer server(port, hostname); server.setTLSOptions(tlsOptions); @@ -25,20 +26,19 @@ namespace ix const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "New connection" << std::endl; - std::cerr << "id: " << connectionState->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + spdlog::info("New connection"); + spdlog::info("id: {}", connectionState->getId()); + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cerr << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; + spdlog::info("Closed connection: code {} reason {}", + msg->closeInfo.code, msg->closeInfo.reason); } else if (msg->type == ix::WebSocketMessageType::Error) { @@ -47,30 +47,29 @@ namespace ix ss << "#retries: " << msg->errorInfo.retries << std::endl; ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); + spdlog::info(ss.str()); } else if (msg->type == ix::WebSocketMessageType::Fragment) { - std::cerr << "Received message fragment" << std::endl; + spdlog::info("Received message fragment"); } else if (msg->type == ix::WebSocketMessageType::Message) { - std::cerr << "Received " << msg->wireSize << " bytes" << std::endl; + spdlog::info("Received {} bytes", msg->wireSize); for (auto&& client : server.getClients()) { if (client != webSocket) { client->send(msg->str, msg->binary, [](int current, int total) -> bool { - std::cerr << "Step " << current << " out of " << total << std::endl; + spdlog::info("Step {} out of {}", current, total); return true; }); do { size_t bufferedAmount = client->bufferedAmount(); - std::cerr << bufferedAmount << " bytes left to be sent" - << std::endl; + spdlog::info("{} bytes left to be sent", bufferedAmount); std::chrono::duration duration(10); std::this_thread::sleep_for(duration); @@ -84,7 +83,7 @@ namespace ix auto res = server.listen(); if (!res.first) { - std::cerr << res.second << std::endl; + spdlog::info(res.second); return 1; } diff --git a/ws/ws_chat.cpp b/ws/ws_chat.cpp index 713e5607..3f9d5278 100644 --- a/ws/ws_chat.cpp +++ b/ws/ws_chat.cpp @@ -9,12 +9,13 @@ // Broadcast server can be ran with `ws broadcast_server` // +#include "linenoise.hpp" #include "nlohmann/json.hpp" -#include #include #include #include #include +#include // for convenience using json = nlohmann::json; @@ -55,7 +56,7 @@ namespace ix void WebSocketChat::log(const std::string& msg) { - std::cout << msg << std::endl; + spdlog::info(msg); } size_t WebSocketChat::getReceivedMessagesCount() const @@ -85,20 +86,21 @@ namespace ix if (msg->type == ix::WebSocketMessageType::Open) { log("ws chat: connected"); - std::cout << "Uri: " << msg->openInfo.uri << std::endl; - std::cout << "Handshake Headers:" << std::endl; + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cout << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } - ss << "ws chat: user " << _user << " Connected !"; + spdlog::info("ws chat: user {} connected !", _user); log(ss.str()); } else if (msg->type == ix::WebSocketMessageType::Close) { - ss << "ws chat: user " << _user << " disconnected !" - << " code " << msg->closeInfo.code << " reason " << msg->closeInfo.reason; + ss << "ws chat user disconnected: " << _user; + ss << " code " << msg->closeInfo.code; + ss << " reason " << msg->closeInfo.reason << std::endl; log(ss.str()); } else if (msg->type == ix::WebSocketMessageType::Message) @@ -162,25 +164,25 @@ namespace ix int ws_chat_main(const std::string& url, const std::string& user) { - std::cout << "Type Ctrl-D to exit prompt..." << std::endl; + spdlog::info("Type Ctrl-D to exit prompt..."); WebSocketChat webSocketChat(url, user); webSocketChat.start(); while (true) { - std::string text; - std::cout << user << " > " << std::flush; - std::getline(std::cin, text); + // Read line + std::string line; + auto quit = linenoise::Readline("> ", line); - if (!std::cin) + if (quit) { break; } - webSocketChat.sendMessage(text); + webSocketChat.sendMessage(line); } - std::cout << std::endl; + spdlog::info(""); webSocketChat.stop(); return 0; diff --git a/ws/ws_cobra_metrics_publish.cpp b/ws/ws_cobra_metrics_publish.cpp index d6dcb318..1d32547a 100644 --- a/ws/ws_cobra_metrics_publish.cpp +++ b/ws/ws_cobra_metrics_publish.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include diff --git a/ws/ws_cobra_metrics_to_redis.cpp b/ws/ws_cobra_metrics_to_redis.cpp index 3e52ba50..40740039 100644 --- a/ws/ws_cobra_metrics_to_redis.cpp +++ b/ws/ws_cobra_metrics_to_redis.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -44,8 +43,7 @@ namespace ix auto timer = [&msgPerSeconds, &msgCount] { while (true) { - std::cout << "#messages " << msgCount << " " - << "msg/s " << msgPerSeconds << std::endl; + spdlog::info("#messages {} msg/s {}", msgCount, msgPerSeconds); msgPerSeconds = 0; auto duration = std::chrono::seconds(1); diff --git a/ws/ws_cobra_publish.cpp b/ws/ws_cobra_publish.cpp index b99145d8..860221da 100644 --- a/ws/ws_cobra_publish.cpp +++ b/ws/ws_cobra_publish.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include diff --git a/ws/ws_cobra_subscribe.cpp b/ws/ws_cobra_subscribe.cpp index 0e6c9943..6661d616 100644 --- a/ws/ws_cobra_subscribe.cpp +++ b/ws/ws_cobra_subscribe.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -41,8 +40,7 @@ namespace ix auto timer = [&msgPerSeconds, &msgCount] { while (true) { - std::cout << "#messages " << msgCount << " " - << "msg/s " << msgPerSeconds << std::endl; + spdlog::info("#messages {} msg/s {}", msgCount, msgPerSeconds); msgPerSeconds = 0; auto duration = std::chrono::seconds(1); @@ -77,7 +75,7 @@ namespace ix [&jsonWriter, &quiet, &msgPerSeconds, &msgCount](const Json::Value& msg) { if (!quiet) { - std::cerr << jsonWriter.write(msg) << std::endl; + spdlog::info(jsonWriter.write(msg)); } msgPerSeconds++; diff --git a/ws/ws_cobra_to_sentry.cpp b/ws/ws_cobra_to_sentry.cpp index 42618eb7..18d7c667 100644 --- a/ws/ws_cobra_to_sentry.cpp +++ b/ws/ws_cobra_to_sentry.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -161,7 +160,7 @@ namespace ix }; // Create a thread pool - std::cerr << "Starting " << jobs << " sentry sender jobs" << std::endl; + spdlog::info("Starting {} sentry sender jobs", jobs); std::vector pool; for (int i = 0; i < jobs; i++) { @@ -197,7 +196,7 @@ namespace ix } else if (eventType == ix::CobraConnection_EventType_Authenticated) { - std::cerr << "Subscriber authenticated" << std::endl; + spdlog::info("Subscriber authenticated"); conn.subscribe(channel, filter, [&jsonWriter, diff --git a/ws/ws_cobra_to_statsd.cpp b/ws/ws_cobra_to_statsd.cpp index 0015b861..c5797d66 100644 --- a/ws/ws_cobra_to_statsd.cpp +++ b/ws/ws_cobra_to_statsd.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include diff --git a/ws/ws_connect.cpp b/ws/ws_connect.cpp index 684ad01c..9cf21b99 100644 --- a/ws/ws_connect.cpp +++ b/ws/ws_connect.cpp @@ -5,10 +5,10 @@ */ #include "linenoise.hpp" -#include #include #include #include +#include #include @@ -93,7 +93,7 @@ namespace ix auto key = token.substr(0, pos); auto val = token.substr(pos + 1); - std::cerr << key << ": " << val << std::endl; + spdlog::info("{}: {}", key, val); headers[key] = val; } @@ -129,11 +129,11 @@ namespace ix if (msg->type == ix::WebSocketMessageType::Open) { log("ws_connect: connected"); - std::cout << "Uri: " << msg->openInfo.uri << std::endl; - std::cout << "Handshake Headers:" << std::endl; + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cout << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) @@ -145,7 +145,7 @@ namespace ix } else if (msg->type == ix::WebSocketMessageType::Message) { - std::cerr << "Received " << msg->wireSize << " bytes" << std::endl; + spdlog::info("Received {} bytes", msg->wireSize); ss << "ws_connect: received message: " << msg->str; log(ss.str()); @@ -160,15 +160,15 @@ namespace ix } else if (msg->type == ix::WebSocketMessageType::Fragment) { - std::cerr << "Received message fragment" << std::endl; + spdlog::info("Received message fragment"); } else if (msg->type == ix::WebSocketMessageType::Ping) { - std::cerr << "Received ping" << std::endl; + spdlog::info("Received ping"); } else if (msg->type == ix::WebSocketMessageType::Pong) { - std::cerr << "Received pong" << std::endl; + spdlog::info("Received pong"); } else { @@ -225,14 +225,14 @@ namespace ix if (line == "/stop") { - std::cout << "Stopping connection..." << std::endl; + spdlog::info("Stopping connection..."); webSocketChat.stop(); continue; } if (line == "/start") { - std::cout << "Starting connection..." << std::endl; + spdlog::info("Starting connection..."); webSocketChat.start(); continue; } @@ -243,7 +243,7 @@ namespace ix linenoise::AddHistory(line.c_str()); } - std::cout << std::endl; + spdlog::info(""); webSocketChat.stop(); return 0; diff --git a/ws/ws_echo_server.cpp b/ws/ws_echo_server.cpp index ea023c26..e2fd46c7 100644 --- a/ws/ws_echo_server.cpp +++ b/ws/ws_echo_server.cpp @@ -4,8 +4,8 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ -#include #include +#include #include namespace ix @@ -15,7 +15,7 @@ namespace ix const std::string& hostname, const ix::SocketTLSOptions& tlsOptions) { - std::cout << "Listening on " << hostname << ":" << port << std::endl; + spdlog::info("Listening on {}:{}", hostname, port); ix::WebSocketServer server(port, hostname); server.setTLSOptions(tlsOptions); @@ -27,13 +27,13 @@ namespace ix [webSocket, connectionState, greetings](const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "New connection" << std::endl; - std::cerr << "id: " << connectionState->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + spdlog::info("New connection"); + spdlog::info("id: {}", connectionState->getId()); + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cerr << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } if (greetings) @@ -43,22 +43,21 @@ namespace ix } else if (msg->type == ix::WebSocketMessageType::Close) { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; + spdlog::info("Closed connection: client id {} code {} reason {}", + connectionState->getId(), + msg->closeInfo.code, + msg->closeInfo.reason); } else if (msg->type == ix::WebSocketMessageType::Error) { - std::stringstream ss; - ss << "Connection error: " << msg->errorInfo.reason << std::endl; - ss << "#retries: " << msg->errorInfo.retries << std::endl; - ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; - ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); + spdlog::error("Connection error: {}", msg->errorInfo.reason); + spdlog::error("#retries: {}", msg->errorInfo.retries); + spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time); + spdlog::error("HTTP Status: {}", msg->errorInfo.http_status); } else if (msg->type == ix::WebSocketMessageType::Message) { - std::cerr << "Received " << msg->wireSize << " bytes" << std::endl; + spdlog::info("Received {} bytes", msg->wireSize); webSocket->send(msg->str, msg->binary); } }); @@ -67,7 +66,7 @@ namespace ix auto res = server.listen(); if (!res.first) { - std::cerr << res.second << std::endl; + spdlog::error(res.second); return 1; } diff --git a/ws/ws_http_client.cpp b/ws/ws_http_client.cpp index 33161d14..1804010d 100644 --- a/ws/ws_http_client.cpp +++ b/ws/ws_http_client.cpp @@ -5,10 +5,10 @@ */ #include -#include #include #include #include +#include #include namespace ix @@ -47,7 +47,7 @@ namespace ix auto key = token.substr(0, pos); auto val = token.substr(pos + 1); - std::cerr << key << ": " << val << std::endl; + spdlog::info("{}: {}", key, val); headers[key] = val; } @@ -76,7 +76,7 @@ namespace ix auto key = token.substr(0, pos); auto val = token.substr(pos + 1); - std::cerr << key << ": " << val << std::endl; + spdlog::info("{}: {}", key, val); httpParameters[key] = val; } @@ -108,10 +108,9 @@ namespace ix args->maxRedirects = maxRedirects; args->verbose = verbose; args->compress = compress; - args->logger = [](const std::string& msg) { std::cout << msg; }; + args->logger = [](const std::string& msg) { spdlog::info(msg); }; args->onProgressCallback = [](int current, int total) -> bool { - std::cerr << "\r" - << "Downloaded " << current << " bytes out of " << total; + spdlog::info("Downloaded {} bytes out of {}", current, total); return true; }; @@ -131,20 +130,20 @@ namespace ix response = httpClient.post(url, httpParameters, args); } - std::cerr << std::endl; + spdlog::info(""); for (auto it : response->headers) { - std::cerr << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } - std::cerr << "Upload size: " << response->uploadSize << std::endl; - std::cerr << "Download size: " << response->downloadSize << std::endl; + spdlog::info("Upload size: {}", response->uploadSize); + spdlog::info("Download size: {}", response->downloadSize); - std::cerr << "Status: " << response->statusCode << std::endl; + spdlog::info("Status: {}", response->statusCode); if (response->errorCode != HttpErrorCode::Ok) { - std::cerr << "error message: " << response->errorMsg << std::endl; + spdlog::info("error message: ", response->errorMsg); } if (!headersOnly && response->errorCode == HttpErrorCode::Ok) @@ -158,7 +157,7 @@ namespace ix filename = output; } - std::cout << "Writing to disk: " << filename << std::endl; + spdlog::info("Writing to disk: {}", filename); std::ofstream out(filename); out.write((char*) &response->payload.front(), response->payload.size()); out.close(); @@ -167,14 +166,13 @@ namespace ix { if (response->headers["Content-Type"] != "application/octet-stream") { - std::cout << "payload: " << response->payload << std::endl; + spdlog::info("payload: {}", response->payload); } else { - std::cerr << "Binary output can mess up your terminal." << std::endl; - std::cerr << "Use the -O flag to save the file to disk." << std::endl; - std::cerr << "You can also use the --output option to specify a filename." - << std::endl; + spdlog::info("Binary output can mess up your terminal."); + spdlog::info("Use the -O flag to save the file to disk."); + spdlog::info("You can also use the --output option to specify a filename."); } } } diff --git a/ws/ws_httpd.cpp b/ws/ws_httpd.cpp index 2a8da3d2..f99abeba 100644 --- a/ws/ws_httpd.cpp +++ b/ws/ws_httpd.cpp @@ -5,7 +5,6 @@ */ #include -#include #include #include #include @@ -32,7 +31,7 @@ namespace ix auto res = server.listen(); if (!res.first) { - std::cerr << res.second << std::endl; + spdlog::error(res.second); return 1; } diff --git a/ws/ws_ping_pong.cpp b/ws/ws_ping_pong.cpp index 8d81b0e4..325a2195 100644 --- a/ws/ws_ping_pong.cpp +++ b/ws/ws_ping_pong.cpp @@ -4,11 +4,12 @@ * Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved. */ -#include #include #include #include +#include #include +#include namespace ix { @@ -40,7 +41,7 @@ namespace ix void WebSocketPingPong::log(const std::string& msg) { - std::cout << msg << std::endl; + spdlog::info(msg); } void WebSocketPingPong::stop() @@ -56,18 +57,18 @@ namespace ix log(std::string("Connecting to url: ") + _url); _webSocket.setOnMessageCallback([this](const ix::WebSocketMessagePtr& msg) { - std::cerr << "Received " << msg->wireSize << " bytes" << std::endl; + spdlog::info("Received {} bytes", msg->wireSize); std::stringstream ss; if (msg->type == ix::WebSocketMessageType::Open) { log("ping_pong: connected"); - std::cout << "Uri: " << msg->openInfo.uri << std::endl; - std::cout << "Handshake Headers:" << std::endl; + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cout << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) @@ -127,7 +128,7 @@ namespace ix int ws_ping_pong_main(const std::string& url, const ix::SocketTLSOptions& tlsOptions) { - std::cout << "Type Ctrl-D to exit prompt..." << std::endl; + spdlog::info("Type Ctrl-D to exit prompt..."); WebSocketPingPong webSocketPingPong(url, tlsOptions); webSocketPingPong.start(); diff --git a/ws/ws_proxy_server.cpp b/ws/ws_proxy_server.cpp index c3431234..29503051 100644 --- a/ws/ws_proxy_server.cpp +++ b/ws/ws_proxy_server.cpp @@ -4,8 +4,8 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ -#include #include +#include #include namespace ix @@ -44,7 +44,7 @@ namespace ix const std::string& remoteUrl, bool verbose) { - std::cout << "Listening on " << hostname << ":" << port << std::endl; + spdlog::info("Listening on {}:{}", hostname, port); ix::WebSocketServer server(port, hostname); server.setTLSOptions(tlsOptions); @@ -64,38 +64,36 @@ namespace ix const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "New connection" << std::endl; - std::cerr << "server id: " << state->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + spdlog::info("New connection to remote server"); + spdlog::info("id: {}", state->getId()); + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cerr << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; - webSocket->close(msg->closeInfo.code, msg->closeInfo.reason); + spdlog::info("Closed remote server connection: client id {} code {} reason {}", + state->getId(), + msg->closeInfo.code, + msg->closeInfo.reason); state->setTerminated(); } else if (msg->type == ix::WebSocketMessageType::Error) { - std::stringstream ss; - ss << "Connection error: " << msg->errorInfo.reason << std::endl; - ss << "#retries: " << msg->errorInfo.retries << std::endl; - ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; - ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); + spdlog::error("Connection error: {}", msg->errorInfo.reason); + spdlog::error("#retries: {}", msg->errorInfo.retries); + spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time); + spdlog::error("HTTP Status: {}", msg->errorInfo.http_status); } else if (msg->type == ix::WebSocketMessageType::Message) { - std::cerr << "Received " << msg->wireSize << " bytes from server" << std::endl; + spdlog::info("Received {} bytes from server", msg->wireSize); if (verbose) { - std::cerr << "payload " << msg->str << std::endl; + spdlog::info("payload {}", msg->str); } webSocket->send(msg->str, msg->binary); @@ -107,13 +105,13 @@ namespace ix const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "New connection" << std::endl; - std::cerr << "client id: " << state->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + spdlog::info("New connection from client"); + spdlog::info("id: {}", state->getId()); + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cerr << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } // Connect to the 'real' server @@ -127,34 +125,34 @@ namespace ix // connection with the remote server while (state->webSocket().getReadyState() != ReadyState::Open) { - std::cerr << "waiting for server connection establishment" << std::endl; + spdlog::info("waiting for server connection establishment"); std::this_thread::sleep_for(std::chrono::milliseconds(10)); } - std::cerr << "server connection established" << std::endl; + spdlog::info("server connection established"); } else if (msg->type == ix::WebSocketMessageType::Close) { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; + spdlog::info("Closed client connection: client id {} code {} reason {}", + state->getId(), + msg->closeInfo.code, + msg->closeInfo.reason); state->webSocket().close(msg->closeInfo.code, msg->closeInfo.reason); } else if (msg->type == ix::WebSocketMessageType::Error) { - std::stringstream ss; - ss << "Connection error: " << msg->errorInfo.reason << std::endl; - ss << "#retries: " << msg->errorInfo.retries << std::endl; - ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; - ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); + spdlog::error("Connection error: {}", msg->errorInfo.reason); + spdlog::error("#retries: {}", msg->errorInfo.retries); + spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time); + spdlog::error("HTTP Status: {}", msg->errorInfo.http_status); } else if (msg->type == ix::WebSocketMessageType::Message) { - std::cerr << "Received " << msg->wireSize << " bytes from client" << std::endl; + spdlog::info("Received {} bytes from client", msg->wireSize); if (verbose) { - std::cerr << "payload " << msg->str << std::endl; + spdlog::info("payload {}", msg->str); } + state->webSocket().send(msg->str, msg->binary); } }); @@ -163,7 +161,7 @@ namespace ix auto res = server.listen(); if (!res.first) { - std::cerr << res.second << std::endl; + spdlog::info(res.second); return 1; } diff --git a/ws/ws_receive.cpp b/ws/ws_receive.cpp index b966ea97..a0086f83 100644 --- a/ws/ws_receive.cpp +++ b/ws/ws_receive.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -75,12 +75,12 @@ namespace ix void WebSocketReceiver::log(const std::string& msg) { - std::cout << msg << std::endl; + spdlog::info(msg); } void WebSocketReceiver::waitForConnection() { - std::cout << "ws_receive: Connecting..." << std::endl; + spdlog::info("{}: Connecting...", "ws_receive"); std::unique_lock lock(_conditionVariableMutex); _condition.wait(lock); @@ -88,7 +88,7 @@ namespace ix void WebSocketReceiver::waitForMessage() { - std::cout << "ws_receive: Waiting for message..." << std::endl; + spdlog::info("{}: Waiting for message...", "ws_receive"); std::unique_lock lock(_conditionVariableMutex); _condition.wait(lock); @@ -124,7 +124,7 @@ namespace ix void WebSocketReceiver::handleMessage(const std::string& str) { - std::cerr << "ws_receive: Received message: " << str.size() << std::endl; + spdlog::info("ws_receive: Received message: {}", str.size()); std::string errMsg; MsgPack data = MsgPack::parse(str, errMsg); @@ -134,17 +134,17 @@ namespace ix return; } - std::cout << "id: " << data["id"].string_value() << std::endl; + spdlog::info("id: {}", data["id"].string_value()); std::vector content = data["content"].binary_items(); - std::cout << "ws_receive: Content size: " << content.size() << std::endl; + spdlog::info("ws_receive: Content size: {}", content.size()); // Validate checksum uint64_t cksum = ix::djb2Hash(content); auto cksumRef = data["djb2_hash"].string_value(); - std::cout << "ws_receive: Computed hash: " << cksum << std::endl; - std::cout << "ws_receive: Reference hash: " << cksumRef << std::endl; + spdlog::info("ws_receive: Computed hash: {}", cksum); + spdlog::info("ws_receive: Reference hash: {}", cksumRef); if (std::to_string(cksum) != cksumRef) { @@ -157,12 +157,12 @@ namespace ix std::string filenameTmp = filename + ".tmp"; - std::cout << "ws_receive: Writing to disk: " << filenameTmp << std::endl; + spdlog::info("ws_receive: Writing to disk: {}", filenameTmp); std::ofstream out(filenameTmp); out.write((char*) &content.front(), content.size()); out.close(); - std::cout << "ws_receive: Renaming " << filenameTmp << " to " << filename << std::endl; + spdlog::info("ws_receive: Renaming {} to {}", filenameTmp, filename); rename(filenameTmp.c_str(), filename.c_str()); std::map pdu; @@ -170,7 +170,7 @@ namespace ix pdu["id"] = data["id"]; pdu["filename"] = data["filename"]; - std::cout << "Sending ack to sender" << std::endl; + spdlog::info("Sending ack to sender"); MsgPack msg(pdu); _webSocket.sendBinary(msg.dump()); } @@ -192,11 +192,11 @@ namespace ix _condition.notify_one(); log("ws_receive: connected"); - std::cout << "Uri: " << msg->openInfo.uri << std::endl; - std::cout << "Handshake Headers:" << std::endl; + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cout << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) @@ -259,7 +259,7 @@ namespace ix std::chrono::duration duration(1000); std::this_thread::sleep_for(duration); - std::cout << "ws_receive: Done !" << std::endl; + spdlog::info("ws_receive: Done !"); webSocketReceiver.stop(); } diff --git a/ws/ws_redis_publish.cpp b/ws/ws_redis_publish.cpp index f3b6fe47..3bd61f1a 100644 --- a/ws/ws_redis_publish.cpp +++ b/ws/ws_redis_publish.cpp @@ -4,8 +4,8 @@ * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. */ -#include #include +#include #include namespace ix @@ -20,7 +20,7 @@ namespace ix RedisClient redisClient; if (!redisClient.connect(hostname, port)) { - std::cerr << "Cannot connect to redis host" << std::endl; + spdlog::info("Cannot connect to redis host"); return 1; } @@ -30,10 +30,10 @@ namespace ix if (!redisClient.auth(password, authResponse)) { std::stringstream ss; - std::cerr << "Cannot authenticated to redis" << std::endl; + spdlog::info("Cannot authenticated to redis"); return 1; } - std::cout << "Auth response: " << authResponse << ":" << port << std::endl; + spdlog::info("Auth response: {}", authResponse); } std::string errMsg; @@ -41,8 +41,7 @@ namespace ix { if (!redisClient.publish(channel, message, errMsg)) { - std::cerr << "Error publishing to channel " << channel << "error: " << errMsg - << std::endl; + spdlog::error("Error publishing to channel {} error {}", channel, errMsg); return 1; } } diff --git a/ws/ws_redis_server.cpp b/ws/ws_redis_server.cpp index 49d09a03..d5dc8791 100644 --- a/ws/ws_redis_server.cpp +++ b/ws/ws_redis_server.cpp @@ -4,7 +4,6 @@ * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. */ -#include #include #include #include @@ -20,7 +19,7 @@ namespace ix auto res = server.listen(); if (!res.first) { - std::cerr << res.second << std::endl; + spdlog::info(res.second); return 1; } diff --git a/ws/ws_redis_subscribe.cpp b/ws/ws_redis_subscribe.cpp index 5fc08c3c..4d0b5d57 100644 --- a/ws/ws_redis_subscribe.cpp +++ b/ws/ws_redis_subscribe.cpp @@ -6,8 +6,8 @@ #include #include -#include #include +#include #include #include @@ -22,7 +22,7 @@ namespace ix RedisClient redisClient; if (!redisClient.connect(hostname, port)) { - std::cerr << "Cannot connect to redis host" << std::endl; + spdlog::info("Cannot connect to redis host"); return 1; } @@ -32,10 +32,10 @@ namespace ix if (!redisClient.auth(password, authResponse)) { std::stringstream ss; - std::cerr << "Cannot authenticated to redis" << std::endl; + spdlog::info("Cannot authenticated to redis"); return 1; } - std::cout << "Auth response: " << authResponse << ":" << port << std::endl; + spdlog::info("Auth response: {}", authResponse); } std::atomic msgPerSeconds(0); @@ -44,7 +44,7 @@ namespace ix auto callback = [&msgPerSeconds, &msgCount, verbose](const std::string& message) { if (verbose) { - std::cout << "received: " << message << std::endl; + spdlog::info("recived: {}", message); } msgPerSeconds++; @@ -52,14 +52,13 @@ namespace ix }; auto responseCallback = [](const std::string& redisResponse) { - std::cout << "Redis subscribe response: " << redisResponse << std::endl; + spdlog::info("Redis subscribe response: {}", redisResponse); }; auto timer = [&msgPerSeconds, &msgCount] { while (true) { - std::cout << "#messages " << msgCount << " " - << "msg/s " << msgPerSeconds << std::endl; + spdlog::info("#messages {} msg/s {}", msgCount, msgPerSeconds); msgPerSeconds = 0; auto duration = std::chrono::seconds(1); @@ -69,10 +68,10 @@ namespace ix std::thread t(timer); - std::cerr << "Subscribing to " << channel << "..." << std::endl; + spdlog::info("Subscribing to {} ...", channel); if (!redisClient.subscribe(channel, responseCallback, callback)) { - std::cerr << "Error subscribing to channel " << channel << std::endl; + spdlog::info("Error subscribing to channel {}", channel); return 1; } diff --git a/ws/ws_send.cpp b/ws/ws_send.cpp index f1ea9a36..9d169703 100644 --- a/ws/ws_send.cpp +++ b/ws/ws_send.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -68,12 +68,12 @@ namespace ix void WebSocketSender::log(const std::string& msg) { - std::cout << msg << std::endl; + spdlog::info(msg); } void WebSocketSender::waitForConnection() { - std::cout << "ws_send: Connecting..." << std::endl; + spdlog::info("{}: Connecting...", "ws_send"); std::unique_lock lock(_conditionVariableMutex); _condition.wait(lock); @@ -81,7 +81,7 @@ namespace ix void WebSocketSender::waitForAck() { - std::cout << "ws_send: Waiting for ack..." << std::endl; + spdlog::info("{}: Waiting for ack...", "ws_send"); std::unique_lock lock(_conditionVariableMutex); _condition.wait(lock); @@ -122,11 +122,11 @@ namespace ix _condition.notify_one(); log("ws_send: connected"); - std::cout << "Uri: " << msg->openInfo.uri << std::endl; - std::cout << "Handshake Headers:" << std::endl; + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cout << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) @@ -147,14 +147,14 @@ namespace ix MsgPack data = MsgPack::parse(msg->str, errMsg); if (!errMsg.empty()) { - std::cerr << "Invalid MsgPack response" << std::endl; + spdlog::info("Invalid MsgPack response"); return; } std::string id = data["id"].string_value(); if (_id != id) { - std::cerr << "Invalid id" << std::endl; + spdlog::info("Invalid id"); } } else if (msg->type == ix::WebSocketMessageType::Error) @@ -201,7 +201,7 @@ namespace ix auto milliseconds = std::chrono::duration_cast(now - _start); _ms = milliseconds.count(); - std::cout << _description << " completed in " << _ms << "ms" << std::endl; + spdlog::info("{} completed in {}", _description, _ms); _reported = true; } @@ -240,7 +240,7 @@ namespace ix Bench bench("Sending file through websocket"); _webSocket.sendBinary(msg.dump(), [throttle](int current, int total) -> bool { - std::cout << "ws_send: Step " << current << " out of " << total << std::endl; + spdlog::info("ws_send: Step {} out of {}", current, total); if (throttle) { @@ -254,7 +254,7 @@ namespace ix do { size_t bufferedAmount = _webSocket.bufferedAmount(); - std::cout << "ws_send: " << bufferedAmount << " bytes left to be sent" << std::endl; + spdlog::info("ws_send: {} bytes left to be sent", bufferedAmount); std::chrono::duration duration(10); std::this_thread::sleep_for(duration); @@ -264,7 +264,7 @@ namespace ix auto duration = bench.getDuration(); auto transferRate = 1000 * content.size() / duration; transferRate /= (1024 * 1024); - std::cout << "ws_send: Send transfer rate: " << transferRate << "MB/s" << std::endl; + spdlog::info("ws_send: Send transfer rate: {} MB/s", transferRate); } void wsSend(const std::string& url, @@ -278,12 +278,12 @@ namespace ix webSocketSender.waitForConnection(); - std::cout << "ws_send: Sending..." << std::endl; + spdlog::info("ws_send: Sending..."); webSocketSender.sendMessage(path, throttle); webSocketSender.waitForAck(); - std::cout << "ws_send: Done !" << std::endl; + spdlog::info("ws_send: Done !"); webSocketSender.stop(); } diff --git a/ws/ws_snake.cpp b/ws/ws_snake.cpp index 0c8f4605..f00501e1 100644 --- a/ws/ws_snake.cpp +++ b/ws/ws_snake.cpp @@ -5,8 +5,8 @@ */ #include -#include #include +#include #include namespace @@ -58,11 +58,11 @@ namespace ix auto str = readAsString(appsConfigPath); if (str.empty()) { - std::cout << "Cannot read content of " << appsConfigPath << std::endl; + spdlog::error("Cannot read content of {}", appsConfigPath); return 1; } - std::cout << str << std::endl; + spdlog::error(str); auto apps = nlohmann::json::parse(str); appConfig.apps = apps["apps"]; diff --git a/ws/ws_transfer.cpp b/ws/ws_transfer.cpp index 9413f0da..6d95804b 100644 --- a/ws/ws_transfer.cpp +++ b/ws/ws_transfer.cpp @@ -4,8 +4,8 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ -#include #include +#include #include namespace ix @@ -14,7 +14,7 @@ namespace ix const std::string& hostname, const ix::SocketTLSOptions& tlsOptions) { - std::cout << "ws_transfer: Listening on " << hostname << ":" << port << std::endl; + spdlog::info("Listening on {}:{}", hostname, port); ix::WebSocketServer server(port, hostname); server.setTLSOptions(tlsOptions); @@ -25,22 +25,23 @@ namespace ix const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "ws_transfer: New connection" << std::endl; - std::cerr << "id: " << connectionState->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + spdlog::info("ws_transfer: New connection"); + spdlog::info("id: {}", connectionState->getId()); + spdlog::info("Uri: {}", msg->openInfo.uri); + spdlog::info("Headers:"); for (auto it : msg->openInfo.headers) { - std::cerr << it.first << ": " << it.second << std::endl; + spdlog::info("{}: {}", it.first, it.second); } } else if (msg->type == ix::WebSocketMessageType::Close) { - std::cerr << "ws_transfer: [client " << connectionState->getId() - << "]: Closed connection, code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; + spdlog::info("ws_transfer: Closed connection: client id {} code {} reason {}", + connectionState->getId(), + msg->closeInfo.code, + msg->closeInfo.reason); auto remaining = server.getClients().erase(webSocket); - std::cerr << "ws_transfer: " << remaining << " remaining clients " << std::endl; + spdlog::info("ws_transfer: {} remaining clients", remaining); } else if (msg->type == ix::WebSocketMessageType::Error) { @@ -49,40 +50,39 @@ namespace ix ss << "#retries: " << msg->errorInfo.retries << std::endl; ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); + spdlog::info(ss.str()); } else if (msg->type == ix::WebSocketMessageType::Fragment) { - std::cerr << "ws_transfer: Received message fragment " << std::endl; + spdlog::info("ws_transfer: Received message fragment "); } else if (msg->type == ix::WebSocketMessageType::Message) { - std::cerr << "ws_transfer: Received " << msg->wireSize << " bytes" << std::endl; + spdlog::info("ws_transfer: Received {} bytes", msg->wireSize); size_t receivers = 0; for (auto&& client : server.getClients()) { if (client != webSocket) { auto readyState = client->getReadyState(); + auto id = connectionState->getId(); + if (readyState == ReadyState::Open) { ++receivers; client->send(msg->str, msg->binary, - [id = connectionState->getId()](int current, - int total) -> bool { - std::cerr << "ws_transfer: [client " << id - << "]: Step " << current << " out of " - << total << std::endl; + [&id](int current, int total) -> bool { + spdlog::info("{}: [client {}]: Step {} out of {}", + "ws_transfer", id, current, total); return true; }); - do { size_t bufferedAmount = client->bufferedAmount(); - std::cerr << "ws_transfer: [client " << connectionState->getId() - << "]: " << bufferedAmount - << " bytes left to be sent, " << std::endl; + + spdlog::info("{}: [client {}]: {} bytes left to send", + "ws_transfer", id, bufferedAmount); std::this_thread::sleep_for(std::chrono::milliseconds(500)); @@ -96,16 +96,15 @@ namespace ix ? "Connecting" : readyState == ReadyState::Closing ? "Closing" : "Closed"; size_t bufferedAmount = client->bufferedAmount(); - std::cerr << "ws_transfer: [client " << connectionState->getId() - << "]: has readystate '" << readyStateString << "' and " - << bufferedAmount << " bytes left to be sent, " - << std::endl; + + spdlog::info("{}: [client {}]: has readystate {} bytes left to be sent", + "ws_transfer", id, readyStateString, bufferedAmount); } } } if (!receivers) { - std::cerr << "ws_transfer: no remaining receivers" << std::endl; + spdlog::info("ws_transfer: no remaining receivers"); } } }); @@ -114,7 +113,7 @@ namespace ix auto res = server.listen(); if (!res.first) { - std::cerr << res.second << std::endl; + spdlog::info(res.second); return 1; }