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/threading/thread.h>
27 #include <utils/system/signal.h>
28 #include <utils/system/argparser.h>
29 #include <utils/logging/liblogger.h>
30 #include <utils/logging/console.h>
31 #include <utils/logging/file.h>
32 #include <core/exceptions/system.h>
33 
34 #include <netdb.h>
35 #include <cstdio>
36 #include <cstring>
37 #include <cstdlib>
38 #include <iostream>
39 
40 #include <list>
41 
42 using namespace std;
43 using namespace fawkes;
44 
45 class LibLoggerQAThread : public Thread
46 {
47 public:
48  LibLoggerQAThread(unsigned int thread_num, unsigned int sleep_time_usec)
49  : Thread("LibLoggerQAThread")
50  {
51  this->sleep_time_usec = sleep_time_usec;
52  this->thread_num = thread_num;
53  i = 0;
54  }
55 
56  ~LibLoggerQAThread()
57  {
58  }
59 
60  virtual void 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 
81 class LibLoggerQAMain : public SignalHandler
82 {
83  public:
84  LibLoggerQAMain(ArgumentParser *argp)
85  {
86  unsigned int sleep_time_usec = 0;
87  unsigned int num_threads = 3;
88  const char *tmp;
89  if ( (tmp = argp->arg("s")) != NULL ) {
90  sleep_time_usec = atoi(tmp);
91  }
92  if ( (tmp = argp->arg("n")) != NULL ) {
93  num_threads = atoi(tmp);
94  if ( num_threads < 0 ) {
95  num_threads = 3;
96  }
97  }
98 
99  threads.clear();
100  for ( unsigned int i = 0; i < num_threads; ++i ) {
101  threads.push_back( new LibLoggerQAThread(i, sleep_time_usec) );
102  }
103  }
104 
105  ~LibLoggerQAMain()
106  {
107  for ( tit = threads.begin(); tit != threads.end(); ++tit ) {
108  delete (*tit);
109  }
110  threads.clear();
111  }
112 
113 
114  virtual void handle_signal(int signum)
115  {
116  printf("Signal received, cancelling threads\n");
117  for ( tit = threads.begin(); tit != threads.end(); ++tit ) {
118  (*tit)->cancel();
119  }
120  printf("Threads cancelled\n");
121  }
122 
123  void 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:182
Interface for logging to a specified file.
Definition: file.h:36
Interface for logging to stderr.
Definition: console.h:36
Fawkes library namespace.
STL namespace.
Interface for signal handling.
Definition: signal.h:35
Parse command line arguments.
Definition: argparser.h:66
Thread class encapsulation of pthreads.
Definition: thread.h:42
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169