diff --git a/src/shrpx.cc b/src/shrpx.cc index 202da17c..2575878d 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -369,6 +369,7 @@ void fill_default_config() mod_config()->gid = 0; mod_config()->backend_ipv4 = false; mod_config()->backend_ipv6 = false; + mod_config()->tty = isatty(fileno(stderr)); } } // namespace diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 4301d25a..ca03f20f 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -122,6 +122,8 @@ struct Config { char *cacert; bool backend_ipv4; bool backend_ipv6; + // true if stderr refers to a terminal. + bool tty; }; const Config* get_config(); diff --git a/src/shrpx_log.cc b/src/shrpx_log.cc index 2ab9f0d7..b9961fcf 100644 --- a/src/shrpx_log.cc +++ b/src/shrpx_log.cc @@ -33,9 +33,20 @@ namespace shrpx { +namespace { const char *SEVERITY_STR[] = { "INFO", "WARN", "ERROR", "FATAL" }; +} // namespace + +namespace { +const char *SEVERITY_COLOR[] = { + "\033[1;32m", // INFO + "\033[1;33m", // WARN + "\033[1;31m", // ERROR + "\033[1;35m", // FATAL +}; +} // namespace int Log::severity_thres_ = WARNING; @@ -80,12 +91,17 @@ Log::Log(int severity, const char *filename, int linenum) Log::~Log() { if(severity_ >= severity_thres_) { - fprintf(stderr, "[%s] %s\n (%s, line %d)\n", - SEVERITY_STR[severity_], stream_.str().c_str(), - filename_, linenum_); + fprintf(stderr, "[%s%s%s] %s\n %s(%s:%d)%s\n", + get_config()->tty ? SEVERITY_COLOR[severity_] : "", + SEVERITY_STR[severity_], + get_config()->tty ? "\033[0m" : "", + stream_.str().c_str(), + get_config()->tty ? "\033[1;30m" : "", + filename_, linenum_, + get_config()->tty ? "\033[0m" : ""); fflush(stderr); if(get_config()->use_syslog) { - syslog(severity_to_syslog_level(severity_), "%s (%s, line %d)\n", + syslog(severity_to_syslog_level(severity_), "%s (%s:%d)\n", stream_.str().c_str(), filename_, linenum_); } }