summaryrefslogtreecommitdiff
path: root/ACE/tests/Log_Msg_Backend_Test.cpp
blob: 8a37c7a118a3e7ad48b9e0655c547b1bd1edb6e9 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

//=============================================================================
/**
 *  @file    Log_Msg_Backend_Test.cpp
 *
 *   This program tests the ACE_Log_Msg class's use of a custom backend,
 *   including its initialization and reset as well as logging information
 *   through the backend.
 *
 *  @author Steve Huston <shuston@riverace.com>
 */
//=============================================================================

#include "test_config.h"

#include "ace/Log_Msg.h"
#include "ace/Log_Msg_Backend.h"
#include "ace/Log_Record.h"

class Backend : public ACE_Log_Msg_Backend
{
public:
  Backend ()
    : reset_ (false), open_ (false), close_ (false), log_count_ (0) {}

  //FUZZ: disable check_for_lack_ACE_OS
  ///FUZZ: enable check_for_lack_ACE_OS
  int open (const ACE_TCHAR *logger_key) override;

  int reset () override;

  //FUZZ: disable check_for_lack_ACE_OS
  ///FUZZ: enable check_for_lack_ACE_OS
  int close () override;

  ssize_t log (ACE_Log_Record &log_record) override;

  // Test probes to see if things worked as specified.
  bool hooks_ok () const;
  unsigned int log_count () const { return this->log_count_; }

private:
  bool reset_;
  bool open_;
  bool close_;
  unsigned int log_count_;
};

int
Backend::open (const ACE_TCHAR *)
{
  this->open_ = true;
  return 0;
}

int
Backend::reset ()
{
  this->reset_ = true;
  return 0;
}

int
Backend::close ()
{
  return 0;
}

ssize_t
Backend::log (ACE_Log_Record &)
{
  ++this->log_count_;
  return 1;
}

// Test probes to see if things worked as specified.
bool
Backend::hooks_ok () const
{
  // Close should not have been called yet.
  const ACE_TCHAR *yes = ACE_TEXT ("yes");
  const ACE_TCHAR *no = ACE_TEXT ("no");
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Open: %s, Reset: %s, Close: %s\n"),
              this->open_ ? yes : no,
              this->reset_ ? yes : no,
              this->close_ ? yes : no));
  return this->open_ && this->reset_ && !this->close_;
}

// Main function.

int
run_main (int, ACE_TCHAR *[])
{
  // Set up the backend prior to ACE_START_TEST so the initialization can
  // call the back end's reset().
  Backend b;
  ACE_Log_Msg_Backend *old_b = ACE_Log_Msg::msg_backend (&b);

  ACE_START_TEST (ACE_TEXT ("Log_Msg_Backend_Test"));

  int status = 0;

  if (old_b != 0)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Old backend (%@) not 0 at start\n"),
                  old_b));
      ++status;
    }

  // Reopen to get the backend established.
  u_long flags = ACE_LOG_MSG->flags ();
  flags |= ACE_Log_Msg::CUSTOM;
  if (-1 == ACE_LOG_MSG->open (ACE_TEXT ("Log_Msg_Backend_Test"), flags))
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Reopening log")));
      ++status;
    }

  // Make sure messages are getting routed correctly.
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Message 1\n")));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Message 2\n")));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Message 3\n")));
  unsigned int count = b.log_count ();
  if (count != 3)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Backend counted %u; expected 3\n"),
                  count));
      ++status;
    }

  // Should have seen the hooks other than close() so far. Note that
  // hooks_ok() logs a message so check counts before checking hooks.
  if (!b.hooks_ok ())
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("Hooks not called correctly\n")));
      ++status;
    }

  ACE_END_TEST;

  // Reset the backend to avoid references to our soon-to-be-destroyed one.
  ACE_Log_Msg::msg_backend (old_b);

  return status;
}