Fawkes API  Fawkes Development Version
qa_logger.cpp
1 
2 /***************************************************************************
3  * qa_logger.cpp - QA for Logger
4  *
5  * Generated: Wed Jan 17 14:19:45 2007
6  * Copyright 2005-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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 // Do not include in api reference
25 ///@cond QA
26 
27 #include <core/threading/thread.h>
28 #include <utils/system/signal.h>
29 #include <utils/logging/console.h>
30 #include <utils/logging/multi.h>
31 #include <utils/logging/logger.h>
32 
33 #include <cstdio>
34 
35 using namespace fawkes;
36 
37 class LoggerQAThread : public Thread
38 {
39  public:
40  LoggerQAThread(const char *name, Logger *logger)
41  : Thread(name)
42  {
43  this->logger = logger;
44  i = 0;
45  }
46 
47  virtual void loop()
48  {
49  ++i;
50  printf("%s: Testing: %i\n", name(), i);
51  logger->log_info(name(), "Testing: %i", i);
52  }
53 
54  private:
55  unsigned int i;
56  Logger *logger;
57 };
58 
59 
60 class LoggerQAMain : public SignalHandler
61 {
62  public:
63  LoggerQAMain()
64  {
65  cl = ml = NULL;
66  t1 = t2 = t3 = t4 = t5 = t6 = NULL;
67  }
68 
69  ~LoggerQAMain()
70  {
71  delete t1;
72  delete t2;
73  delete t3;
74  delete t4;
75  delete t5;
76  delete t6;
77  // also deletes cl!
78  delete ml;
79  }
80 
81  virtual void handle_signal(int signum)
82  {
83  printf("Signal received, cancelling threads\n");
84  t1->cancel();
85  t2->cancel();
86  t3->cancel();
87  t4->cancel();
88  t5->cancel();
89  t6->cancel();
90  printf("Threads cancelled\n");
91  }
92 
93  void run()
94  {
95  cl = new ConsoleLogger();
96  ml = new MultiLogger(cl);
97 
98  t1 = new LoggerQAThread("L-1-", ml);
99  t2 = new LoggerQAThread("L-2-", ml);
100  t3 = new LoggerQAThread("L-3-", ml);
101  t4 = new LoggerQAThread("L-4-", ml);
102  t5 = new LoggerQAThread("L-5-", ml);
103  t6 = new LoggerQAThread("L-6-", ml);
104 
105  t1->start();
106  t2->start();
107  t3->start();
108  t4->start();
109  t5->start();
110  t6->start();
111  t1->join();
112  t2->join();
113  t3->join();
114  t4->join();
115  t5->join();
116  t6->join();
117  }
118 
119  private:
120  Logger *cl;
121  Logger *ml;
122  LoggerQAThread *t1;
123  LoggerQAThread *t2;
124  LoggerQAThread *t3;
125  LoggerQAThread *t4;
126  LoggerQAThread *t5;
127  LoggerQAThread *t6;
128 };
129 
130 int
131 main(int argc, char **argv)
132 {
133 
134  /*
135  ConsoleLogger cl;
136 
137  Exception e("Test Exception");
138 
139  cl.log_debug("QA", "DEBUG test output %i", 1);
140  cl.log_info("QA", "DEBUG test output %i", 2);
141  cl.log_warn("QA", "DEBUG test output %i", 3);
142  cl.log_error("QA", "DEBUG test output %i", 4);
143 
144  cl.log_debug("QA", e);
145  cl.log_info("QA", e);
146  cl.log_warn("QA", e);
147  cl.log_error("QA", e);
148 
149  ConsoleLogger *clp = new ConsoleLogger();
150 
151  clp->log_debug("QA", "DEBUG test output %i", 1);
152  clp->log_info("QA", "DEBUG test output %i", 2);
153  clp->log_warn("QA", "DEBUG test output %i", 3);
154  clp->log_error("QA", "DEBUG test output %i", 4);
155 
156  clp->log_debug("QA", e);
157  clp->log_info("QA", e);
158  clp->log_warn("QA", e);
159  clp->log_error("QA", e);
160 
161  LoggerTestThread *tt = new LoggerTestThread(clp);
162  tt->start();
163  tt->join();
164  delete tt;
165 
166  delete clp;
167  */
168 
169  LoggerQAMain main;
170  SignalManager::register_handler(SIGINT, &main);
171  main.run();
173 
174  return 0;
175 }
176 
177 /// @endcond
static void finalize()
Finalize (and free) the SignalManager instance, this does NOT implicitly delete the signal handlers...
Definition: signal.cpp:98
Interface for logging to stderr.
Definition: console.h:36
Fawkes library namespace.
Interface for signal handling.
Definition: signal.h:35
Thread class encapsulation of pthreads.
Definition: thread.h:42
Log through multiple loggers.
Definition: multi.h:35
static SignalHandler * register_handler(int signum, SignalHandler *handler)
Register a SignalHandler for a signal.
Definition: signal.cpp:116
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: multi.cpp:205
Interface for logging.
Definition: logger.h:34