diff --git a/services/tunnelbroker/src/Amqp/AmqpManager.cpp b/services/tunnelbroker/src/Amqp/AmqpManager.cpp --- a/services/tunnelbroker/src/Amqp/AmqpManager.cpp +++ b/services/tunnelbroker/src/Amqp/AmqpManager.cpp @@ -4,6 +4,8 @@ #include "DeliveryBroker.h" #include "Tools.h" +#include + #include namespace comm { @@ -23,7 +25,7 @@ const std::string fanoutExchangeName = config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_AMQP_FANOUT_EXCHANGE); - std::cout << "AMQP: Connecting to " << amqpUri << std::endl; + LOG(INFO) << "AMQP: Connecting to " << amqpUri; auto *loop = uv_default_loop(); AMQP::LibUvHandler handler(loop); @@ -31,8 +33,8 @@ this->amqpChannel = std::make_unique(&connection); this->amqpChannel->onError([this](const char *message) { - std::cout << "AMQP: channel error: " << message << ", will try to reconnect" - << std::endl; + LOG(ERROR) << "AMQP: channel error: " << message + << ", will try to reconnect"; this->amqpReady = false; }); @@ -45,12 +47,12 @@ const std::string &name, uint32_t messagecount, uint32_t consumercount) { - std::cout << "AMQP: Queue " << name << " created" << std::endl; + LOG(INFO) << "AMQP: Queue " << name << " created"; this->amqpChannel->bindQueue(fanoutExchangeName, tunnelbrokerID, "") .onError([this, tunnelbrokerID, fanoutExchangeName]( const char *message) { - std::cout << "AMQP: Failed to bind queue: " << tunnelbrokerID - << " to exchange: " << fanoutExchangeName << std::endl; + LOG(ERROR) << "AMQP: Failed to bind queue: " << tunnelbrokerID + << " to exchange: " << fanoutExchangeName; this->amqpReady = false; }); this->amqpReady = true; @@ -65,18 +67,16 @@ const std::string toDeviceID(headers[AMQP_HEADER_TO_DEVICEID]); const std::string fromDeviceID( headers[AMQP_HEADER_FROM_DEVICEID]); - std::cout << "AMQP: Message consumed for deviceID: " - << toDeviceID << std::endl; + LOG(INFO) << "AMQP: Message consumed for deviceID: " + << toDeviceID; DeliveryBroker::getInstance().push( messageID, deliveryTag, toDeviceID, fromDeviceID, payload); } catch (const std::exception &e) { - std::cout << "AMQP: Message parsing exception: " << e.what() - << std::endl; + LOG(ERROR) << "AMQP: Message parsing exception: " << e.what(); } }) .onError([](const char *message) { - std::cout << "AMQP: Error on message consume: " << message - << std::endl; + LOG(ERROR) << "AMQP: Error on message consume: " << message; }); }) .onError([](const char *message) { @@ -110,7 +110,7 @@ std::string toDeviceID, std::string payload) { if (!this->amqpReady) { - std::cout << "AMQP: Message send error: channel not ready" << std::endl; + LOG(ERROR) << "AMQP: Message send error: channel not ready"; return false; } try { @@ -128,8 +128,7 @@ "", env); } catch (std::runtime_error &e) { - std::cout << "AMQP: Error while publishing message: " << e.what() - << std::endl; + LOG(ERROR) << "AMQP: Error while publishing message: " << e.what(); return false; } return true; @@ -137,7 +136,7 @@ void AmqpManager::ack(uint64_t deliveryTag) { if (!this->amqpReady) { - std::cout << "AMQP: Message ACK error: channel not ready" << std::endl; + LOG(ERROR) << "AMQP: Message ACK error: channel not ready"; return; } this->amqpChannel->ack(deliveryTag); diff --git a/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.cpp b/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.cpp --- a/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.cpp +++ b/services/tunnelbroker/src/DeliveryBroker/DeliveryBroker.cpp @@ -1,5 +1,7 @@ #include "DeliveryBroker.h" +#include + namespace comm { namespace network { @@ -28,8 +30,8 @@ .fromDeviceID = fromDeviceID, .payload = payload}); } catch (const std::exception &e) { - std::cout << "DeliveryBroker push: " - << "Got an exception " << e.what() << std::endl; + LOG(ERROR) << "DeliveryBroker push: " + << "Got an exception " << e.what(); } }; @@ -54,8 +56,8 @@ this->messagesMap.find(deviceID)->second->blockingRead(receievedMessage); return receievedMessage; } catch (const std::exception &e) { - std::cout << "DeliveryBroker pop: " - << "Got an exception " << e.what() << std::endl; + LOG(ERROR) << "DeliveryBroker pop: " + << "Got an exception " << e.what(); } return {}; }; diff --git a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp --- a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp +++ b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp @@ -1,5 +1,4 @@ #include "TunnelbrokerServiceImpl.h" - #include "AmqpManager.h" #include "AwsTools.h" #include "ConfigManager.h" @@ -8,6 +7,8 @@ #include "DeliveryBroker.h" #include "Tools.h" +#include + namespace comm { namespace network { @@ -42,8 +43,8 @@ tunnelbroker::SessionSignatureResponse *reply) { const std::string deviceID = request->deviceid(); if (!tools::validateDeviceID(deviceID)) { - std::cout << "gRPC: " - << "Format validation failed for " << deviceID << std::endl; + LOG(INFO) << "gRPC: " + << "Format validation failed for " << deviceID; return grpc::Status( grpc::StatusCode::INVALID_ARGUMENT, "Format validation failed for deviceID"); @@ -67,8 +68,8 @@ std::shared_ptr publicKeyItem; const std::string deviceID = request->deviceid(); if (!tools::validateDeviceID(deviceID)) { - std::cout << "gRPC: " - << "Format validation failed for " << deviceID << std::endl; + LOG(INFO) << "gRPC: " + << "Format validation failed for " << deviceID; return grpc::Status( grpc::StatusCode::INVALID_ARGUMENT, "Format validation failed for deviceID"); @@ -80,9 +81,8 @@ sessionSignItem = database::DatabaseManager::getInstance().findSessionSignItem(deviceID); if (sessionSignItem == nullptr) { - std::cout << "gRPC: " - << "Session sign request not found for deviceID: " << deviceID - << std::endl; + LOG(INFO) << "gRPC: " + << "Session sign request not found for deviceID: " << deviceID; return grpc::Status( grpc::StatusCode::NOT_FOUND, "Session sign request not found"); } @@ -94,8 +94,8 @@ database::DatabaseManager::getInstance().putPublicKeyItem( *newPublicKeyItem); } else if (publicKey != publicKeyItem->getPublicKey()) { - std::cout << "gRPC: " - << "The public key doesn't match for deviceID" << std::endl; + LOG(INFO) << "gRPC: " + << "The public key doesn't match for deviceID"; return grpc::Status( grpc::StatusCode::PERMISSION_DENIED, "The public key doesn't match for deviceID"); @@ -103,9 +103,8 @@ const std::string verificationMessage = sessionSignItem->getSign(); if (!comm::network::crypto::rsaVerifyString( publicKey, verificationMessage, signature)) { - std::cout << "gRPC: " - << "Signature for the verification message is not valid" - << std::endl; + LOG(INFO) << "gRPC: " + << "Signature for the verification message is not valid"; return grpc::Status( grpc::StatusCode::PERMISSION_DENIED, "Signature for the verification message is not valid"); @@ -122,9 +121,8 @@ request->deviceos()); database::DatabaseManager::getInstance().putSessionItem(*deviceSessionItem); } catch (std::runtime_error &e) { - std::cout << "gRPC: " - << "Error while processing 'NewSession' request: " << e.what() - << std::endl; + LOG(ERROR) << "gRPC: " + << "Error while processing 'NewSession' request: " << e.what(); return grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } reply->set_sessionid(newSessionID); @@ -138,8 +136,8 @@ try { const std::string sessionID = request->sessionid(); if (!tools::validateSessionID(sessionID)) { - std::cout << "gRPC: " - << "Format validation failed for " << sessionID << std::endl; + LOG(INFO) << "gRPC: " + << "Format validation failed for " << sessionID; return grpc::Status( grpc::StatusCode::INVALID_ARGUMENT, "Format validation failed for sessionID"); @@ -147,8 +145,8 @@ std::shared_ptr sessionItem = database::DatabaseManager::getInstance().findSessionItem(sessionID); if (sessionItem == nullptr) { - std::cout << "gRPC: " - << "Session " << sessionID << " not found" << std::endl; + LOG(INFO) << "gRPC: " + << "Session " << sessionID << " not found"; return grpc::Status( grpc::StatusCode::PERMISSION_DENIED, "No such session found. SessionID: " + sessionID); @@ -160,16 +158,15 @@ clientDeviceID, request->todeviceid(), std::string(request->payload()))) { - std::cout << "gRPC: " - << "Error while publish the message to AMQP" << std::endl; + LOG(ERROR) << "gRPC: " + << "Error while publish the message to AMQP"; return grpc::Status( grpc::StatusCode::INTERNAL, "Error while publish the message to AMQP"); } } catch (std::runtime_error &e) { - std::cout << "gRPC: " - << "Error while processing 'Send' request: " << e.what() - << std::endl; + LOG(ERROR) << "gRPC: " + << "Error while processing 'Send' request: " << e.what(); return grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } return grpc::Status::OK; @@ -182,8 +179,8 @@ try { const std::string sessionID = request->sessionid(); if (!tools::validateSessionID(sessionID)) { - std::cout << "gRPC: " - << "Format validation failed for " << sessionID << std::endl; + LOG(INFO) << "gRPC: " + << "Format validation failed for " << sessionID; return grpc::Status( grpc::StatusCode::INVALID_ARGUMENT, "Format validation failed for sessionID"); @@ -191,8 +188,8 @@ std::shared_ptr sessionItem = database::DatabaseManager::getInstance().findSessionItem(sessionID); if (sessionItem == nullptr) { - std::cout << "gRPC: " - << "Session " << sessionID << " not found" << std::endl; + LOG(INFO) << "gRPC: " + << "Session " << sessionID << " not found"; return grpc::Status( grpc::StatusCode::PERMISSION_DENIED, "No such session found. SessionID: " + sessionID); @@ -215,9 +212,8 @@ } } } catch (std::runtime_error &e) { - std::cout << "gRPC: " - << "Error while processing 'Get' request: " << e.what() - << std::endl; + LOG(ERROR) << "gRPC: " + << "Error while processing 'Get' request: " << e.what(); return grpc::Status(grpc::StatusCode::INTERNAL, e.what()); } return grpc::Status::OK; diff --git a/services/tunnelbroker/src/Tools/CryptoTools.cpp b/services/tunnelbroker/src/Tools/CryptoTools.cpp --- a/services/tunnelbroker/src/Tools/CryptoTools.cpp +++ b/services/tunnelbroker/src/Tools/CryptoTools.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -35,8 +36,8 @@ reinterpret_cast(decodedSignature.c_str()), decodedSignature.length()); } catch (const std::exception &e) { - std::cout << "CryptoTools: " - << "Got an exception " << e.what() << std::endl; + LOG(ERROR) << "CryptoTools: " + << "Got an exception " << e.what(); return false; } } diff --git a/services/tunnelbroker/src/Tools/Tools.cpp b/services/tunnelbroker/src/Tools/Tools.cpp --- a/services/tunnelbroker/src/Tools/Tools.cpp +++ b/services/tunnelbroker/src/Tools/Tools.cpp @@ -2,6 +2,7 @@ #include "ConfigManager.h" #include "Constants.h" +#include #include #include #include @@ -45,9 +46,8 @@ } return std::regex_match(deviceID, DEVICEID_FORMAT_REGEX); } catch (const std::exception &e) { - std::cout << "Tools: " - << "Got an exception at `validateDeviceID`: " << e.what() - << std::endl; + LOG(ERROR) << "Tools: " + << "Got an exception at `validateDeviceID`: " << e.what(); return false; } } @@ -61,9 +61,8 @@ try { return std::regex_match(sessionID, SESSION_ID_FORMAT_REGEX); } catch (const std::exception &e) { - std::cout << "Tools: " - << "Got an exception at `validateSessionId`: " << e.what() - << std::endl; + LOG(ERROR) << "Tools: " + << "Got an exception at `validateSessionId`: " << e.what(); return false; } } diff --git a/services/tunnelbroker/src/server.cpp b/services/tunnelbroker/src/server.cpp --- a/services/tunnelbroker/src/server.cpp +++ b/services/tunnelbroker/src/server.cpp @@ -3,6 +3,7 @@ #include "Constants.h" #include "TunnelbrokerServiceImpl.h" +#include #include #include @@ -23,8 +24,7 @@ // clients. In this case it corresponds to an *synchronous* service. builder.RegisterService(&service); std::unique_ptr server(builder.BuildAndStart()); - std::cout << "gRPC Server listening at :" << SERVER_LISTEN_ADDRESS - << std::endl; + LOG(INFO) << "gRPC Server listening at :" << SERVER_LISTEN_ADDRESS; // Wait for the server to shutdown. Note that some other thread must be // responsible for shutting down the server for this call to ever return. server->Wait(); @@ -38,8 +38,8 @@ } // namespace comm int main(int argc, char **argv) { + google::InitGoogleLogging(argv[0]); comm::network::config::ConfigManager::getInstance().load(); - std::thread amqpThread(comm::network::RunAmqpClient); std::thread grpcThread(comm::network::RunServer); amqpThread.join();