summaryrefslogtreecommitdiff
path: root/polux/application/agent/src/agent_remote_log.cpp
diff options
context:
space:
mode:
authorJean-Philippe SAVE2012-02-20 16:38:56 +0100
committerJean-Philippe SAVE2012-02-20 16:38:56 +0100
commit55a15cc820e926219ebce47218ce1e2f35bb0c48 (patch)
treedba3ff39a766e47859ab7fd837d8da5d30b56b1f /polux/application/agent/src/agent_remote_log.cpp
parent1353d3215782b997fdec3f9182cbda547d92d7e9 (diff)
parentcfc4d43d4d19c398d994b75cb1eeda3c499bd234 (diff)
Add polux base by subtree merge
Diffstat (limited to 'polux/application/agent/src/agent_remote_log.cpp')
-rw-r--r--polux/application/agent/src/agent_remote_log.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/polux/application/agent/src/agent_remote_log.cpp b/polux/application/agent/src/agent_remote_log.cpp
new file mode 100644
index 0000000000..2b338ce1f6
--- /dev/null
+++ b/polux/application/agent/src/agent_remote_log.cpp
@@ -0,0 +1,79 @@
+#ifdef REMOTE_LOG
+
+#include "agent_remote_log.h"
+// socket part
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/un.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <signal.h>
+
+#include <sm_agent_log_entry.h>
+
+AgentRemoteLog::AgentRemoteLog(char* ipAddress,int portNb) {
+ // open the socket to send datas
+ struct sockaddr_in servaddr;
+
+ isConnected = false;
+ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
+ // socket error
+ return; // return without opening the socket
+ }
+ bzero(&servaddr, sizeof(servaddr));
+
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = htons(portNb);
+
+ if (inet_pton(AF_INET, ipAddress , &servaddr.sin_addr) <= 0){
+ // inet_pton error
+ return; // return without opening the socket
+ }
+
+ if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){
+ // connect error
+ return; // return without opening the socket
+ }
+ isConnected = true;
+};
+
+AgentRemoteLog::~AgentRemoteLog() {
+ if (isConnected) {
+ close(sockfd);
+ }
+};
+
+LogEntry* AgentRemoteLog::create_log_entry(unsigned char t) const
+{
+ return new SMAgentLogEntry(t);
+}
+
+AgentLog& AgentRemoteLog::operator+=(const LogEntry* log) {
+ // send the log to the server through the socket
+
+ if ( !isConnected ) {
+ return *this; // return without doing anything
+ }
+
+ // write the text to the socket
+ if ( write(sockfd,log->get_value(),strlen(log->get_value())) < 0 ) {
+ // write error
+ return *this; // return without doing anything
+ }
+ if ( write(sockfd,"\n",1) < 0 ) { // add a new line
+ // write error
+ return *this; // return without doing anything
+ }
+
+
+ // check if critical error
+ if ((log->get_class() == ERROR_LOG) && (log->get_level() == 0)) {
+ raise(SIGTERM);
+ }
+
+ return *this;
+};
+
+#endif // REMOTE_LOG