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 <netcomm/fawkes/client.h>
24 #include <netcomm/fawkes/client_handler.h>
25 #include <netcomm/fawkes/component_ids.h>
26 #include <logging/network.h>
27 #include <logging/console.h>
28 #include <utils/system/signal.h>
29 #include <utils/system/argparser.h>
30 
31 #include <cstring>
32 #include <cstdio>
33 #include <cstdlib>
34 
35 using namespace fawkes;
36 
37 /// @cond INTERNALS
38 
39 class NetLogConsolePrinter
41  public SignalHandler
42 {
43  public:
44  NetLogConsolePrinter(const char *hostport)
45  {
46  logger = new ConsoleLogger();
47  quit = false;
48 
49  char *hp = strdup(hostport);
50  const char *hostname = strtok(hp, ":");
51  const char *portstr = strtok(NULL, "");
52  int port = 1910;
53  if ( portstr ) {
54  port = atoi(portstr);
55  if ( (port < 0) || ( port > 0xFFFF ) ) {
56  printf("Invalid port given, must be in range [1:65535]. Using default 1910 instead\n");
57  port = 1910;
58  }
59  }
60 
61  client = new FawkesNetworkClient(hostname, port);
62  client->connect();
63  client->register_handler(this, FAWKES_CID_NETWORKLOGGER);
64 
65  client->enqueue(new FawkesNetworkMessage(FAWKES_CID_NETWORKLOGGER,
67  }
68 
69  ~NetLogConsolePrinter()
70  {
71  delete logger;
72  delete client;
73  }
74 
75  void handle_signal(int signal)
76  {
77  quit = true;
78  client->wake(FAWKES_CID_NETWORKLOGGER);
79  }
80 
81  virtual void inbound_received(FawkesNetworkMessage *m,
82  unsigned int id) throw()
83  {
84  if ( (m->cid() == FAWKES_CID_NETWORKLOGGER) &&
85  (m->msgid() == NetworkLogger::MSGTYPE_LOGMESSAGE) ) {
87  struct timeval t = content->get_time();
88  /* Yes, it is risky to just use get_message() as format, but for now we are happy
89  * and do not expect bad guys. To be fixed. */
90  logger->tlog(content->get_loglevel(), &t, content->get_component(), content->get_message());
91  }
92  }
93 
94  virtual void deregistered(unsigned int id) throw()
95  {
96  quit = true;
97  }
98 
99 
100  virtual void connection_died(unsigned int id) throw()
101  {
102  printf("Connection to host died. Aborting.\n");
103  quit = true;
104  }
105 
106 
107  virtual void connection_established(unsigned int id) throw()
108  {
109  }
110 
111 
112  void run()
113  {
114  while ( ! quit ) {
115  client->wait(FAWKES_CID_NETWORKLOGGER);
116  }
117  client->disconnect();
118  }
119 
120 
121  private:
122  FawkesNetworkClient *client;
123  ConsoleLogger *logger;
124  bool quit;
125 };
126 /// @endcond
127 
128 
129 void
130 print_usage(const char *program_name)
131 {
132  printf("Usage: %s [hostname[:port]]\n", program_name);
133 }
134 
135 int
136 main(int argc, char **argv)
137 {
138  ArgumentParser argp(argc, argv, "h");
139 
140  if ( argp.has_arg("h") ) {
141  print_usage(argv[0]);
142  exit(0);
143  }
144 
145  const char *hostport = (argp.num_items() > 0) ? argp.items()[0] : "localhost:1910";
146  NetLogConsolePrinter printer(hostport);
147 
148  SignalManager::register_handler(SIGINT, &printer);
149  printer.run();
150 
151 }
152 
Message handler for FawkesNetworkClient.
const char * get_component() const
Get component.
Definition: network.cpp:562
const char * get_message() const
Get message.
Definition: network.cpp:572
Message sent over the network with a log message.
Definition: network.h:120
Simple Fawkes network client.
Definition: client.h:52
Interface for logging to stderr.
Definition: console.h:36
Fawkes library namespace.
Representation of a message that is sent over the network.
Definition: message.h:75
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:446
Interface for signal handling.
Definition: signal.h:35
Parse command line arguments.
Definition: argparser.h:66
Logger::LogLevel get_loglevel() const
Log level.
Definition: network.cpp:582
static SignalHandler * register_handler(int signum, SignalHandler *handler)
Register a SignalHandler for a signal.
Definition: signal.cpp:116
struct timeval get_time() const
Get time.
Definition: network.cpp:549
Subscribe for logging messages.
Definition: network.h:88