Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_FancyOStream_def.hpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_FANCY_O_STREAM_DEF_HPP
11#define TEUCHOS_FANCY_O_STREAM_DEF_HPP
12
13#include "Teuchos_FancyOStream.hpp"
14
15namespace Teuchos {
16
17RCP<basic_FancyOStream<char> >
18fancyOStream(
19 const RCP< std::basic_ostream<char> >& oStream,
20 const std::basic_string<char>& tabIndentStr,
21 const int startingTab,
22 const bool showLinePrefix,
23 const int maxLenLinePrefix,
24 const bool showTabCount,
25 const bool showProcRank
26 )
27{
28 if (nonnull(oStream)) {
29 return rcp(
33 )
34 );
35 }
36 return null;
37}
38
39
41getFancyOStream( const RCP<std::basic_ostream<char> > &out )
42{
43 if (is_null(out))
44 return Teuchos::null;
48 return fancyOut;
49 return rcp(new basic_FancyOStream<char>(out));
50}
51
52template <typename CharT, typename Traits>
54tab(
56 const int tabs,
57 const std::basic_string<CharT,Traits> linePrefix
58 )
59{
60 if(out.get()==NULL)
61 return Teuchos::null;
63 set_extra_data(
65 "OSTab",
66 inOutArg(fancyOut),
67 PRE_DESTROY,
68 false
69 );
70 return fancyOut;
71}
73
74template <typename CharT, typename Traits>
76tab(
77 const RCP<std::basic_ostream<CharT,Traits> > &out
78 ,const int tabs
79 ,const std::basic_string<CharT,Traits> linePrefix
80 )
82 return tab(getFancyOStream(out),tabs,linePrefix);
83}
85
86// ////////////////////////////////
87// Defintions
88
89
90//
91// basic_FancyOStream_buf
92//
94
95template<typename CharT, typename Traits>
97 const RCP<std::basic_ostream<char_type,traits_type> > &oStream
98 ,const std::basic_string<char_type,traits_type> &tabIndentStr
99 ,const int startingTab
100 ,const bool showLinePrefix
101 ,const int maxLenLinePrefix
102 ,const bool showTabCount
103 ,const bool showProcRank
104 )
105{
108}
109
110
111template<typename CharT, typename Traits>
113 const RCP<std::basic_ostream<char_type,traits_type> > &oStream
114 ,const std::basic_string<char_type,traits_type> &tabIndentStr
115 ,const int startingTab
116 ,const bool showLinePrefix
117 ,const int maxLenLinePrefix
118 ,const bool showTabCount
119 ,const bool showProcRank
120 )
121{
122 oStreamSet_ = oStream;
123 oStream_ = oStream;
124 tabIndentStr_ = tabIndentStr;
125 showLinePrefix_ = showLinePrefix;
126 maxLenLinePrefix_ = maxLenLinePrefix;
127 showTabCount_ = showTabCount;
128 showProcRank_ = showProcRank;
129 rootRank_ = -1;
130 procRank_ = GlobalMPISession::getRank();
131 numProcs_ = GlobalMPISession::getNProc();
132 rankPrintWidth_ = int(std::log10(float(numProcs_)))+1;
133 tabIndent_ = startingTab;
134 tabIndentStack_.clear();
135 linePrefixStack_.clear();
136 wroteNewline_ = true;
137 enableTabbingStack_ = 0;
139
140
141template<typename CharT, typename Traits>
144{
145 return oStreamSet_;
146}
147
149template<typename CharT, typename Traits>
151 const std::basic_string<char_type,traits_type> &tabIndentStr
152 )
153{
154 tabIndentStr_ = tabIndentStr;
155}
156
157
158template<typename CharT, typename Traits>
159const std::basic_string<CharT,Traits>&
161{
162 return tabIndentStr_;
164
165
166template<typename CharT, typename Traits>
169 showLinePrefix_ = showLinePrefix;
170}
172
173template<typename CharT, typename Traits>
175{
176 return showLinePrefix_;
178
179
180template<typename CharT, typename Traits>
186
187
188template<typename CharT, typename Traits>
190{
191 return maxLenLinePrefix_;
192}
193
194
195template<typename CharT, typename Traits>
200
201
202template<typename CharT, typename Traits>
204{
205 return showTabCount_;
206}
207
208
209template<typename CharT, typename Traits>
214
215
216template<typename CharT, typename Traits>
218{
219 return showProcRank_;
220}
221
222
223template<typename CharT, typename Traits>
225 const int procRank, const int numProcs
226 )
227{
228 procRank_ = procRank;
229 numProcs_ = numProcs;
230}
231
232
233template<typename CharT, typename Traits>
235{
236 return procRank_;
237}
238
239
240template<typename CharT, typename Traits>
242{
243 return numProcs_;
244}
245
246
247template<typename CharT, typename Traits>
249 const int rootRank
250 )
251{
252 rootRank_ = rootRank;
253 if(rootRank >= 0) {
254 if(rootRank == procRank_)
255 oStream_ = oStreamSet_;
256 else
257 oStream_ = rcp(new oblackholestream());
258 // Only processor is being output to so there is no need for line
259 // batching!
260 lineOut_ = null;
261 }
262 else {
263 oStream_ = oStreamSet_;
264 // Output is being sent to all processors so we need line batching to
265 // insure that each line will be printed all together!
266 lineOut_ = rcp(new std::ostringstream());
267 }
268}
269
270
271template<typename CharT, typename Traits>
273{
274 return rootRank_;
275}
276
277
278template<typename CharT, typename Traits>
280{
281 if( tabIndent_ + tabs < 0 ) {
282 tabIndentStack_.push_back(-tabIndent_);
283 tabIndent_ = 0;
284 }
285 else {
286 tabIndentStack_.push_back(tabs);
287 tabIndent_ += tabs;
288 }
289}
290
291
292template<typename CharT, typename Traits>
294{
295 return tabIndent_;
296}
297
298
299template<typename CharT, typename Traits>
301{
302 tabIndent_ -= tabIndentStack_.back();
303 tabIndentStack_.pop_back();
304}
305
306
307template<typename CharT, typename Traits>
309 const std::basic_string<char_type,traits_type> &linePrefix
310 )
311{
312 linePrefixStack_.push_back(linePrefix);
313}
314
315
316template<typename CharT, typename Traits>
318{
319 linePrefixStack_.pop_back();
320}
321
322
323template<typename CharT, typename Traits>
324const std::basic_string<CharT,Traits>&
326{
327 return linePrefixStack_.back();
328}
329
330
331template<typename CharT, typename Traits>
333{
334 ++enableTabbingStack_;
335}
336
337
338template<typename CharT, typename Traits>
340{
341 --enableTabbingStack_;
342}
343
344
345// protected
346
347
348template<typename CharT, typename Traits>
350 const char_type* s, std::streamsize n
351 )
352{
353#ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
354 std::cerr << "\ncalled xsputn()\n";
355#endif
356 writeChars(s,n);
357 return n;
358}
359
360
361template<typename CharT, typename Traits>
364{
365#ifdef TEUCHOS_FANCY_OSTREAM_SHOW_ALL_CALLS
366 std::cerr << "\ncalled overflow()\n";
367#endif
368 if(c != traits_type::eof()) {
369 const char_type cc[] = { traits_type::to_char_type(c) };
370 this->writeChars(cc,1);
371 }
372 return traits_type::not_eof(c);
373 //return std::basic_streambuf<CharT,Traits>::overflow(c);
374}
375
376
377// private
378
379
380template<typename CharT, typename Traits>
382{
383 if(lineOut_.get())
384 return *lineOut_;
385 return *oStream_;
386}
387
388
389template<typename CharT, typename Traits>
390void basic_FancyOStream_buf<CharT,Traits>::writeChars(
391 const char_type s[], std::streamsize n
392 )
393{
394 if(n == 0) return;
395 std::streamsize p = 0, first_p = 0;
396 bool done_outputting = false;
397 const char_type newline = '\n';
398 while( !done_outputting ) {
399 // Find the next newline
400 for( p = first_p; p < n; ++p ) {
401 if(s[p] == newline) {
402 break;
403 }
404 }
405 if(p == n) {
406 // We did not find a newline at the end!
407 --p;
409 }
410 else if( p == n-1 && s[p] == newline ) {
411 // The last character in the std::string is a newline
412 done_outputting = true;
413 }
414 // Write the beginning of the line if we need to
415 if(wroteNewline_) {
416 writeFrontMatter();
417 wroteNewline_ = false;
418 }
419 // Write up to the newline or the end of the std::string
420 out().write(s+first_p,p-first_p+1);
421 if(s[p] == newline) {
422 wroteNewline_ = true;
423 if(lineOut_.get()) {
424 *oStream_ << lineOut_->str() << std::flush;
425 lineOut_->str("");
426 }
427 }
428 // Update for next search
429 if(!done_outputting)
430 first_p = p+1;
431 }
433
434
435template<typename CharT, typename Traits>
437{
438 bool didOutput = false;
439 std::ostream &o = this->out();
440 if(showProcRank_) {
441 o << "p=" << std::right << std::setw(rankPrintWidth_) << procRank_;
442 didOutput = true;
443 }
444 if(showLinePrefix_) {
445 if(didOutput)
446 o << ", ";
447 std::string currLinePrefix = "";
448 if ( linePrefixStack_.size() )
449 currLinePrefix = this->getTopLinePrefix();
450 const int localMaxLenLinePrefix =
451 TEUCHOS_MAX( as<int>(currLinePrefix.length()), maxLenLinePrefix_ );
452 o << std::left << std::setw(localMaxLenLinePrefix);
453 o << currLinePrefix;
454 didOutput = true;
455 }
456 if(showTabCount_) {
457 if(didOutput)
458 o << ", ";
459 o << "tabs=" << std::right << std::setw(2) << tabIndent_;
460 didOutput = true;
461 }
462 // ToDo: Add the Prefix name if asked
463 // ToDo: Add the processor number if asked
464 // ToDo: Add the number of indents if asked
466 o << " |" << tabIndentStr_;
467 }
468 if(enableTabbingStack_==0) {
469 for( int i = 0; i < tabIndent_; ++i )
470 o << tabIndentStr_;
471 }
472}
473
474
475//
476// basic_FancyOStream
477//
478
479
480template<typename CharT, typename Traits>
482 const RCP< std::basic_ostream<char_type,traits_type> > &oStream
483 ,const std::basic_string<char_type,traits_type> &tabIndentStr
484 ,const int startingTab
485 ,const bool showLinePrefix
486 ,const int maxLenLinePrefix
487 ,const bool showTabCount
488 ,const bool showProcRank
489 )
490 :ostream_t(NULL),
493{
494 this->init(&streambuf_);
496
497
498template<typename CharT, typename Traits>
500 const RCP< std::basic_ostream<char_type,traits_type> > &oStream
501 ,const std::basic_string<char_type,traits_type> &tabIndentStr
502 ,const int startingTab
503 ,const bool showLinePrefix
504 ,const int maxLenLinePrefix
505 ,const bool showTabCount
506 ,const bool showProcRank
507 )
508{
511 this->init(&streambuf_);
512}
514
515template<typename CharT, typename Traits>
518{
519 return streambuf_.getOStream();
520}
521
522
523template<typename CharT, typename Traits>
526 const std::basic_string<char_type,traits_type> &tabIndentStr
527 )
529 streambuf_.setTabIndentStr(tabIndentStr);
530 return *this;
532
533
534template<typename CharT, typename Traits>
535const std::basic_string<CharT,Traits>&
538 return streambuf_.getTabIndentStr();
539}
541
542template<typename CharT, typename Traits>
553
554
555template<typename CharT, typename Traits>
562
563
564template<typename CharT, typename Traits>
571
572
573template<typename CharT, typename Traits>
580
581
582template<typename CharT, typename Traits>
585{
586 streambuf_.setShowProcRank(showProcRank);
587 return *this;
588}
589
590
591template<typename CharT, typename Traits>
594{
596 return *this;
597}
598
599
600template<typename CharT, typename Traits>
603{
604 streambuf_.setOutputToRootOnly(rootRank);
605 return *this;
606}
607
608
609template<typename CharT, typename Traits>
614
615
616template<typename CharT, typename Traits>
619{
620 //streambuf_.setTabIndentStr(oStream.streambuf_.getTabIndentStr());
621 streambuf_.setShowLinePrefix(oStream.streambuf_.getShowLinePrefix());
622 streambuf_.setMaxLenLinePrefix(oStream.streambuf_.getMaxLenLinePrefix());
623 streambuf_.setShowTabCount(oStream.streambuf_.getShowTabCount());
624 streambuf_.setShowProcRank(oStream.streambuf_.getShowProcRank());
625 streambuf_.setProcRankAndSize(oStream.streambuf_.getProcRank(),
626 oStream.streambuf_.getNumProcs());
627 streambuf_.setOutputToRootOnly(oStream.streambuf_.getOutputToRootOnly());
628}
629
630
631template<typename CharT, typename Traits>
633{
634 streambuf_.pushTab(tabs);
635}
636
637
638template<typename CharT, typename Traits>
640{
641 return streambuf_.getNumCurrTabs();
642}
643
644
645template<typename CharT, typename Traits>
647{
648 streambuf_.popTab();
649}
650
651
652template<typename CharT, typename Traits>
654 const std::basic_string<char_type,traits_type> &linePrefix
655 )
656{
657 streambuf_.pushLinePrefix(linePrefix);
658}
659
660
661template<typename CharT, typename Traits>
666
667
668template<typename CharT, typename Traits>
669const std::basic_string<CharT,Traits>&
674
675
676template<typename CharT, typename Traits>
681
682
683template<typename CharT, typename Traits>
688
689
690} // namespace Teuchos
691
692
693#endif // TEUCHOS_FANCY_O_STREAM_DEF_HPP
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
static int getNProc()
The number of processes in MPI_COMM_WORLD.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
Stream buffering class that performs the magic of indenting data sent to an std::ostream object.
int getNumProcs() const
Get the number of processes in the communicator.
void setShowProcRank(const bool showProcRank)
void setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
void setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
void setShowTabCount(const bool showTabCount)
void pushLinePrefix(const std::basic_string< char_type, traits_type > &linePrefix)
void setProcRankAndSize(const int procRank, const int numProcs)
Set the (MPI) process rank and the number of processes in the communicator.
int getProcRank() const
Get the rank of the calling (MPI) process.
void setMaxLenLinePrefix(const int maxLenLinePrefix)
void pushTab(const int tabs)
Push one or more tabs.
const std::basic_string< char_type, traits_type > & getTopLinePrefix() const
void initialize(const RCP< std::basic_ostream< char_type, traits_type > > &oStream, const std::basic_string< char_type, traits_type > &tabIndentStr, const int startingTab, const bool showLinePrefix, const int maxLenLinePrefix, const bool showTabCount, const bool showProcRank)
void setShowLinePrefix(const bool showLinePrefix)
std::streamsize xsputn(const char_type *s, std::streamsize n)
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
void initialize(const RCP< std::basic_ostream< char_type, traits_type > > &oStream, const std::basic_string< char_type, traits_type > &tabIndentStr=" ", const int startingTab=0, const bool showLinePrefix=false, const int maxLenLinePrefix=10, const bool showTabCount=false, const bool showProcRank=false)
Initialize the output stream.
basic_FancyOStream & setTabIndentStr(const std::basic_string< char_type, traits_type > &tabIndentStr)
Set the tab indent string.
basic_FancyOStream & setProcRankAndSize(const int procRank, const int numProcs)
Set the (MPI) process rank and the number of processes in the communicator.
const std::basic_string< char_type, traits_type > & getTabIndentStr() const
Get the tab indent string.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
basic_FancyOStream & setMaxLenLinePrefix(const int maxLenLinePrefix)
std::basic_ostream< char_type, traits_type > ostream_t
void pushLinePrefix(const std::basic_string< char_type, traits_type > &linePrefix)
basic_FancyOStream & setShowLinePrefix(const bool showLinePrefix)
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
Get the output stream this object wraps.
const std::basic_string< char_type, traits_type > & getTopLinePrefix() const
basic_FancyOStream & setShowTabCount(const bool showTabCount)
basic_FancyOStream & setShowProcRank(const bool showProcRank)
void pushTab(const int tabs=1)
Push one or more tabs.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
basic_oblackholestream< char, std::char_traits< char > > oblackholestream
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.