Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / HTIOP / AMI / Test_Output.cpp
blobc66fe3f85beb05194df9c86e82f2ae82f3e31e2d
1 // -*- C++ -*-
3 // ============================================================================
4 /**
5 * @file Test_Output.cpp
7 * This file factors out common macros and other utilities used by the
8 * ACE automated regression tests.
10 * @author Prashant Jain <pjain@cs.wustl.edu>
11 * @author Tim Harrison <harrison@cs.wustl.edu>
12 * @author David Levine <levine@cs.wustl.edu>
13 * @author Don Hinton <dhinton@dresystems.com>
15 // ============================================================================
17 #include "../test_config.h"
18 #include "ace/OS_NS_stdio.h"
19 #include "ace/OS_NS_string.h"
20 #include "ace/OS_NS_sys_stat.h"
21 #include "ace/Guard_T.h"
22 #include "ace/Object_Manager.h"
24 // FUZZ: disable check_for_streams_include
25 #include "ace/streams.h"
27 #include "ace/Framework_Component.h"
28 #include "ace/Log_Msg.h"
29 #include "ace/ACE.h"
31 #if defined (VXWORKS)
32 # include "ace/OS_NS_unistd.h"
33 # include "ace/OS_NS_fcntl.h"
34 #endif /* VXWORKS */
36 ACE_Test_Output *ACE_Test_Output::instance_ = 0;
38 ACE_Test_Output::ACE_Test_Output (void)
39 : output_file_ (0)
41 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
42 this->output_file_ = new OFSTREAM;
43 #endif /* ACE_LACKS_IOSTREAM_TOTALLY */
46 ACE_Test_Output::~ACE_Test_Output (void)
48 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
49 ACE_LOG_MSG->msg_ostream (&cerr);
50 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
52 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::OSTREAM);
53 ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
55 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY) && !defined (ACE_HAS_PHARLAP)
56 delete this->output_file_;
57 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
60 OFSTREAM *
61 ACE_Test_Output::output_file (void)
63 return this->output_file_;
66 int
67 ACE_Test_Output::set_output (const ACE_TCHAR *filename, int append)
69 #if defined (ACE_HAS_PHARLAP)
70 // For PharLap, just send it all to the host console for now - redirect
71 // to a file there for saving/analysis.
72 EtsSelectConsole(ETS_CO_HOST);
73 ACE_LOG_MSG->msg_ostream (&cout);
75 #else
76 ACE_TCHAR temp[MAXPATHLEN];
77 // Ignore the error value since the directory may already exist.
78 const ACE_TCHAR *test_dir;
80 #if !defined (ACE_HAS_WINCE)
81 # if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
82 test_dir = ACE_OS::getenv (ACE_TEXT ("ACE_TEST_DIR"));
83 # else
84 ACE_TCHAR tempenv[MAXPATHLEN];
85 char *test_dir_n = ACE_OS::getenv ("ACE_TEST_DIR");
86 if (test_dir_n == 0)
87 test_dir = 0;
88 else
90 ACE_OS::strcpy (tempenv, ACE_TEXT_CHAR_TO_TCHAR (test_dir_n));
91 test_dir = tempenv;
93 # endif /* ACE_WIN32 || !ACE_USES_WCHAR */
95 if (test_dir == 0)
96 #endif /* ACE_HAS_WINCE */
97 test_dir = ACE_TEXT ("");
99 // This could be done with ACE_OS::sprintf() but it requires different
100 // format strings for wide-char POSIX vs. narrow-char POSIX and Windows.
101 // Easier to keep straight like this.
102 ACE_OS::strcpy (temp, test_dir);
103 ACE_OS::strcat (temp, ACE_LOG_DIRECTORY);
104 ACE_OS::strcat
105 (temp, ACE::basename (filename, ACE_DIRECTORY_SEPARATOR_CHAR));
106 ACE_OS::strcat (temp, ACE_LOG_FILE_EXT_NAME);
108 #if defined (VXWORKS)
109 // This is the only way I could figure out to avoid a console
110 // warning about opening an existing file (w/o O_CREAT), or
111 // attempting to unlink a non-existant one.
112 ACE_HANDLE fd = ACE_OS::open (temp,
113 O_WRONLY|O_CREAT,
114 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
115 if (fd != ERROR)
117 ACE_OS::close (fd);
118 ACE_OS::unlink (temp);
120 # else /* ! VXWORKS */
121 // This doesn't seem to work on VxWorks if the directory doesn't
122 // exist: it creates a plain file instead of a directory. If the
123 // directory does exist, it causes a wierd console error message
124 // about "cat: input error on standard input: Is a directory". So,
125 // VxWorks users must create the directory manually.
126 # if defined (ACE_HAS_WINCE)
127 ACE_OS::mkdir (ACE_LOG_DIRECTORY_FOR_MKDIR);
128 # else
129 ACE_OS::mkdir (ACE_LOG_DIRECTORY);
130 # endif // ACE_HAS_WINCE
131 # endif /* ! VXWORKS */
133 # if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
134 this->output_file_->open (ACE_TEXT_ALWAYS_CHAR (temp),
135 ios::out | (append ? ios::app : ios::trunc));
136 if (this->output_file_->bad ())
137 return -1;
138 #else /* when ACE_LACKS_IOSTREAM_TOTALLY */
139 ACE_TCHAR *fmode = 0;
140 if (append)
141 fmode = ACE_TEXT ("a");
142 else
143 fmode = ACE_TEXT ("w");
144 this->output_file_ = ACE_OS::fopen (temp, fmode);
145 # endif /* ACE_LACKS_IOSTREAM_TOTALLY */
147 ACE_LOG_MSG->msg_ostream (this->output_file ());
148 #endif /* ACE_HAS_PHARLAP */
150 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER );
151 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
153 return 0;
156 void
157 ACE_Test_Output::close (void)
159 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
160 this->output_file_->flush ();
161 this->output_file_->close ();
162 #else
163 ACE_OS::fflush (this->output_file_);
164 ACE_OS::fclose (this->output_file_);
165 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
168 ACE_Test_Output*
169 ACE_Test_Output::instance ()
171 if (ACE_Test_Output::instance_ == 0)
173 // Perform Double-Checked Locking Optimization.
174 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
175 *ACE_Static_Object_Lock::instance (), 0));
177 if (ACE_Test_Output::instance_ == 0)
179 ACE_NEW_RETURN (ACE_Test_Output::instance_,
180 ACE_Test_Output,
182 ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Test_Output, ACE_Test_Output::instance_)
185 return ACE_Test_Output::instance_;
188 const ACE_TCHAR *
189 ACE_Test_Output::dll_name (void)
191 return ACE_TEXT ("Test_Output");
194 const ACE_TCHAR *
195 ACE_Test_Output::name (void)
197 return ACE_TEXT ("ACE_Test_Output");
200 void
201 ACE_Test_Output::close_singleton (void)
203 delete ACE_Test_Output::instance_;
204 ACE_Test_Output::instance_ = 0;
207 void
208 randomize (int array[], size_t size)
210 size_t i;
212 for (i = 0; i < size; i++)
213 array [i] = static_cast<int> (i);
215 // See with a fixed number so that we can produce "repeatable"
216 // random numbers.
217 ACE_OS::srand (0);
219 // Generate an array of random numbers from 0 .. size - 1.
221 for (i = 0; i < size; i++)
223 size_t index = ACE_OS::rand() % size--;
224 int temp = array [index];
225 array [index] = array [size];
226 array [size] = temp;