qa_liblogger.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <core/threading/thread.h>
00027 #include <utils/system/signal.h>
00028 #include <utils/system/argparser.h>
00029 #include <utils/logging/liblogger.h>
00030 #include <utils/logging/console.h>
00031 #include <utils/logging/file.h>
00032 #include <core/exceptions/system.h>
00033
00034 #include <netdb.h>
00035 #include <cstdio>
00036 #include <cstring>
00037 #include <cstdlib>
00038 #include <iostream>
00039
00040 #include <list>
00041
00042 using namespace std;
00043 using namespace fawkes;
00044
00045 class LibLoggerQAThread : public Thread
00046 {
00047 public:
00048 LibLoggerQAThread(unsigned int thread_num, unsigned int sleep_time_usec)
00049 : Thread("LibLoggerQAThread")
00050 {
00051 this->sleep_time_usec = sleep_time_usec;
00052 this->thread_num = thread_num;
00053 i = 0;
00054 }
00055
00056 ~LibLoggerQAThread()
00057 {
00058 }
00059
00060 virtual void loop()
00061 {
00062 if ( (thread_num % 4) == 0 ) {
00063 LibLogger::log_debug("LibLoggerQA", "%u: %u (debug)", thread_num, ++i);
00064 } else if ( (thread_num % 3) == 0 ) {
00065 LibLogger::log_info("LibLoggerQA", "%u: %u (info)", thread_num, ++i);
00066 } else if ( (thread_num % 2) == 0 ) {
00067 LibLogger::log_warn("LibLoggerQA", "%u: %u (warn)", thread_num, ++i);
00068 } else {
00069 LibLogger::log_error("LibLoggerQA", "%u: %u (error)", thread_num, ++i);
00070 }
00071 usleep(sleep_time_usec);
00072 }
00073
00074 private:
00075 unsigned int sleep_time_usec;
00076 unsigned int thread_num;
00077 unsigned int i;
00078 };
00079
00080
00081 class LibLoggerQAMain : public SignalHandler
00082 {
00083 public:
00084 LibLoggerQAMain(ArgumentParser *argp)
00085 {
00086 unsigned int sleep_time_usec = 0;
00087 unsigned int num_threads = 3;
00088 const char *tmp;
00089 if ( (tmp = argp->arg("s")) != NULL ) {
00090 sleep_time_usec = atoi(tmp);
00091 }
00092 if ( (tmp = argp->arg("n")) != NULL ) {
00093 num_threads = atoi(tmp);
00094 if ( num_threads < 0 ) {
00095 num_threads = 3;
00096 }
00097 }
00098
00099 threads.clear();
00100 for ( unsigned int i = 0; i < num_threads; ++i ) {
00101 threads.push_back( new LibLoggerQAThread(i, sleep_time_usec) );
00102 }
00103 }
00104
00105 ~LibLoggerQAMain()
00106 {
00107 for ( tit = threads.begin(); tit != threads.end(); ++tit ) {
00108 delete (*tit);
00109 }
00110 threads.clear();
00111 }
00112
00113
00114 virtual void handle_signal(int signum)
00115 {
00116 printf("Signal received, cancelling threads\n");
00117 for ( tit = threads.begin(); tit != threads.end(); ++tit ) {
00118 (*tit)->cancel();
00119 }
00120 printf("Threads cancelled\n");
00121 }
00122
00123 void run()
00124 {
00125 for ( tit = threads.begin(); tit != threads.end(); ++tit ) {
00126 (*tit)->start();
00127 }
00128 for ( tit = threads.begin(); tit != threads.end(); ++tit ) {
00129 (*tit)->join();
00130 }
00131 }
00132
00133 private:
00134 list<Thread *> threads;
00135 list<Thread *>::iterator tit;
00136 ArgumentParser *argp;
00137 };
00138
00139 int
00140 main(int argc, char **argv)
00141 {
00142 ArgumentParser *argp = new ArgumentParser(argc, argv, "s:n:");
00143
00144 if ( argp->has_arg("h") ) {
00145 cout << "Usage: " << argv[0] << "[-s n] [-n n]" << endl
00146 << " -s n Sleep time for thres in usec" << endl
00147 << " -h this help message" << endl
00148 << " -n n number of threads" << endl;
00149 return 0;
00150 }
00151
00152 LibLoggerQAMain m(argp);
00153 SignalManager::register_handler(SIGINT, &m);
00154 SignalManager::ignore(SIGPIPE);
00155
00156 LibLogger::init();
00157 LibLogger::add_logger(new FileLogger("qa_utils_liblogger.log"));
00158 LibLogger::add_logger(new ConsoleLogger());
00159
00160 m.run();
00161
00162 LibLogger::finalize();
00163
00164 delete argp;
00165 return 0;
00166 }
00167
00168