Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Test_Output.cpp
blobdc236ce0567b16bb8bf3ecba8c19b992ddf43f88
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, 0);
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 (!defined (ACE_HAS_PHARLAP) || defined (ACE_PHARLAP_TESTLOG_TO_FILE))
57 delete this->output_file_;
58 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
61 OFSTREAM *
62 ACE_Test_Output::output_file (void)
64 // the output_file_ is loaned to ACE_LOG_MSG
65 // and something else might destroy and/or change the stream
66 // so return what ACE_LOG_MSG is using.
67 #if defined (ACE_LACKS_IOSTREAM_TOTALLY)
68 return reinterpret_cast<OFSTREAM*>(ACE_LOG_MSG->msg_ostream ());
69 #else
70 return dynamic_cast<OFSTREAM*>(ACE_LOG_MSG->msg_ostream ());
71 #endif /* ACE_LACKS_IOSTREAM_TOTALLY */
74 int
75 ACE_Test_Output::set_output (const ACE_TCHAR *filename, int append)
77 #if defined (ACE_HAS_PHARLAP) && !defined (ACE_PHARLAP_TESTLOG_TO_FILE)
78 // For PharLap, just send it all to the host console for now - redirect
79 // to a file there for saving/analysis.
80 EtsSelectConsole(ETS_CO_HOST);
81 ACE_LOG_MSG->msg_ostream (&cout);
83 #else
84 ACE_TCHAR temp[MAXPATHLEN + 1] = { 0 };
85 // Ignore the error value since the directory may already exist.
86 const ACE_TCHAR *test_dir = 0;
88 #if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
89 test_dir = ACE_OS::getenv (ACE_TEXT ("ACE_TEST_DIR"));
90 #else
91 ACE_TCHAR tempenv[MAXPATHLEN + 1] = { 0 };
92 char const * const test_dir_n = ACE_OS::getenv ("ACE_TEST_DIR");
93 if (test_dir_n == 0)
95 test_dir = 0;
97 else
99 ACE_OS::strncpy (tempenv,
100 ACE_TEXT_CHAR_TO_TCHAR (test_dir_n),
101 MAXPATHLEN);
102 test_dir = tempenv;
105 #endif /* ACE_WIN32 */
106 if (test_dir == 0)
107 test_dir = ACE_DEFAULT_TEST_DIR;
109 // This could be done with ACE_OS::sprintf() but it requires different
110 // format strings for wide-char POSIX vs. narrow-char POSIX and Windows.
111 // Easier to keep straight like this.
112 ACE_OS::strncpy (temp, test_dir, MAXPATHLEN);
113 ACE_OS::strcat (temp, ACE_LOG_DIRECTORY);
114 ACE_OS::strcat (temp,
115 ACE::basename (filename, ACE_DIRECTORY_SEPARATOR_CHAR));
116 ACE_OS::strcat (temp, ACE_LOG_FILE_EXT_NAME);
118 #if defined (ACE_VXWORKS)
119 // This is the only way I could figure out to avoid a console
120 // warning about opening an existing file (w/o O_CREAT), or
121 // attempting to unlink a non-existant one.
122 ACE_HANDLE fd = ACE_OS::open (temp,
123 O_WRONLY|O_CREAT,
124 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
125 if (fd != ERROR)
127 ACE_OS::close (fd);
128 ACE_OS::unlink (temp);
130 #else /* ! ACE_VXWORKS */
131 // This doesn't seem to work on VxWorks if the directory doesn't
132 // exist: it creates a plain file instead of a directory. If the
133 // directory does exist, it causes a weird console error message
134 // about "cat: input error on standard input: Is a directory". So,
135 // VxWorks users must create the directory manually.
136 ACE_OS::mkdir (ACE_LOG_DIRECTORY_FOR_MKDIR);
137 #endif /* ACE_VXWORKS */
139 # if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
140 this->output_file_->open (ACE_TEXT_ALWAYS_CHAR (temp),
141 ios::out | (append ? ios::app : ios::trunc));
142 if (!this->output_file_->good ())
143 return -1;
144 #else /* when ACE_LACKS_IOSTREAM_TOTALLY */
145 const ACE_TCHAR *fmode = 0;
146 if (append)
147 fmode = ACE_TEXT ("a");
148 else
149 fmode = ACE_TEXT ("w");
150 this->output_file_ = ACE_OS::fopen (temp, fmode);
151 # endif /* ACE_LACKS_IOSTREAM_TOTALLY */
153 ACE_LOG_MSG->msg_ostream (this->output_file_, 0);
154 #endif /* ACE_HAS_PHARLAP && !ACE_PHARLAP_TESTLOG_TO_FILE */
156 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER );
157 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
159 return 0;
162 void
163 ACE_Test_Output::close (void)
165 if (this->output_file_ &&
166 (this->output_file_ == ACE_LOG_MSG->msg_ostream ()))
168 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
169 this->output_file_->flush ();
170 this->output_file_->close ();
171 #else
172 ACE_OS::fflush (this->output_file_);
173 ACE_OS::fclose (this->output_file_);
174 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
175 ACE_LOG_MSG->msg_ostream (0, 0);
177 // else something else changed the stream and hence should
178 // have closed the output_file_
181 ACE_Test_Output *
182 ACE_Test_Output::instance ()
184 if (ACE_Test_Output::instance_ == 0)
186 // Perform Double-Checked Locking Optimization.
187 ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
188 *ACE_Static_Object_Lock::instance (), 0));
190 if (ACE_Test_Output::instance_ == 0)
192 ACE_NEW_RETURN (ACE_Test_Output::instance_,
193 ACE_Test_Output,
195 ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Test_Output, ACE_Test_Output::instance_)
198 return ACE_Test_Output::instance_;
201 const ACE_TCHAR *
202 ACE_Test_Output::dll_name (void)
204 return ACE_TEXT ("Test_Output");
207 const ACE_TCHAR *
208 ACE_Test_Output::name (void)
210 return ACE_TEXT ("ACE_Test_Output");
213 void
214 ACE_Test_Output::close_singleton (void)
216 delete ACE_Test_Output::instance_;
217 ACE_Test_Output::instance_ = 0;