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;
}