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

Extend format status output - Allow to change also order of description in NINJA_STATUS #1045

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 20 additions & 8 deletions src/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,14 @@ void BuildStatus::BuildFinished() {
}

string BuildStatus::FormatProgressStatus(
const char* progress_status_format, EdgeStatus status) const {
const char* progress_status_format, EdgeStatus status, const string to_print) const {
string out;
char buf[32];
int percent;
bool has_desc = false;
char total_edges_str[32];
snprintf(total_edges_str, sizeof(total_edges_str), "%d", total_edges_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: total_edges_ can change over the course of a build, so this isn't guaranteed. The most common thing in fact is for the total edge count to be overestimated and then reduced in the case where a command doesn't modify its outputs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is not a problem. It is just used as an estimate for length alignment. It just provide better output for next print...

int total_edges_len = strlen(total_edges_str);
for (const char* s = progress_status_format; *s != '\0'; ++s) {
if (*s == '%') {
++s;
Expand All @@ -198,14 +202,13 @@ string BuildStatus::FormatProgressStatus(

// Started edges.
case 's':
snprintf(buf, sizeof(buf), "%d", started_edges_);
snprintf(buf, sizeof(buf), "%*d", total_edges_len, started_edges_);
out += buf;
break;

// Total edges.
case 't':
snprintf(buf, sizeof(buf), "%d", total_edges_);
out += buf;
out += total_edges_str;
break;

// Running edges.
Expand All @@ -214,20 +217,20 @@ string BuildStatus::FormatProgressStatus(
// count the edge that just finished as a running edge
if (status == kEdgeFinished)
running_edges++;
snprintf(buf, sizeof(buf), "%d", running_edges);
snprintf(buf, sizeof(buf), "%*d", total_edges_len, running_edges);
out += buf;
break;
}

// Unstarted edges.
case 'u':
snprintf(buf, sizeof(buf), "%d", total_edges_ - started_edges_);
snprintf(buf, sizeof(buf), "%*d", total_edges_len, total_edges_ - started_edges_);
out += buf;
break;

// Finished edges.
case 'f':
snprintf(buf, sizeof(buf), "%d", finished_edges_);
snprintf(buf, sizeof(buf), "%*d", total_edges_len, finished_edges_);
out += buf;
break;

Expand Down Expand Up @@ -259,6 +262,12 @@ string BuildStatus::FormatProgressStatus(
break;
}

// Description (to_print)
case 'd':
out += to_print;
has_desc = true;
break;

default:
Fatal("unknown placeholder '%%%c' in $NINJA_STATUS", *s);
return "";
Expand All @@ -268,6 +277,9 @@ string BuildStatus::FormatProgressStatus(
}
}

if (!has_desc)
out += to_print;

return out;
}

Expand All @@ -281,7 +293,7 @@ void BuildStatus::PrintStatus(Edge* edge, EdgeStatus status) {
if (to_print.empty() || force_full_command)
to_print = edge->GetBinding("command");

to_print = FormatProgressStatus(progress_status_format_, status) + to_print;
to_print = FormatProgressStatus(progress_status_format_, status, to_print);

printer_.Print(to_print,
force_full_command ? LinePrinter::FULL : LinePrinter::ELIDE);
Expand Down
2 changes: 1 addition & 1 deletion src/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ struct BuildStatus {
/// @param progress_status_format The format of the progress status.
/// @param status The status of the edge.
string FormatProgressStatus(const char* progress_status_format,
EdgeStatus status) const;
EdgeStatus status, const string to_print) const;

private:
void PrintStatus(Edge* edge, EdgeStatus status);
Expand Down
6 changes: 3 additions & 3 deletions src/build_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ TEST_F(BuildWithLogTest, RestatTest) {
EXPECT_TRUE(builder_.Build(&err));
ASSERT_EQ("", err);
EXPECT_EQ("[3/3]", builder_.status_->FormatProgressStatus("[%s/%t]",
BuildStatus::kEdgeStarted));
BuildStatus::kEdgeStarted, ""));
command_runner_.commands_ran_.clear();
state_.Reset();

Expand Down Expand Up @@ -1768,13 +1768,13 @@ TEST_F(BuildTest, StatusFormatElapsed) {
// Before any task is done, the elapsed time must be zero.
EXPECT_EQ("[%/e0.000]",
status_.FormatProgressStatus("[%%/e%e]",
BuildStatus::kEdgeStarted));
BuildStatus::kEdgeStarted, ""));
}

TEST_F(BuildTest, StatusFormatReplacePlaceholder) {
EXPECT_EQ("[%/s0/t0/r0/u0/f0]",
status_.FormatProgressStatus("[%%/s%s/t%t/r%r/u%u/f%f]",
BuildStatus::kEdgeStarted));
BuildStatus::kEdgeStarted, ""));
}

TEST_F(BuildTest, FailedDepsParse) {
Expand Down