Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Misc / test_trace.cpp
blob32f062d5d8587be99d3c0a33af1857a220a76802
2 //=============================================================================
3 /**
4 * @file test_trace.cpp
6 * This example illustrates how to use the ACE tracing feature and
7 * the ACE_TRACE macro. It also shows the use of the ACE_Task_Base
8 * class running as an "active object".
10 * When adding ACE tracing to an application one option is to add
12 * #define ACE_NTRACE 0
14 * in the line above #include "ace/Log_Msg.h". That's the approach shown below.
16 * Another option is to add this line in $ACE_ROOT/ace/config.h.
17 * Note, however, that if you add the line in config.h, you need to
18 * add it *after* you've built ACE, i.e., don't build ACE with it
19 * set to 0 as then you will get all the internal ACE_TRACE calls
20 * showing up as well! In a nutshell, in this second option you
21 * do this:
23 * 1. Build ACE without tracing (i.e., don't #define ACE_NTRACE 0 in config.h)
24 * 2. Add #define ACE_NTRACE 0 in config.h
25 * 3. Build your app with tracing.
27 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu> and Irfan Pyarali <irfan@cs.wustl.edu>
29 //=============================================================================
32 // Enable tracing
33 #define ACE_NTRACE 0
35 #include "ace/OS_main.h"
36 #include "ace/Signal.h"
37 #include "ace/Task.h"
38 #include "ace/Trace.h"
40 class My_Task : public ACE_Task_Base
42 public:
43 My_Task (size_t depth)
44 : depth_ (depth)
48 int recursive (size_t depth);
50 virtual int svc ()
52 return this->recursive (this->depth_);
55 private:
56 /// Depth of the recursion.
57 size_t depth_;
60 int
61 My_Task::recursive (size_t depth)
63 ACE_TRACE ("My_Task::recursive");
65 if (depth > 0)
66 return recursive (depth - 1);
67 else
68 return 0;
69 // Destructor of <ACE_Trace> automatically called.
72 extern "C"
73 void
74 exithook ()
76 ACE_TRACE ("void exithook ()");
78 ACE_DEBUG ((LM_DEBUG,
79 "we're outta here!\n"));
82 int
83 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
85 const size_t MAX_DEPTH = argc == 1 ? 10 : ACE_OS::atoi (argv[1]);
87 ACE_OS::atexit (exithook);
89 if (argc > 2)
90 ACE_Trace::set_nesting_indent (ACE_OS::atoi (argv[2]));
92 ACE_TRACE ("int ACE_TMAIN (int argc, ACE_TCHAR *argv[])");
94 ACE_Sig_Action sig1 ((ACE_SignalHandler) ACE_Trace::start_tracing,
95 SIGUSR1);
96 ACE_UNUSED_ARG (sig1);
97 ACE_Sig_Action sig2 ((ACE_SignalHandler) ACE_Trace::stop_tracing,
98 SIGUSR2);
99 ACE_UNUSED_ARG (sig2);
101 My_Task task (MAX_DEPTH);
103 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
104 int n_threads = argc > 3 ? ACE_OS::atoi (argv[3]) : 4;
106 if (task.activate (THR_BOUND | THR_DETACHED, n_threads) == -1)
107 ACE_ERROR_RETURN ((LM_ERROR,
108 "%p\n",
109 "activate"),
110 -1);
112 // Wait for all the threads to exit.
113 ACE_Thread_Manager::instance ()->wait ();
114 #else
115 const int MAX_ITERATIONS = argc > 3 ? ACE_OS::atoi (argv[3]) : 10;
117 for (int i = 0; i < MAX_ITERATIONS; i++)
118 task.svc ();
119 #endif /* ACE_MT_SAFE */
121 // Destructor automatically called.
122 return 0;