summaryrefslogtreecommitdiff
path: root/ACE/examples/Misc/test_trace.cpp
blob: 8f684d237b7840f3a97af3ce3454076ba58bff89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// $Id$

// ============================================================================
//
// = LIBRARY
//    examples/Misc
//
// = FILENAME
//    test_trace.cpp
//
// = DESCRIPTION
//    This example illustrates how to use the ACE tracing feature and
//    the ACE_TRACE macro.  It also shows the use of the ACE_Task_Base
//    class running as an "active object".
//
//    When adding ACE tracing to an application one option is to add
//
//    #define ACE_NTRACE 0
//
//    in the line above #include "ace/Log_Msg.h".  That's the approach shown below.
//
//    Another option is to add this line in $ACE_ROOT/ace/config.h.
//    Note, however, that if you add the line in config.h, you need to
//    add it *after* you've built ACE, i.e., don't build ACE with it
//    set to 0 as then you will get all the internal ACE_TRACE calls
//    showing up as well!  In a nutshell, in this second option you
//    do this:
//
//    1. Build ACE without tracing (i.e., don't #define ACE_NTRACE 0 in config.h)
//    2. Add #define ACE_NTRACE 0 in config.h
//    3. Build your app with tracing.
//
// = AUTHOR
//    Douglas C. Schmidt <schmidt@cs.wustl.edu> and
//    Irfan Pyarali <irfan@cs.wustl.edu>
//
// ============================================================================

// Enable tracing
#define ACE_NTRACE 0

#include "ace/OS_main.h"
#include "ace/Signal.h"
#include "ace/Task.h"



class My_Task : public ACE_Task_Base
{
public:
  My_Task (size_t depth)
    : depth_ (depth)
  {
  }

  int recursive (size_t depth);

  virtual int svc (void)
  {
    return this->recursive (this->depth_);
  }

private:
  size_t depth_;
  // Depth of the recursion.
};

int
My_Task::recursive (size_t depth)
{
  ACE_TRACE ("My_Task::recursive");

  if (depth > 0)
    return recursive (depth - 1);
  else
    return 0;
  // Destructor of <ACE_Trace> automatically called.
}

extern "C"
void
exithook (void)
{
  ACE_TRACE ("void exithook (void)");

  ACE_DEBUG ((LM_DEBUG,
              "we're outta here!\n"));
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  const size_t MAX_DEPTH = argc == 1 ? 10 : ACE_OS::atoi (argv[1]);

  ACE_OS::atexit (exithook);

  if (argc > 2)
    ACE_Trace::set_nesting_indent (ACE_OS::atoi (argv[2]));

  ACE_TRACE ("int ACE_TMAIN (int argc, ACE_TCHAR *argv[])");

  ACE_Sig_Action sig1 ((ACE_SignalHandler) ACE_Trace::start_tracing,
                       SIGUSR1);
  ACE_UNUSED_ARG (sig1);
  ACE_Sig_Action sig2 ((ACE_SignalHandler) ACE_Trace::stop_tracing,
                       SIGUSR2);
  ACE_UNUSED_ARG (sig2);

  My_Task task (MAX_DEPTH);

#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  int n_threads = argc > 3 ? ACE_OS::atoi (argv[3]) : 4;

  if (task.activate (THR_BOUND | THR_DETACHED, n_threads) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "activate"),
                      -1);

  // Wait for all the threads to exit.
  ACE_Thread_Manager::instance ()->wait ();
#else
  const int MAX_ITERATIONS = argc > 3 ? ACE_OS::atoi (argv[3]) : 10;

  for (int i = 0; i < MAX_ITERATIONS; i++)
    task.svc ();
#endif /* ACE_MT_SAFE */

  // Destructor automatically called.
  return 0;
}