Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TcpLogging.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_TCPLOGGING_HXX_
37#define _UTILS_TCPLOGGING_HXX_
38
40#include "utils/logging.h"
41#include "utils/Singleton.hxx"
42
45{
47 string data;
48};
49
52
55class FdLoggingServer : public Singleton<FdLoggingServer>,
57{
58public:
67
68private:
70 Action entry() override
71 {
72 if (fd_ < 0)
73 {
74 return release_and_exit();
75 }
76 return write_repeated(&selectHelper_, fd_, input()->data.data(),
77 input()->data.size(), STATE(write_done));
78 }
79
82 {
84 {
85 ::close(fd_);
86 fd_ = -1;
87 }
88 return release_and_exit();
89 }
90
94 {
95 return message()->data();
96 }
97
98protected:
100 int fd_{-1};
101
102private:
105};
106
114void log_output(char *buf, int size)
115{
116 if (size <= 0)
117 return;
119 return;
120 auto *b = Singleton<FdLoggingServer>::instance()->alloc();
121 b->data()->data.reserve(size + 1);
122 b->data()->data.assign(buf, size);
123 b->data()->data.push_back('\n');
125}
126
131{
132public:
141 std::placeholders::_1))
142 {
143 }
144
145private:
148 void on_new_connection(int fd)
149 {
150 if (fd_ >= 0)
151 {
152 ::close(fd_);
153 }
154 fd_ = fd;
155 }
156
159};
160
163{
164public:
172 {
173 fd_ = ::open(name, O_WRONLY);
174 HASSERT(fd_ >= 0);
175 }
176};
177
178#endif // _UTILS_TCPLOGGING_HXX_
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
Definition Socket.cxx:159
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
void log_output(char *buf, int size)
Implementation of the weak symbol to output log lines.
Base class that can receive log buffers from the FdLogging implementation and prints them to a file d...
Action write_done()
Called when the FD write is completed.
FdLoggingServer(Service *service)
Constructor.
int fd_
File descriptor to write log entries to.
Action entry() override
Arrival of new log message.
StateFlowSelectHelper selectHelper_
Helper object for asynchronpus writing.
LogEntry * input()
Helper function to get typed current message.
A list of queues.
Definition Queue.hxx:466
Implementation of the logging proxy that outputs all logs to a serial port.
SerialLoggingServer(Service *service, const char *name)
Constructor.
Collection of related state machines that pend on incoming messages.
Singleton class.
Definition Singleton.hxx:65
static T * instance()
Definition Singleton.hxx:77
Return type for a state flow callback.
Service * service()
Return a pointer to the service I am bound to.
Action write_repeated(StateFlowSelectHelper *helper, int fd, const void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
Writes some data into a file descriptor, repeating the operation as necessary until all bytes are wri...
Action release_and_exit()
Terminates the processing of the current message.
State flow with a given typed input queue.
Implementation of the logging proxy that listens on a TCP port, waits for the first incoming connecti...
void on_new_connection(int fd)
Callback for the TCP server.
SocketListener listener_
Helper class for listening on a TCP socket.
TcpLoggingServer(Service *service, int port)
Constructor.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
Buffer content structure for sending log entries.
string data
Log output line. Contains a trailing newline.
Use this class to read from an fd using select() in a state flow.
unsigned hasError_
1 if there was an error reading of writing.