-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_logging.hpp
117 lines (99 loc) · 3.43 KB
/
remote_logging.hpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef REMOTE_LOGGING_HPP_
#define REMOTE_LOGGING_HPP_
/* Copyright (c) 2009 Tiago Alves Macambira
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <sstream>
#include <string>
namespace tmacam {
/** Make a UDP client socket connected to hostname:servname
*
* Errors returned by the syscalls used by this function are reported to
* stderr.
*
* @return -1 in case of errors, a valid socket file descriptor otherwise.
*/
int MakeSocketFromAddr(const char* hostname, const char* servname);
/**Remote logging made easy with a std. stream-like interface.
*
* This class is to be used in place of std::cout for logging. But instead of
* outputting (only) to the stdout, it will output to a remote
* udp server. If no server is supplied or setup than it will just output
* to std::cout.
*
* An optional prefix can be set and will be prepended to every message "logged"
* using this class.
*
* Notice that, just as logging w/ printf's std::cout, this is a blocking
* logging facility.
*
*/
class RemoteLogging {
int skt_; // the socket file descriptor
std::string prefix_;
std::ostringstream msg_buffer_;
public:
//!Default constructor -- forces logging to stdout.
RemoteLogging() : skt_(0), prefix_() {}
/**Constructor
*
* @param host Hostname of the logging server.
* @param port Port or Serv. name of the logging server.
*
*/
RemoteLogging(const char* host, const char* port)
: skt_(MakeSocketFromAddr(host, port)), prefix_() {}
//!Destructor
~RemoteLogging() {
if (skt_) {
close(skt_);
}
}
std::string prefix() const;
RemoteLogging& set_prefix(const std::string& prefix );
RemoteLogging& set_loghost(const char* host, const char* port);
/** Add msg to the log buffer.
*
* Output will be delayed untill Flush() is called. Applying a
* std::endl to us has the same effect of calling Flush().
*
*/
template <class T> RemoteLogging& operator<<(const T& msg) {
msg_buffer_ << msg;
return *this;
}
//! Force outputing the log buffer contents.
RemoteLogging& Flush();
// Deals with << endl in a sane manner.
// http://bytes.com/topic/c/answers/128046-std-endl-type-unknown
inline RemoteLogging& operator<<( std::ostream& (*f)(std::ostream&) ) {
return this->Flush();
}
private:
// Disallow evil constructors
RemoteLogging(const RemoteLogging&);
RemoteLogging& operator=(const RemoteLogging&);
};
}; // namespace tmacam
#endif /* REMOTE_LOGGING_HPP_ */