From ae58a2dcda266b3e75982e0fb18c2710d5348ab4 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 26 Apr 2012 16:45:00 +0200 Subject: [PATCH] Cleaned up logging. Implemented TCPSocket::Get*Address() --- base/application.cpp | 29 +++---- base/application.h | 2 +- base/i2-base.h | 23 ++--- base/tcpsocket.cpp | 85 +++++++++++++++++++ base/tcpsocket.h | 10 +++ base/win32.h | 4 +- components/configfile/configfilecomponent.cpp | 2 +- components/demo/democomponent.cpp | 4 +- icinga/endpointmanager.cpp | 9 +- icinga/icingaapplication.cpp | 4 +- icinga/jsonrpcendpoint.cpp | 18 ++-- 11 files changed, 142 insertions(+), 48 deletions(-) diff --git a/base/application.cpp b/base/application.cpp index aff6e23a9..2e9fb961e 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -195,7 +195,7 @@ Component::Ptr Application::LoadComponent(const string& path, Component::Ptr component; Component *(*pCreateComponent)(); - Log("Loading component '%s'", path.c_str()); + Log("Loading component '" + path + "'"); #ifdef _WIN32 HMODULE hModule = LoadLibrary(path.c_str()); @@ -250,7 +250,7 @@ void Application::UnregisterComponent(Component::Ptr component) { string name = component->GetName(); - Log("Unloading component '%s'", name.c_str()); + Log("Unloading component '" + name + "'"); map::iterator i = m_Components.find(name); if (i != m_Components.end()) m_Components.erase(i); @@ -281,20 +281,19 @@ Component::Ptr Application::GetComponent(const string& name) * * Logs a message. * - * @param format The format string. - * @param ... Additional parameters for the format string. + * @param message The message. */ -void Application::Log(const char *format, ...) +void Application::Log(string message) { - char message[512]; - va_list marker; + char timestamp[100]; - va_start(marker, format); - vsnprintf(message, sizeof(message), format, marker); - va_end(marker); + time_t now; + time(&now); + tm tmnow = *localtime(&now); - // TODO: log to file - fprintf(stderr, "%s\n", message); + strftime(timestamp, sizeof(timestamp), "%a %B %d %Y %H:%M:%S", &tmnow); + + cout << "[" << timestamp << "]: " << message << endl; } /** @@ -491,9 +490,9 @@ int icinga::RunApplication(int argc, char **argv, Application *instance) try { result = Application::Instance->Main(args); } catch (const Exception& ex) { - cerr << "---" << endl; - cerr << "Exception: " << Utility::GetTypeName(ex) << endl; - cerr << "Message: " << ex.GetMessage() << endl; + Application::Log("---"); + Application::Log("Exception: " + Utility::GetTypeName(ex)); + Application::Log("Message: " + ex.GetMessage()); return EXIT_FAILURE; } diff --git a/base/application.h b/base/application.h index 912ef4336..94d3d66a8 100644 --- a/base/application.h +++ b/base/application.h @@ -35,7 +35,7 @@ public: void Shutdown(void); - static void Log(const char *format, ...); + static void Log(string message); ConfigHive::Ptr GetConfigHive(void) const; diff --git a/base/i2-base.h b/base/i2-base.h index 95889e6fb..5e726bbd3 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -9,6 +9,17 @@ # include "config.h" #endif /* _MSC_VER */ +#define PLATFORM_WINDOWS 1 +#define PLATFORM_UNIX 2 + +#ifdef _WIN32 +# define I2_PLATFORM PLATFORM_WINDOWS +# include "win32.h" +#else +# define I2_PLATFORM PLATFORM_UNIX +# include "unix.h" +#endif + #include #include #include @@ -18,6 +29,7 @@ #include #include +#include #include #include #include @@ -55,17 +67,6 @@ using namespace std::tr1::placeholders; # include "cxx11-compat.h" #endif -#define PLATFORM_WINDOWS 1 -#define PLATFORM_UNIX 2 - -#ifdef _WIN32 -# define I2_PLATFORM PLATFORM_WINDOWS -# include "win32.h" -#else -# define I2_PLATFORM PLATFORM_UNIX -# include "unix.h" -#endif - #ifdef I2_BASE_BUILD # define I2_BASE_API I2_EXPORT #else /* I2_BASE_BUILD */ diff --git a/base/tcpsocket.cpp b/base/tcpsocket.cpp index f1120ce5b..3e9bbf995 100644 --- a/base/tcpsocket.cpp +++ b/base/tcpsocket.cpp @@ -38,3 +38,88 @@ void TCPSocket::Bind(const char *hostname, unsigned short port) if (::bind(GetFD(), (sockaddr *)&sin, sizeof(sin)) < 0) HandleSocketError(); } + +string TCPSocket::GetAddressFromSockaddr(sockaddr *address) +{ + static char Buffer[256]; + +#ifdef _WIN32 + DWORD BufferLength = sizeof(Buffer); + + socklen_t len; + if (address->sa_family == AF_INET) + len = sizeof(sockaddr_in); + else if (address->sa_family == AF_INET6) + len = sizeof(sockaddr_in6); + else { + assert(0); + + return ""; + } + + if (WSAAddressToString(address, len, NULL, Buffer, &BufferLength) != 0) { + return NULL; + } +#else /* _WIN32 */ + void *IpAddress; + + if (Address->sa_family == AF_INET) { + IpAddress = &(((sockaddr_in *)Address)->sin_addr); + } else { + IpAddress = &(((sockaddr_in6 *)Address)->sin6_addr); + } + + if (inet_ntop(Address->sa_family, IpAddress, Buffer, sizeof(Buffer)) == NULL) { + return NULL; + } +#endif /* _WIN32 */ + + return Buffer; +} + +unsigned short TCPSocket::GetPortFromSockaddr(sockaddr *address) +{ + if (address->sa_family == AF_INET) + return htons(((sockaddr_in *)address)->sin_port); + else if (address->sa_family == AF_INET6) + return htons(((sockaddr_in6 *)address)->sin6_port); + else { + assert(0); + + return 0; + } +} + +void TCPSocket::GetClientSockaddr(sockaddr_storage *address) +{ + socklen_t len = sizeof(*address); + + if (getsockname(GetFD(), (sockaddr *)address, &len) < 0) + HandleSocketError(); +} + +void TCPSocket::GetPeerSockaddr(sockaddr_storage *address) +{ + socklen_t len = sizeof(*address); + + if (getpeername(GetFD(), (sockaddr *)address, &len) < 0) + HandleSocketError(); +} + +string TCPSocket::GetClientAddress(void) +{ + sockaddr_storage sin; + + GetClientSockaddr(&sin); + + return GetAddressFromSockaddr((sockaddr *)&sin); +} + +string TCPSocket::GetPeerAddress(void) +{ + sockaddr_storage sin; + + GetPeerSockaddr(&sin); + + return GetAddressFromSockaddr((sockaddr *)&sin); +} diff --git a/base/tcpsocket.h b/base/tcpsocket.h index 8a3feb50d..f374d771e 100644 --- a/base/tcpsocket.h +++ b/base/tcpsocket.h @@ -6,6 +6,10 @@ namespace icinga class I2_BASE_API TCPSocket : public Socket { +private: + static string GetAddressFromSockaddr(sockaddr *address); + static unsigned short GetPortFromSockaddr(sockaddr *address); + public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; @@ -14,6 +18,12 @@ public: void Bind(unsigned short port); void Bind(const char *hostname, unsigned short port); + + void GetClientSockaddr(sockaddr_storage *address); + void GetPeerSockaddr(sockaddr_storage *address); + + string TCPSocket::GetClientAddress(void); + string TCPSocket::GetPeerAddress(void); }; } diff --git a/base/win32.h b/base/win32.h index fe6d5ba6f..d189a775c 100644 --- a/base/win32.h +++ b/base/win32.h @@ -1,8 +1,10 @@ #ifndef WIN32_H #define WIN32_H -#define NOGDI +#define WIN32_LEAN_AND_MEAN #include +#include +#include #include #include diff --git a/components/configfile/configfilecomponent.cpp b/components/configfile/configfilecomponent.cpp index bbd407583..4406e6e8c 100644 --- a/components/configfile/configfilecomponent.cpp +++ b/components/configfile/configfilecomponent.cpp @@ -23,7 +23,7 @@ void ConfigFileComponent::Start(void) if (fp.fail()) throw ConfigParserException("Could not open config file"); - GetApplication()->Log("Reading config file: %s", filename.c_str()); + GetApplication()->Log("Reading config file: " + filename); while (!fp.eof()) { size_t bufferSize = 1024; diff --git a/components/demo/democomponent.cpp b/components/demo/democomponent.cpp index dd370b08d..2c9de30b0 100644 --- a/components/demo/democomponent.cpp +++ b/components/demo/democomponent.cpp @@ -52,7 +52,7 @@ int DemoComponent::NewEndpointHandler(const NewEndpointEventArgs& neea) int DemoComponent::DemoTimerHandler(const TimerEventArgs& tea) { - cout << "Sending multicast 'hello world' message." << endl; + Application::Log("Sending multicast 'hello world' message."); JsonRpcRequest request; request.SetMethod("demo::HelloWorld"); @@ -65,7 +65,7 @@ int DemoComponent::DemoTimerHandler(const TimerEventArgs& tea) int DemoComponent::HelloWorldRequestHandler(const NewRequestEventArgs& nrea) { - cout << "Got 'hello world' from " << nrea.Sender->GetAddress() << endl; + Application::Log("Got 'hello world' from address:" + nrea.Sender->GetAddress() + ", identity:" + nrea.Sender->GetIdentity()); return 0; } diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index 7649ed3b9..c6b028068 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -9,7 +9,7 @@ EndpointManager::EndpointManager(shared_ptr sslContext) void EndpointManager::AddListener(unsigned short port) { - Application::Log("Adding new listener: port %d", port); + Application::Log("Adding new listener: port " + port); JsonRpcServer::Ptr server = make_shared(m_SSLContext); RegisterServer(server); @@ -22,7 +22,9 @@ void EndpointManager::AddListener(unsigned short port) void EndpointManager::AddConnection(string host, unsigned short port) { - Application::Log("Adding new endpoint: %s:%d", host.c_str(), port); + stringstream s; + s << "Adding new endpoint: " << host << ":" << port; + Application::Log(s.str()); JsonRpcEndpoint::Ptr endpoint = make_shared(); endpoint->Connect(host, port, m_SSLContext); @@ -37,7 +39,8 @@ void EndpointManager::RegisterServer(JsonRpcServer::Ptr server) int EndpointManager::NewClientHandler(const NewClientEventArgs& ncea) { - Application::Log("Accepted new client"); + string address = ncea.Client->GetPeerAddress(); + Application::Log("Accepted new client from " + address); JsonRpcEndpoint::Ptr endpoint = make_shared(); endpoint->SetClient(static_pointer_cast(ncea.Client)); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index 6683ccd13..2b3ed95b5 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -12,9 +12,9 @@ using namespace icinga; int IcingaApplication::Main(const vector& args) { #ifdef _WIN32 - cout << "Icinga component loader" << endl; + Application::Log("Icinga component loader"); #else /* _WIN32 */ - cout << "Icinga component loader (version: " << ICINGA_VERSION << ")" << endl; + Application::Log("Icinga component loader (version: " ICINGA_VERSION ")"); #endif /* _WIN32 */ if (args.size() < 2) { diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index 25e3d10ce..2b3dacaea 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -2,14 +2,12 @@ using namespace icinga; -void JsonRpcEndpoint::SetAddress(string address) -{ - m_Address = address; -} - string JsonRpcEndpoint::GetAddress(void) const { - return m_Address; + if (!m_Client) + return ""; + + return m_Client->GetPeerAddress(); } JsonRpcClient::Ptr JsonRpcEndpoint::GetClient(void) @@ -61,10 +59,6 @@ bool JsonRpcEndpoint::IsAllowedMethodSource(string method) const void JsonRpcEndpoint::Connect(string host, unsigned short port, shared_ptr sslContext) { - char portStr[20]; - sprintf(portStr, "%d", port); - SetAddress("jsonrpc-tcp://" + host + ":" + portStr); - JsonRpcClient::Ptr client = make_shared(RoleOutbound, sslContext); client->MakeSocket(); client->Connect(host, port); @@ -142,7 +136,7 @@ int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea) int JsonRpcEndpoint::ClientClosedHandler(const EventArgs& ea) { string address = GetAddress(); - Application::Log("Lost connection to endpoint: %s", address.c_str()); + Application::Log("Lost connection to endpoint: " + address); m_PendingCalls.clear(); @@ -154,7 +148,7 @@ int JsonRpcEndpoint::ClientClosedHandler(const EventArgs& ea) timer->Start(); m_ReconnectTimer = timer; - Application::Log("Spawned reconnect timer (30 seconds)", address.c_str()); + Application::Log("Spawned reconnect timer (30 seconds)"); } // TODO: _only_ clear non-persistent method sources/sinks