Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

539 perf. colorization code and unify color selection #544

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/vt/configs/arguments/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@
#include <string>
#include <vector>

#include <unistd.h>

#include "CLI/CLI11.hpp"

namespace vt { namespace arguments {

/*static*/ bool ArgConfig::vt_color = true;
/*static*/ bool ArgConfig::vt_color = false;
/*static*/ bool ArgConfig::vt_no_color = false;
/*static*/ bool ArgConfig::vt_auto_color = false;
/*static*/ bool ArgConfig::vt_auto_color = true;
/*static*/ bool ArgConfig::vt_quiet = false;

/*static*/ bool ArgConfig::colorize_output = false;

/*static*/ bool ArgConfig::vt_no_sigint = false;
/*static*/ bool ArgConfig::vt_no_sigsegv = false;
/*static*/ bool ArgConfig::vt_no_terminate = false;
Expand Down Expand Up @@ -155,9 +159,9 @@ namespace vt { namespace arguments {
* Flags for controlling the colorization of output from vt
*/
auto quiet = "Quiet the output from vt (only errors, warnings)";
auto always = "Always colorize output";
auto never = "Never colorize output";
auto maybe = "Use isatty to determine colorization of output";
auto always = "Colorize output (overrides --vt_auto_color)";
auto never = "Never colorize output (overrides --vt_color)";
auto maybe = "Automatic colorization of output (default, unnecessary)";
auto a = app.add_flag("-c,--vt_color", vt_color, always);
auto b = app.add_flag("-n,--vt_no_color", vt_no_color, never);
auto c = app.add_flag("-a,--vt_auto_color", vt_auto_color, maybe);
Expand Down Expand Up @@ -446,6 +450,15 @@ namespace vt { namespace arguments {
return app.exit(ex);
}

// Determine the final colorization setting.
if (vt_no_color) {
colorize_output = false;
} else if (vt_color) {
colorize_output = true;
} else { // assume auto-color
colorize_output = isatty(fileno(stdout));
}

/*
* Put the arguments back into argc, argv, but properly order them based on
* the input order by comparing between the current args
Expand Down
3 changes: 3 additions & 0 deletions src/vt/configs/arguments/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct ArgConfig {
static bool vt_auto_color;
static bool vt_quiet;

// Derived from vt_*_color arguments after parsing.
static bool colorize_output;

static bool vt_no_sigint;
static bool vt_no_sigsegv;
static bool vt_no_terminate;
Expand Down
44 changes: 18 additions & 26 deletions src/vt/configs/debug/debug_colorize.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,36 @@
#include "vt/configs/types/types_type.h"

#include <string>
#include <unistd.h>
// #include <sys/stat.h>

namespace vt { namespace debug {

inline auto istty() -> bool {
return isatty(fileno(stdout)) ? true : false;
inline bool colorizeOutput() {
return arguments::ArgConfig::colorize_output;
}

inline auto ttyc() -> bool {
auto nocolor = arguments::ArgConfig::vt_no_color ? false : true;
auto tty = arguments::ArgConfig::vt_auto_color ? istty() : nocolor;
return tty;
}

inline auto green() -> std::string { return ttyc() ? "\033[32m" : ""; }
inline auto bold() -> std::string { return ttyc() ? "\033[1m" : ""; }
inline auto magenta() -> std::string { return ttyc() ? "\033[95m" : ""; }
inline auto red() -> std::string { return ttyc() ? "\033[31m" : ""; }
inline auto bred() -> std::string { return ttyc() ? "\033[31;1m" : ""; }
inline auto reset() -> std::string { return ttyc() ? "\033[00m" : ""; }
inline auto bd_green() -> std::string { return ttyc() ? "\033[32;1m" : ""; }
inline auto it_green() -> std::string { return ttyc() ? "\033[32;3m" : ""; }
inline auto un_green() -> std::string { return ttyc() ? "\033[32;4m" : ""; }
inline auto byellow() -> std::string { return ttyc() ? "\033[33;1m" : ""; }
inline auto yellow() -> std::string { return ttyc() ? "\033[33m" : ""; }
inline auto blue() -> std::string { return ttyc() ? "\033[34m" : ""; }
inline std::string green() { return colorizeOutput() ? "\033[32m" : ""; }
inline std::string bold() { return colorizeOutput() ? "\033[1m" : ""; }
inline std::string magenta() { return colorizeOutput() ? "\033[95m" : ""; }
inline std::string red() { return colorizeOutput() ? "\033[31m" : ""; }
inline std::string bred() { return colorizeOutput() ? "\033[31;1m" : ""; }
inline std::string reset() { return colorizeOutput() ? "\033[00m" : ""; }
inline std::string bd_green() { return colorizeOutput() ? "\033[32;1m" : ""; }
inline std::string it_green() { return colorizeOutput() ? "\033[32;3m" : ""; }
inline std::string un_green() { return colorizeOutput() ? "\033[32;4m" : ""; }
inline std::string byellow() { return colorizeOutput() ? "\033[33;1m" : ""; }
inline std::string yellow() { return colorizeOutput() ? "\033[33m" : ""; }
inline std::string blue() { return colorizeOutput() ? "\033[34m" : ""; }

inline auto emph(std::string str) -> std::string {
inline std::string emph(std::string str) {
return magenta() + str + reset();
}
inline auto reg(std::string str) -> std::string {
inline std::string reg(std::string str) {
return green() + str + reset();
}
inline auto vtPre() -> std::string {
inline std::string vtPre() {
return bd_green() + std::string("vt") + reset() + ": ";
}
inline auto proc(vt::NodeType const& node) -> std::string {
inline std::string proc(vt::NodeType const& node) {
return blue() + "[" + std::to_string(node) + "]" + reset();
}

Expand Down
35 changes: 19 additions & 16 deletions src/vt/configs/debug/debug_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,29 @@
debug_test(backend,line_file, "{}:{} ", )

#define vt_type_print_colorize(debug_type)
vt_print_colorize_impl("\033[32m", debug_pretty_print(debug_type), ":")
vt_print_colorize_impl(::vt::debug::green(), debug_pretty_print(debug_type), ":")

*/

/// Colorize the first string followed by the second in normal color.
/// Honors colorization checks.
#define vt_print_colorize_impl(color, str, str2) \
((::vt::debug::ttyc()) ? \
(std::string(color) + std::string(str) + std::string("\033[00m") + \
(::vt::debug::colorizeOutput() ? \
(color + std::string(str) + ::vt::debug::reset() + \
std::string(str2)) : \
std::string(str) + std::string(str2))

#define vt_print_colorize vt_print_colorize_impl("\033[32;1m", "vt", ":")
#define vt_print_colorize \
vt_print_colorize_impl(::vt::debug::bd_green(), "vt", ":")

#define vt_proc_print_colorize(proc) \
vt_print_colorize_impl("\033[34m", "[" + std::to_string(proc) + "]", "")
#define vt_proc_print_colorize(proc) \
vt_print_colorize_impl(::vt::debug::blue(), "[" + std::to_string(proc) + "]", "")

#define debug_argument_option(opt) \
::vt::arguments::ArgConfig::vt_debug_ ## opt

#define debug_all_option ::vt::arguments::ArgConfig::vt_debug_all

namespace vt { namespace runtime {
struct Runtime;
} /* end namespace runtime */
extern runtime::Runtime* curRT;
} /* end namespace vt */

#define debug_print_impl(force, inconfig, inmode, cat, ctx, ...) \
vt::config::ApplyOp< \
vt::config::DebugPrintOp, \
Expand Down Expand Up @@ -150,11 +147,17 @@ extern runtime::Runtime* curRT;

#define vt_option_check_enabled(mode, bit) ((mode & bit) not_eq 0)

namespace vt { namespace debug {
namespace vt { namespace runtime {
struct Runtime;
}} /* end namespace vt::runtime */

NodeType preNode();
namespace vt {
extern runtime::Runtime* curRT;
} /* end namespace vt */

}} /* end naamespace vt::ctx */
namespace vt { namespace debug {
NodeType preNode();
}} /* end namespace vt::debug */

namespace vt { namespace config {

Expand All @@ -170,7 +173,7 @@ static inline void debugPrintImpl(NodeType node, Arg&& arg, Args&&... args) {
"{} {} {} {}",
vt_print_colorize,
vt_proc_print_colorize(node),
vt_print_colorize_impl("\033[32m", PrettyPrintCat<cat>::print(), ":"),
vt_print_colorize_impl(::vt::debug::green(), PrettyPrintCat<cat>::print(), ":"),
user
);
if (vt_option_check_enabled(mod, ModeEnum::flush)) {
Expand Down
16 changes: 7 additions & 9 deletions src/vt/runtime/runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,14 @@ void Runtime::printStartupBanner() {
auto f11 = fmt::format("Disabling color output");
auto f12 = opt_on("--vt_no_color", f11);
fmt::print("{}\t{}{}", vt_pre, f12, reset);
} else if (ArgType::vt_color) {
auto f11 = fmt::format("Color output enabled");
auto f12 = opt_on("--vt_color", f11);
fmt::print("{}\t{}{}", vt_pre, f12, reset);
} else {
if (ArgType::vt_auto_color) {
auto f11 = fmt::format("Automatic TTY detection for color output");
auto f12 = opt_on("--vt_auto_color", f11);
fmt::print("{}\t{}{}", vt_pre, f12, reset);
} else {
auto f11 = fmt::format("Color output enabled by default");
auto f12 = opt_inverse("--vt_no_color", f11);
fmt::print("{}\t{}{}", vt_pre, f12, reset);
}
auto f11 = fmt::format("Automatically color output (if terminal)");
auto f12 = opt_inverse("--vt_no_color", f11);
fmt::print("{}\t{}{}", vt_pre, f12, reset);
}

if (ArgType::vt_no_stack) {
Expand Down