-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_logging.cpp
100 lines (79 loc) · 2.57 KB
/
remote_logging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "remote_logging.hpp"
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#define BEEN_HERE std::cout << __PRETTY_FUNCTION__ << " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl
namespace tmacam {
int MakeSocketFromAddr(const char* hostname, const char* servname) {
const int ERROR = -1;
/* most code from getaddrinfo manpage */
int sfd = ERROR; // socket file destriptor
int s; // for getaddrinfo return
struct addrinfo hints;
struct addrinfo *result, *rp; // will point to the results
/* Obtain address(es) matching host/port */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
s = getaddrinfo(hostname, servname, &hints, &result);
if (s != 0) {
std::ostringstream err_msg;
err_msg << "getaddrinfo() for address '" << hostname <<
":" << servname << "': " << gai_strerror(s);
std::cerr << err_msg.str() << std::endl;
return -1;
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sfd == ERROR) {
continue;
}
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
break; /* Success */
}
close(sfd);
}
if (rp == NULL) { /* No address succeeded */
std::ostringstream err_msg;
err_msg << "Could not connect to '" << hostname << ":" <<
servname << "'.";
std::cerr << err_msg.str() << std::endl;
return ERROR;
}
freeaddrinfo(result); /* No longer needed */
return sfd;
}
std::string RemoteLogging::prefix() const { return prefix_; }
RemoteLogging& RemoteLogging::set_prefix(const std::string& prefix ) {
prefix_ = prefix;
return *this;
}
RemoteLogging& RemoteLogging::set_loghost(const char* host, const char* port) {
skt_ = MakeSocketFromAddr(host, port);
return *this;
}
RemoteLogging& RemoteLogging::Flush() {
std::string log_msg = prefix_ + msg_buffer_.str();
msg_buffer_.str(""); // clear msg_buffer_
if (skt_ ) {
int res;
res = send(skt_, log_msg.c_str(), log_msg.size(), 0);
if(res < 0) {
std::cerr << "LOGGING Oops! send() returned "
<< res << "." << std::endl;
std::cout << log_msg << std::endl;
}
} else {
std::cout << log_msg << std::endl;
}
return *this;
}
}; //namespace tmacam