]> granicus.if.org Git - icinga2/commitdiff
Cleaned up logging.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 26 Apr 2012 14:45:00 +0000 (16:45 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 26 Apr 2012 14:50:47 +0000 (16:50 +0200)
Implemented TCPSocket::Get*Address()

base/application.cpp
base/application.h
base/i2-base.h
base/tcpsocket.cpp
base/tcpsocket.h
base/win32.h
components/configfile/configfilecomponent.cpp
components/demo/democomponent.cpp
icinga/endpointmanager.cpp
icinga/icingaapplication.cpp
icinga/jsonrpcendpoint.cpp

index aff6e23a9278e60d8f9fb354768250c31d65197b..2e9fb961edaa452b66de61e55b4ce78cc4f6a713 100644 (file)
@@ -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<string, Component::Ptr>::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;
                }
index 912ef433674124f7d84da9822fc7895d9ef1a6d2..94d3d66a85bea35fabfa1dd47e688e79dcd68838 100644 (file)
@@ -35,7 +35,7 @@ public:
 
        void Shutdown(void);
 
-       static void Log(const char *format, ...);
+       static void Log(string message);
 
        ConfigHive::Ptr GetConfigHive(void) const;
 
index 95889e6fb1dc52a2d65faac43ce42afa8ce5f26e..5e726bbd3bed90ba5fb2cdb397bc5ad1db29f30e 100644 (file)
@@ -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 <cstdlib>
 #include <cstdarg>
 #include <cstdio>
@@ -18,6 +29,7 @@
 
 #include <memory>
 #include <string>
+#include <sstream>
 #include <vector>
 #include <set>
 #include <iostream>
@@ -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 */
index f1120ce5b0545be096e9c72bea651da696ab560f..3e9bbf995b1500fe4a4609bb5a031082ab7ef1b9 100644 (file)
@@ -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);
+}
index 8a3feb50dde436163c8b14fa199099c914c3ac0e..f374d771e66ca550455218cc38a912cdad2d6f8b 100644 (file)
@@ -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<TCPSocket> Ptr;
        typedef weak_ptr<TCPSocket> 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);
 };
 
 }
index fe6d5ba6f8e2acbff2609197becd345f66521bb2..d189a775cc5e89cdce2e3882a065bd8662581e76 100644 (file)
@@ -1,8 +1,10 @@
 #ifndef WIN32_H
 #define WIN32_H
 
-#define NOGDI
+#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
 #include <imagehlp.h>
 #include <shlwapi.h>
 
index bbd40758342bece64b25a38a841ceee73ba68b8e..4406e6e8c6a4dae6cbf67903231acd92be39a3a1 100644 (file)
@@ -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;
index dd370b08dc8efa3dfd8fc9cca11f4080e85ea9c7..2c9de30b050e8bb847be67896a643feb784727e9 100644 (file)
@@ -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;
 }
index 7649ed3b9fb2b8bc8fcd7d96226e34427a905fb1..c6b028068dca46f727e5eeb5cec4d7738365832c 100644 (file)
@@ -9,7 +9,7 @@ EndpointManager::EndpointManager(shared_ptr<SSL_CTX> 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<JsonRpcServer>(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<JsonRpcEndpoint>();
        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<JsonRpcEndpoint>();
        endpoint->SetClient(static_pointer_cast<JsonRpcClient>(ncea.Client));
index 6683ccd1379b4046bf10223c6cf71f23b59fbb2b..2b3ed95b517d97f928b2621f864bb3b633742bb0 100644 (file)
@@ -12,9 +12,9 @@ using namespace icinga;
 int IcingaApplication::Main(const vector<string>& 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) {
index 25e3d10ce57522d52c3f23b46779d8c2cbedb545..2b3dacaea646190e4da4bb2c2543f82352c6f14d 100644 (file)
@@ -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 "<disconnected endpoint>";
+
+       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<SSL_CTX> sslContext)
 {
-       char portStr[20];
-       sprintf(portStr, "%d", port);
-       SetAddress("jsonrpc-tcp://" + host + ":" + portStr);
-
        JsonRpcClient::Ptr client = make_shared<JsonRpcClient>(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