forked from apache/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dan and I were looking at some ksck output earlier and found it somewhat hard to read. This colorizes the output and also cleans up the format to be slightly less messy. The output now goes to stdout instead of stderr as well, so it's easier to pipe to a file or 'less', which is a common use case. Example output: Tablet 0e4e79de2c2547c091779b13735d2b73 of table 'my_table' is under-replicated: 1 replica(s) not RUNNING 63c4785c3b6f47f5a1c23ffbf6e52b71 (ts-023.foo.com:7050): RUNNING cd20c68cd23c4cf986eda0936a5691cc (ts-004.foo.com:7050): RUNNING [LEADER] 5edf82f0516b4897b3a7991a7e67d71c (ts-028.foo.com:7050): bad state State: NOT_STARTED Data state: TABLET_DATA_COPYING Last status: TabletCopy: Downloading block 4611685629730158289 (3491/9569) Change-Id: Icc5196d63cbcbcbb2a9aba1ff17377b678c104bd Reviewed-on: http://gerrit.cloudera.org:8080/4129 Tested-by: Kudu Jenkins Reviewed-by: Adar Dembo <[email protected]>
- Loading branch information
1 parent
362743c
commit cf0eb4d
Showing
7 changed files
with
299 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "kudu/tools/color.h" | ||
|
||
#include <gflags/gflags.h> | ||
#include <glog/logging.h> | ||
#include <unistd.h> | ||
|
||
#include "kudu/gutil/strings/substitute.h" | ||
#include "kudu/util/flag_tags.h" | ||
|
||
DEFINE_string(color, "auto", | ||
"Specifies whether output should be colorized. The default value " | ||
"'auto' colorizes output if the output is a terminal. The other " | ||
"valid values are 'always' or 'never'."); | ||
TAG_FLAG(color, stable); | ||
|
||
static bool ValidateColorFlag(const char* flagname, const string& value) { | ||
if (value == "always" || | ||
value == "auto" || | ||
value == "never") { | ||
return true; | ||
} | ||
LOG(ERROR) << "option 'color' expects \"always\", \"auto\", or \"never\""; | ||
return false; | ||
} | ||
static bool dummy = google::RegisterFlagValidator( | ||
&FLAGS_color, &ValidateColorFlag); | ||
|
||
|
||
namespace kudu { | ||
namespace tools { | ||
|
||
namespace { | ||
bool UseColor() { | ||
if (FLAGS_color == "never") return false; | ||
if (FLAGS_color == "always") return true; | ||
return isatty(STDOUT_FILENO); | ||
} | ||
|
||
const char* StringForCode(AnsiCode color) { | ||
if (!UseColor()) return ""; | ||
|
||
// Codes from: https://en.wikipedia.org/wiki/ANSI_escape_code | ||
switch (color) { | ||
case AnsiCode::RED: return "\x1b[31m"; | ||
case AnsiCode::GREEN: return "\x1b[32m"; | ||
case AnsiCode::YELLOW: return "\x1b[33m"; | ||
case AnsiCode::BLUE: return "\x1b[34m"; | ||
case AnsiCode::RESET: return "\x1b[m"; | ||
} | ||
LOG(FATAL); | ||
return ""; | ||
} | ||
} // anonymous namespace | ||
|
||
string Color(AnsiCode color, StringPiece s) { | ||
return strings::Substitute("$0$1$2", | ||
StringForCode(color), | ||
s, | ||
StringForCode(AnsiCode::RESET)); | ||
} | ||
|
||
} // namespace tools | ||
} // namespace kudu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "kudu/gutil/strings/stringpiece.h" | ||
|
||
namespace kudu { | ||
namespace tools { | ||
|
||
enum class AnsiCode { | ||
RED, | ||
YELLOW, | ||
GREEN, | ||
BLUE, | ||
RESET | ||
}; | ||
|
||
std::string Color(AnsiCode color, StringPiece s); | ||
|
||
} // namespace tools | ||
} // namespace kudu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.