3 // ============================================================================
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"
32 # include "ace/OS_NS_unistd.h"
33 # include "ace/OS_NS_fcntl.h"
36 ACE_Test_Output
*ACE_Test_Output::instance_
= 0;
38 ACE_Test_Output::ACE_Test_Output ()
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 ()
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)
56 delete this->output_file_
;
57 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
61 ACE_Test_Output::output_file ()
63 return this->output_file_
;
67 ACE_Test_Output::set_output (const ACE_TCHAR
*filename
, int append
)
69 ACE_TCHAR temp
[MAXPATHLEN
];
70 // Ignore the error value since the directory may already exist.
71 const ACE_TCHAR
*test_dir
{};
73 # if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
74 test_dir
= ACE_OS::getenv (ACE_TEXT ("ACE_TEST_DIR"));
76 ACE_TCHAR tempenv
[MAXPATHLEN
];
77 char *test_dir_n
= ACE_OS::getenv ("ACE_TEST_DIR");
82 ACE_OS::strcpy (tempenv
, ACE_TEXT_CHAR_TO_TCHAR (test_dir_n
));
85 # endif /* ACE_WIN32 || !ACE_USES_WCHAR */
88 test_dir
= ACE_TEXT ("");
90 // This could be done with ACE_OS::sprintf() but it requires different
91 // format strings for wide-char POSIX vs. narrow-char POSIX and Windows.
92 // Easier to keep straight like this.
93 ACE_OS::strcpy (temp
, test_dir
);
94 ACE_OS::strcat (temp
, ACE_LOG_DIRECTORY
);
96 (temp
, ACE::basename (filename
, ACE_DIRECTORY_SEPARATOR_CHAR
));
97 ACE_OS::strcat (temp
, ACE_LOG_FILE_EXT_NAME
);
100 // This is the only way I could figure out to avoid a console
101 // warning about opening an existing file (w/o O_CREAT), or
102 // attempting to unlink a non-existent one.
103 ACE_HANDLE fd
= ACE_OS::open (temp
,
105 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
109 ACE_OS::unlink (temp
);
111 # else /* ! VXWORKS */
112 // This doesn't seem to work on VxWorks if the directory doesn't
113 // exist: it creates a plain file instead of a directory. If the
114 // directory does exist, it causes a wierd console error message
115 // about "cat: input error on standard input: Is a directory". So,
116 // VxWorks users must create the directory manually.
117 ACE_OS::mkdir (ACE_LOG_DIRECTORY
);
118 # endif /* ! VXWORKS */
120 # if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
121 this->output_file_
->open (ACE_TEXT_ALWAYS_CHAR (temp
),
122 ios::out
| (append
? ios::app
: ios::trunc
));
123 if (this->output_file_
->bad ())
125 #else /* when ACE_LACKS_IOSTREAM_TOTALLY */
126 ACE_TCHAR
*fmode
= 0;
128 fmode
= ACE_TEXT ("a");
130 fmode
= ACE_TEXT ("w");
131 this->output_file_
= ACE_OS::fopen (temp
, fmode
);
132 # endif /* ACE_LACKS_IOSTREAM_TOTALLY */
134 ACE_LOG_MSG
->msg_ostream (this->output_file ());
135 ACE_LOG_MSG
->clr_flags (ACE_Log_Msg::STDERR
| ACE_Log_Msg::LOGGER
);
136 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::OSTREAM
);
142 ACE_Test_Output::close ()
144 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
145 this->output_file_
->flush ();
146 this->output_file_
->close ();
148 ACE_OS::fflush (this->output_file_
);
149 ACE_OS::fclose (this->output_file_
);
150 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
154 ACE_Test_Output::instance ()
156 if (ACE_Test_Output::instance_
== 0)
158 // Perform Double-Checked Locking Optimization.
159 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex
, ace_mon
,
160 *ACE_Static_Object_Lock::instance (), 0));
162 if (ACE_Test_Output::instance_
== 0)
164 ACE_NEW_RETURN (ACE_Test_Output::instance_
,
167 ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Test_Output
, ACE_Test_Output::instance_
)
170 return ACE_Test_Output::instance_
;
174 ACE_Test_Output::dll_name ()
176 return ACE_TEXT ("Test_Output");
180 ACE_Test_Output::name ()
182 return ACE_TEXT ("ACE_Test_Output");
186 ACE_Test_Output::close_singleton ()
188 delete ACE_Test_Output::instance_
;
189 ACE_Test_Output::instance_
= 0;
193 randomize (int array
[], size_t size
)
197 for (i
= 0; i
< size
; i
++)
198 array
[i
] = static_cast<int> (i
);
200 // See with a fixed number so that we can produce "repeatable"
204 // Generate an array of random numbers from 0 .. size - 1.
206 for (i
= 0; i
< size
; i
++)
208 size_t index
= ACE_OS::rand() % size
--;
209 int temp
= array
[index
];
210 array
[index
] = array
[size
];