Fawkes API  Fawkes Development Version
qa_liblogger.cpp
1 
2 /***************************************************************************
3  * qa_liblogger.cpp - Fawkes QA for LibLogger
4  *
5  * Created: Mon May 07 17:04:10 2007
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. 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 /// @cond QA
25 
26 #include <core/exceptions/system.h>
27 #include <core/threading/thread.h>
28 #include <utils/logging/console.h>
29 #include <utils/logging/file.h>
30 #include <utils/logging/liblogger.h>
31 #include <utils/system/argparser.h>
32 #include <utils/system/signal.h>
33 
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstring>
37 #include <iostream>
38 #include <list>
39 #include <netdb.h>
40 
41 using namespace std;
42 using namespace fawkes;
43 
44 class LibLoggerQAThread : public Thread
45 {
46 public:
47  LibLoggerQAThread(unsigned int thread_num, unsigned int sleep_time_usec)
48  : Thread("LibLoggerQAThread")
49  {
50  this->sleep_time_usec = sleep_time_usec;
51  this->thread_num = thread_num;
52  i = 0;
53  }
54 
55  ~LibLoggerQAThread()
56  {
57  }
58 
59  virtual void
60  loop()
61  {
62  if ((thread_num % 4) == 0) {
63  LibLogger::log_debug("LibLoggerQA", "%u: %u (debug)", thread_num, ++i);
64  } else if ((thread_num % 3) == 0) {
65  LibLogger::log_info("LibLoggerQA", "%u: %u (info)", thread_num, ++i);
66  } else if ((thread_num % 2) == 0) {
67  LibLogger::log_warn("LibLoggerQA", "%u: %u (warn)", thread_num, ++i);
68  } else {
69  LibLogger::log_error("LibLoggerQA", "%u: %u (error)", thread_num, ++i);
70  }
71  usleep(sleep_time_usec);
72  }
73 
74 private:
75  unsigned int sleep_time_usec;
76  unsigned int thread_num;
77  unsigned int i;
78 };
79 
80 class LibLoggerQAMain : public SignalHandler
81 {
82 public:
83  LibLoggerQAMain(ArgumentParser *argp)
84  {
85  unsigned int sleep_time_usec = 0;
86  unsigned int num_threads = 3;
87  const char * tmp;
88  if ((tmp = argp->arg("s")) != NULL) {
89  sleep_time_usec = atoi(tmp);
90  }
91  if ((tmp = argp->arg("n")) != NULL) {
92  num_threads = atoi(tmp);
93  if (num_threads < 0) {
94  num_threads = 3;
95  }
96  }
97 
98  threads.clear();
99  for (unsigned int i = 0; i < num_threads; ++i) {
100  threads.push_back(new LibLoggerQAThread(i, sleep_time_usec));
101  }
102  }
103 
104  ~LibLoggerQAMain()
105  {
106  for (tit = threads.begin(); tit != threads.end(); ++tit) {
107  delete (*tit);
108  }
109  threads.clear();
110  }
111 
112  virtual void
113  handle_signal(int signum)
114  {
115  printf("Signal received, cancelling threads\n");
116  for (tit = threads.begin(); tit != threads.end(); ++tit) {
117  (*tit)->cancel();
118  }
119  printf("Threads cancelled\n");
120  }
121 
122  void
123  run()
124  {
125  for (tit = threads.begin(); tit != threads.end(); ++tit) {
126  (*tit)->start();
127  }
128  for (tit = threads.begin(); tit != threads.end(); ++tit) {
129  (*tit)->join();
130  }
131  }
132 
133 private:
134  list<Thread *> threads;
135  list<Thread *>::iterator tit;
136  ArgumentParser * argp;
137 };
138 
139 int
140 main(int argc, char **argv)
141 {
142  ArgumentParser *argp = new ArgumentParser(argc, argv, "s:n:");
143 
144  if (argp->has_arg("h")) {
145  cout << "Usage: " << argv[0] << "[-s n] [-n n]" << endl
146  << " -s n Sleep time for thres in usec" << endl
147  << " -h this help message" << endl
148  << " -n n number of threads" << endl;
149  return 0;
150  }
151 
152  LibLoggerQAMain m(argp);
153  SignalManager::register_handler(SIGINT, &m);
154  SignalManager::ignore(SIGPIPE);
155 
156  LibLogger::init();
157  LibLogger::add_logger(new FileLogger("qa_utils_liblogger.log"));
158  LibLogger::add_logger(new ConsoleLogger());
159 
160  m.run();
161 
162  LibLogger::finalize();
163 
164  delete argp;
165  return 0;
166 }
167 
168 /// @endcond
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:177
Interface for logging to a specified file.
Definition: file.h:37
Interface for logging to stderr.
Definition: console.h:36
Fawkes library namespace.
Interface for signal handling.
Definition: signal.h:35
Parse command line arguments.
Definition: argparser.h:63
Thread class encapsulation of pthreads.
Definition: thread.h:45
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:165