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 
35 using namespace fawkes;
36 
37 /// @cond INTERNALS
38 
39 class NetLogConsolePrinter : public FawkesNetworkClientHandler, public SignalHandler
40 {
41 public:
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) throw()
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) throw()
94  {
95  quit = true;
96  }
97 
98  virtual void
99  connection_died(unsigned int id) throw()
100  {
101  printf("Connection to host died. Aborting.\n");
102  quit = true;
103  }
104 
105  virtual void
106  connection_established(unsigned int id) throw()
107  {
108  }
109 
110  void
111  run()
112  {
113  while (!quit) {
114  client->wait(FAWKES_CID_NETWORKLOGGER);
115  }
116  client->disconnect();
117  }
118 
119 private:
120  FawkesNetworkClient *client;
121  ConsoleLogger * logger;
122  bool quit;
123 };
124 /// @endcond
125 
126 void
127 print_usage(const char *program_name)
128 {
129  printf("Usage: %s [hostname[:port]]\n", program_name);
130 }
131 
132 int
133 main(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 }
fawkes::NetworkLoggerMessageContent
Definition: network_logger.h:126
fawkes::ConsoleLogger
Definition: console.h:40
fawkes::SignalHandler
Definition: signal.h:43
fawkes::NetworkLoggerMessageContent::get_time
struct timeval get_time() const
Get time.
Definition: network_logger.cpp:527
fawkes::NetworkLogger::MSGTYPE_SUBSCRIBE
Subscribe for logging messages.
Definition: network_logger.h:88
fawkes::SignalManager::register_handler
static SignalHandler * register_handler(int signum, SignalHandler *handler)
Register a SignalHandler for a signal.
Definition: signal.cpp:121
fawkes::MultiLogger::tlog
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:428
fawkes::FawkesNetworkClientHandler
Definition: client_handler.h:35
fawkes::NetworkLoggerMessageContent::get_loglevel
Logger::LogLevel get_loglevel() const
Log level.
Definition: network_logger.cpp:557
fawkes
fawkes::ArgumentParser
Definition: argparser.h:67
fawkes::NetworkLogger::MSGTYPE_LOGMESSAGE
Log message.
Definition: network_logger.h:90
fawkes::FawkesNetworkMessage
Definition: message.h:80
fawkes::FawkesNetworkClient
Definition: client.h:55
fawkes::NetworkLoggerMessageContent::get_component
const char * get_component() const
Get component.
Definition: network_logger.cpp:539
fawkes::NetworkLoggerMessageContent::get_message
const char * get_message() const
Get message.
Definition: network_logger.cpp:548