@@ -36,29 +36,40 @@ using ecsact::cli::warning_message;
3636#endif
3737
3838namespace {
39- auto print_text_report (const alert_message& msg) -> void {
40- std::cout << std::format ( //
39+ constexpr auto get_outputstream (auto && stream, auto && preferred)
40+ -> decltype(auto ) {
41+ if constexpr (std::is_same_v<
42+ std::nullptr_t ,
43+ std::remove_cvref_t <decltype (stream)>>) {
44+ return preferred;
45+ } else {
46+ return stream;
47+ }
48+ }
49+
50+ auto print_text_report (auto && output, const alert_message& msg) -> void {
51+ get_outputstream (output, std::cout) << std::format ( //
4152 COLOR_RED " ALERT:" COLOR_RESET " {}\n " ,
4253 msg.content
4354 );
4455}
4556
46- auto print_text_report (const info_message& msg) -> void {
47- std::cout << std::format ( //
57+ auto print_text_report (auto && output, const info_message& msg) -> void {
58+ get_outputstream (output, std::cout) << std::format ( //
4859 COLOR_GRN " INFO:" COLOR_RESET " {}\n " ,
4960 msg.content
5061 );
5162}
5263
53- auto print_text_report (const error_message& msg) -> void {
54- std::cout << std::format ( //
64+ auto print_text_report (auto && output, const error_message& msg) -> void {
65+ get_outputstream (output, std::cerr) << std::format ( //
5566 COLOR_RED " ERROR:" COLOR_RESET " {}\n " ,
5667 msg.content
5768 );
5869}
5970
60- auto print_text_report (const ecsact_error_message& msg) -> void {
61- std::cerr << std::format ( //
71+ auto print_text_report (auto && output, const ecsact_error_message& msg) -> void {
72+ get_outputstream (output, std::cerr) << std::format ( //
6273 COLOR_RED " ERROR:" COLOR_RESET " {}:{}:{}\n {}\n " ,
6374 msg.ecsact_source_path ,
6475 msg.line ,
@@ -67,31 +78,34 @@ auto print_text_report(const ecsact_error_message& msg) -> void {
6778 );
6879}
6980
70- auto print_text_report (const warning_message& msg) -> void {
71- std::cout << std::format ( //
81+ auto print_text_report (auto && output, const warning_message& msg) -> void {
82+ get_outputstream (output, std::cout) << std::format ( //
7283 COLOR_YEL " WARNING:" COLOR_RESET " {}\n " ,
7384 msg.content
7485 );
7586}
7687
77- auto print_text_report (const success_message& msg) -> void {
78- std::cout << std::format ( //
88+ auto print_text_report (auto && output, const success_message& msg) -> void {
89+ get_outputstream (output, std::cout) << std::format ( //
7990 COLOR_GRN " SUCCESS:" COLOR_RESET " {}\n " ,
8091 msg.content
8192 );
8293}
8394
84- auto print_text_report (const module_methods_message& msg) -> void {
85- std::cout << " MODULE METHODS FOR " << msg.module_name << " :\n " ;
95+ auto print_text_report (auto && output, const module_methods_message& msg)
96+ -> void {
97+ get_outputstream (output, std::cout)
98+ << " MODULE METHODS FOR " << msg.module_name << " :\n " ;
8699 for (auto & method : msg.methods ) {
87100 std::cout //
88101 << " " << (method.available ? COLOR_GRN " YES " : COLOR_RED " NO " )
89102 << COLOR_RESET << method.method_name << " \n " ;
90103 }
91104}
92105
93- auto print_text_report (const subcommand_start_message& msg) -> void {
94- std::cout << std::format ( //
106+ auto print_text_report (auto && output, const subcommand_start_message& msg)
107+ -> void {
108+ get_outputstream (output, std::cout) << std::format ( //
95109 COLOR_BLU " SUBCOMMAND({}) START >>" COLOR_RESET " {} " ,
96110 msg.id ,
97111 msg.executable
@@ -102,32 +116,36 @@ auto print_text_report(const subcommand_start_message& msg) -> void {
102116 std::cout << " \n " ;
103117}
104118
105- auto print_text_report (const subcommand_stdout_message& msg) -> void {
106- std::cout << std::format ( //
119+ auto print_text_report (auto && output, const subcommand_stdout_message& msg)
120+ -> void {
121+ get_outputstream (output, std::cout) << std::format ( //
107122 COLOR_BLU " SUBCOMMAND({}) STDOUT:" COLOR_RESET " {}\n " ,
108123 msg.id ,
109124 msg.line
110125 );
111126}
112127
113- auto print_text_report (const subcommand_stderr_message& msg) -> void {
114- std::cout << std::format ( //
128+ auto print_text_report (auto && output, const subcommand_stderr_message& msg)
129+ -> void {
130+ get_outputstream (output, std::cout) << std::format ( //
115131 COLOR_RED " SUBCOMMAND({}) STDERR:" COLOR_RESET " {}\n " ,
116132 msg.id ,
117133 msg.line
118134 );
119135}
120136
121- auto print_text_report (const subcommand_progress_message& msg) -> void {
122- std::cout << std::format ( //
137+ auto print_text_report (auto && output, const subcommand_progress_message& msg)
138+ -> void {
139+ get_outputstream (output, std::cout) << std::format ( //
123140 COLOR_BLU " SUBCOMMAND({}) PROG:" COLOR_RESET " {}\n " ,
124141 msg.id ,
125142 msg.description
126143 );
127144}
128145
129- auto print_text_report (const subcommand_end_message& msg) -> void {
130- std::cout << std::format ( //
146+ auto print_text_report (auto && output, const subcommand_end_message& msg)
147+ -> void {
148+ get_outputstream (output, std::cout) << std::format ( //
131149 COLOR_BLU " SUBCOMMAND({}) END << " COLOR_RESET " exit code {}\n " ,
132150 msg.id ,
133151 msg.exit_code
@@ -138,5 +156,17 @@ auto print_text_report(const subcommand_end_message& msg) -> void {
138156auto ecsact::cli::detail::text_report::operator ()( //
139157 const message_variant_t & message
140158) const -> void {
141- std::visit ([](const auto & message) { print_text_report (message); }, message);
159+ std::visit (
160+ [](const auto & message) { print_text_report (nullptr , message); },
161+ message
162+ );
163+ }
164+
165+ auto ecsact::cli::detail::text_report_stderr_only::operator ()( //
166+ const message_variant_t & message
167+ ) const -> void {
168+ std::visit (
169+ [](const auto & message) { print_text_report (std::cerr, message); },
170+ message
171+ );
142172}
0 commit comments