cppx-core
debugging-output.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 #include <cppx-core/collections/is_in.hpp> // cppx::is_in
3 #include <cppx-core/io/monospaced_bullet_block.hpp> // cppx::monospaced_bullet_block
4 #include <cppx-core/language/syntax/macro-use.hpp> // CPPX_USE_STD
5 
6 //#include <c/stdio.hpp> // FILE, fflush, fprintf, stderr, stdout
7 #include <iostream> // std::(cerr, clog, endl)
8 #include <string_view> // std::(string_view)
9 
10 namespace cppx
11 {
12  namespace debug
13  {
14  CPPX_USE_STD( cerr, cout, clog, endl, ostream, string_view );
15 
16  inline auto put_stderr_line( const string_view s ) noexcept
17  -> Truth
18  {
19  if( not is_empty( s ) )
20  {
21  fprintf( stderr, "%.*s\n", int( s.length() ), s.data() );
22  }
23  fflush( stderr );
24  return !!ferror( stderr );
25  }
26 
27  inline auto put_line(
28  ostream& stream,
29  const string_view s
30  ) noexcept
31  -> Truth
32  {
33  if( is_in( { &cerr, &clog }, &stream ) )
34  {
35  // Going down to the C level avoids effect where last '\n' is chopped off.
36  stream.flush();
37  return put_stderr_line( s );
38  }
39  else
40  {
41  const std::ios::iostate original_x_flags = stream.exceptions();
42  stream << s << endl;
43  const Truth ok = not stream.fail();
44  if( original_x_flags ) { stream.exceptions( original_x_flags ); }
45  return ok;
46  }
47  }
48 
49  inline auto put_line( const string_view s ) noexcept
50  -> Truth
51  { return put_line( cerr, s ); }
52 
53  inline auto put_block_to(
54  ostream& stream,
55  const string_view bullet,
56  const string_view s
57  )
58  -> Truth
59  { return put_line( stream, monospaced_bullet_block( s, bullet ) ); }
60 
61  inline auto put_info_block(
62  const string_view s,
63  ostream& stream = clog
64  )
65  -> Truth
66  { return put_block_to( stream, "i", s ); }
67 
68  inline auto put_error_block(
69  const string_view s,
70  ostream& stream = cerr
71  )
72  -> Truth
73  { return put_block_to( stream, best_effort::right_arrow_str, s ); }
74 
75  } // namespace debug
76 } // namespace cppx
constexpr auto & right_arrow_str
auto put_line(ostream &stream, const string_view s) noexcept -> Truth
A drop-in replacement for bool without implicit conversion from/to types other than bool.
Definition: Truth.hpp:34
auto put_block_to(ostream &stream, const string_view bullet, const string_view s) -> Truth
auto put_error_block(const string_view s, ostream &stream=cerr) -> Truth
auto put_stderr_line(const string_view s) noexcept -> Truth
auto is_in(const basic_string_view< Char > &sv, const Char ch) noexcept -> Truth
Definition: is_in.hpp:21
CPPX_USE_STD(cerr, cout, clog, endl, ostream, string_view)
auto is_empty(const Collection &c) -> Truth
Definition: is_empty.hpp:33
auto put_info_block(const string_view s, ostream &stream=clog) -> Truth
auto monospaced_bullet_block(const string_view &s, const string_view &bullet=best_effort::bullet_str, const int indent_size=4) -> string
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...