Fawkes API Fawkes Development Version
main.cpp
1
2/***************************************************************************
3 * main.cpp - Fawkes network log view
4 *
5 * Created: Sat Dec 15 01:57:20 2007 (after I5 xmas party)
6 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include <logging/console.h>
24#include <netcomm/fawkes/client.h>
25#include <netcomm/fawkes/client_handler.h>
26#include <netcomm/fawkes/component_ids.h>
27#include <network_logger/network_logger.h>
28#include <utils/system/argparser.h>
29#include <utils/system/signal.h>
30
31#include <cstdio>
32#include <cstdlib>
33#include <cstring>
34
35using namespace fawkes;
36
37/// @cond INTERNALS
38
39class NetLogConsolePrinter : public FawkesNetworkClientHandler, public SignalHandler
40{
41public:
42 explicit NetLogConsolePrinter(const char *hostport)
43 {
44 logger = new ConsoleLogger();
45 quit = false;
46
47 char * hp = strdup(hostport);
48 const char *hostname = strtok(hp, ":");
49 const char *portstr = strtok(NULL, "");
50 int port = 1910;
51 if (portstr) {
52 port = atoi(portstr);
53 if ((port < 0) || (port > 0xFFFF)) {
54 printf("Invalid port given, must be in range [1:65535]. Using default 1910 instead\n");
55 port = 1910;
56 }
57 }
58
59 client = new FawkesNetworkClient(hostname, port);
60 client->connect();
61 client->register_handler(this, FAWKES_CID_NETWORKLOGGER);
62
63 client->enqueue(
64 new FawkesNetworkMessage(FAWKES_CID_NETWORKLOGGER, NetworkLogger::MSGTYPE_SUBSCRIBE));
65 }
66
67 ~NetLogConsolePrinter()
68 {
69 delete logger;
70 delete client;
71 }
72
73 void
74 handle_signal(int signal)
75 {
76 quit = true;
77 client->wake(FAWKES_CID_NETWORKLOGGER);
78 }
79
80 virtual void
81 inbound_received(FawkesNetworkMessage *m, unsigned int id) noexcept
82 {
83 if ((m->cid() == FAWKES_CID_NETWORKLOGGER)
84 && (m->msgid() == NetworkLogger::MSGTYPE_LOGMESSAGE)) {
86 struct timeval t = content->get_time();
87 logger->tlog(
88 content->get_loglevel(), &t, content->get_component(), "%s", content->get_message());
89 }
90 }
91
92 virtual void
93 deregistered(unsigned int id) noexcept
94 {
95 quit = true;
96 }
97
98 virtual void
99 connection_died(unsigned int id) noexcept
100 {
101 printf("Connection to host died. Aborting.\n");
102 quit = true;
103 }
104
105 virtual void
106 connection_established(unsigned int id) noexcept
107 {
108 }
109
110 void
111 run()
112 {
113 while (!quit) {
114 client->wait(FAWKES_CID_NETWORKLOGGER);
115 }
116 client->disconnect();
117 }
118
119private:
120 FawkesNetworkClient *client;
121 ConsoleLogger * logger;
122 bool quit;
123};
124/// @endcond
125
126void
127print_usage(const char *program_name)
128{
129 printf("Usage: %s [hostname[:port]]\n", program_name);
130}
131
132int
133main(int argc, char **argv)
134{
135 ArgumentParser argp(argc, argv, "h");
136
137 if (argp.has_arg("h")) {
138 print_usage(argv[0]);
139 exit(0);
140 }
141
142 const char * hostport = (argp.num_items() > 0) ? argp.items()[0] : "localhost:1910";
143 NetLogConsolePrinter printer(hostport);
144
145 SignalManager::register_handler(SIGINT, &printer);
146 printer.run();
147}
Parse command line arguments.
Definition: argparser.h:64
Interface for logging to stderr.
Definition: console.h:37
Message handler for FawkesNetworkClient.
virtual void deregistered(unsigned int id) noexcept=0
This handler has been deregistered.
virtual void inbound_received(FawkesNetworkMessage *m, unsigned int id) noexcept=0
Called for incoming messages.
virtual void connection_established(unsigned int id) noexcept=0
Client has established a connection.
virtual void connection_died(unsigned int id) noexcept=0
Client connection died.
Simple Fawkes network client.
Definition: client.h:52
Representation of a message that is sent over the network.
Definition: message.h:77
virtual void tlog(LogLevel level, struct timeval *t, const char *component, const char *format,...)
Log message of given log level and time.
Definition: multi.cpp:424
Message sent over the network with a log message.
Logger::LogLevel get_loglevel() const
Log level.
struct timeval get_time() const
Get time.
const char * get_message() const
Get message.
const char * get_component() const
Get component.
Interface for signal handling.
Definition: signal.h:36
virtual void handle_signal(int signal)=0
Signal hanlding method.
Fawkes library namespace.