summaryrefslogtreecommitdiff
path: root/ACE/netsvcs/clients/Logger/indirect_logging.cpp
blob: 40cd0d61e108599d1bb0e6565373b7e6a24c48d5 (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
// $Id$

// This is a simple test that sends logging records to the Client
// Logging Daemon running on the localhost.  This daemon then forwards
// them to the Server Logging Daemon.  If there is no Server Logging
// Daemon, the logging records will be written to stderr.

#include "ace/OS_NS_time.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/Log_Msg.h"
#include "ace/Log_Record.h"

ACE_RCSID(Logger, indirect_logging, "$Id$")

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  const ACE_TCHAR *prog_name  = argv[0];
  int iterations  = argc < 2 ? 10 : ACE_OS::atoi (argv[1]);
  const ACE_TCHAR *logger_key = argc < 3 ? ACE_DEFAULT_LOGGER_KEY : argv[2];
  int verbose = argc < 4 ? 0 : ACE_Log_Msg::VERBOSE;

  ACE_OS::srand ((u_int) ACE_OS::time (0));

  if (ACE_LOG_MSG->open (prog_name, ACE_Log_Msg::LOGGER, logger_key) == -1)
    {
      ACE_ERROR ((LM_ERROR, "Cannot open logger, using STDERR\n"));

      if (ACE_LOG_MSG->open (prog_name, ACE_Log_Msg::STDERR | verbose) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "Cannot open logger\n"), -1);
    }

  ACE_DEBUG ((LM_STARTUP, "starting up the test\n"));

  for (int i = 0; i < iterations; i++)
    {
      size_t priority = ACE_OS::rand () % int (LM_MAX);
      ACE_POW (priority);
      ACE_Log_Priority log_priority = ACE_Log_Priority (priority);
      ACE_DEBUG ((log_priority,
		  "random message %s (%d)...\n",
                  ACE_Log_Record::priority_name (log_priority),
		  priority));
    }

  ACE_DEBUG ((LM_SHUTDOWN, "closing down the test\n"));

#if defined (ACE_WIN32)
  // !!Important, Winsock is broken in that if you don't close
  // down the connection before exiting main, you'll lose data.
  // More over, your server might get "Access Violation" from
  // within Winsock functions.

  // Here we close down the connection to Logger by redirecting
  // the logging destination back to stderr.
  ACE_LOG_MSG->open (0, ACE_Log_Msg::STDERR, 0);
#endif /* ACE_WIN32 */

  return 0;
}