Skip to content

Commit 2fd1bf2

Browse files
authored
fea/add color log (#13305)
1 parent a39eba7 commit 2fd1bf2

File tree

8 files changed

+112
-7
lines changed

8 files changed

+112
-7
lines changed

paddle/fluid/framework/ir/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function(pass_library TARGET DEST)
1919
endfunction()
2020

2121
cc_library(node SRCS node.cc DEPS proto_desc)
22-
cc_library(graph SRCS graph.cc DEPS node)
22+
cc_library(graph SRCS graph.cc DEPS node pretty_log)
2323
cc_library(graph_helper SRCS graph_helper.cc DEPS graph)
2424
cc_library(pass SRCS pass.cc DEPS graph node graph_helper)
2525
cc_library(graph_traits SRCS graph_traits.cc DEPS graph)

paddle/fluid/framework/ir/graph_pattern_detector.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
#include "paddle/fluid/framework/ir/graph_traits.h"
2222
#include "paddle/fluid/framework/ir/graph_viz_pass.h"
2323
#include "paddle/fluid/platform/enforce.h"
24+
#include "paddle/fluid/string/pretty_log.h"
2425
#include "paddle/fluid/string/printf.h"
2526

2627
namespace paddle {
2728
namespace framework {
2829
namespace ir {
2930

31+
using string::PrettyLogEndl;
32+
using string::PrettyLog;
33+
using string::Style;
34+
3035
size_t PDPattern::id_ = 0UL;
3136

3237
PDNode* PDPattern::NewNode(const std::string& name) {
@@ -83,7 +88,7 @@ void GraphPatternDetector::operator()(Graph* graph,
8388
ValidateByNodeRole(&subgraphs);
8489

8590
if (subgraphs.empty()) return;
86-
LOG(INFO) << "detect " << subgraphs.size() << " subgraph matches the pattern";
91+
PrettyLogEndl(Style::detail(), "--- detect %d subgraphs", subgraphs.size());
8792
int id = 0;
8893
for (auto& g : subgraphs) {
8994
VLOG(3) << "optimizing #" << id++ << " subgraph";

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cc_library(ir_pass_manager SRCS ir_pass_manager.cc DEPS graph pass)
22
set(analysis_deps
3-
framework_proto proto_desc ir_pass_manager graph pass paddle_fluid_api executor)
3+
framework_proto proto_desc ir_pass_manager graph pass paddle_fluid_api executor pretty_log)
44

55
cc_library(analysis SRCS pass_manager.cc node.cc data_flow_graph.cc graph_traits.cc subgraph_splitter.cc
66
analyzer.cc

paddle/fluid/inference/analysis/ir_pass_manager.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
1818
#include "paddle/fluid/framework/ir/graph.h"
1919
#include "paddle/fluid/framework/scope.h"
20+
#include "paddle/fluid/string/pretty_log.h"
2021

2122
namespace paddle {
2223
namespace inference {
2324
namespace analysis {
25+
using string::PrettyLogEndl;
26+
using string::PrettyLog;
27+
using string::Style;
2428

2529
IRPassManager::IRPassManager(const ProgramDesc &program,
2630
framework::Scope *scope)
@@ -34,7 +38,7 @@ void IRPassManager::Apply(const std::vector<std::string> &passes) {
3438
// Apply all the passes
3539
std::string pre_pass;
3640
for (const std::string &pass_name : passes) {
37-
LOG(WARNING) << "Running IR pass [" << pass_name << "]";
41+
PrettyLogEndl(Style::H2(), "--- Running IR pass [%s]", pass_name);
3842
auto pass = framework::ir::PassRegistry::Instance().Get(pass_name);
3943
if (pass_name == "graph_viz_pass") {
4044
std::string dot_file_path =

paddle/fluid/inference/analysis/pass_manager.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License. */
1414

1515
#include "paddle/fluid/inference/analysis/pass_manager.h"
1616
#include "paddle/fluid/inference/analysis/fluid_to_data_flow_graph_pass.h"
17+
#include "paddle/fluid/string/pretty_log.h"
1718

1819
namespace paddle {
1920
namespace inference {
@@ -22,7 +23,7 @@ namespace analysis {
2223
bool PassManager::Initialize(Argument* argument) {
2324
argument_ = argument;
2425
for (auto& pass : data_) {
25-
LOG(WARNING) << "Initializing pass [" << pass->repr() << "]";
26+
VLOG(3) << "Initializing pass [" << pass->repr() << "]";
2627
if (!pass->Initialize(argument)) {
2728
LOG(ERROR) << "Failed to initialize pass [" << pass->repr() << "]";
2829
return false;
@@ -33,9 +34,10 @@ bool PassManager::Initialize(Argument* argument) {
3334

3435
void DfgPassManager::RunAll() {
3536
PADDLE_ENFORCE(argument_);
36-
LOG(INFO) << "Total " << data_.size() << " Analysys passes";
37+
VLOG(3) << "Total " << data_.size() << " Analysys passes";
3738
for (auto& pass : data_) {
38-
LOG(WARNING) << "Running Analysis pass [" << pass->repr() << "]";
39+
string::PrettyLogEndl(string::Style::H1(), "* Running Analysis pass [%s]",
40+
pass->repr());
3941
pass->Run(argument_->main_dfg.get());
4042
}
4143
}

paddle/fluid/string/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cc_library(stringpiece SRCS piece.cc)
2+
cc_library(pretty_log SRCS pretty_log.cc)
3+
cc_test(test_pretty_log SRCS pretty_log.cc)
24
cc_test(stringpiece_test SRCS piece_test.cc DEPS stringpiece glog gflags)
35
cc_test(stringprintf_test SRCS printf_test.cc DEPS glog gflags)
46
cc_test(to_string_test SRCS to_string_test.cc)

paddle/fluid/string/pretty_log.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/string/pretty_log.h"
16+
#include <gflags/gflags.h>
17+
18+
DEFINE_bool(color, true, "Whether to turn on pretty log");
19+
20+
namespace paddle {
21+
namespace string {} // namespace string
22+
} // namespace paddle

paddle/fluid/string/pretty_log.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include <gflags/gflags.h>
17+
#include <iostream>
18+
#include <sstream>
19+
#include <string>
20+
#include <utility>
21+
#include "paddle/fluid/string/printf.h"
22+
23+
DECLARE_bool(color);
24+
25+
namespace paddle {
26+
27+
namespace string {
28+
29+
inline std::string black() { return FLAGS_color ? "\e[30m" : ""; }
30+
inline std::string red() { return FLAGS_color ? "\e[31m" : ""; }
31+
inline std::string b_red() { return FLAGS_color ? "\e[41m" : ""; }
32+
inline std::string green() { return FLAGS_color ? "\e[32m" : ""; }
33+
inline std::string yellow() { return FLAGS_color ? "\e[33m" : ""; }
34+
inline std::string blue() { return FLAGS_color ? "\e[34m" : ""; }
35+
inline std::string purple() { return FLAGS_color ? "\e[35m" : ""; }
36+
inline std::string cyan() { return FLAGS_color ? "\e[36m" : ""; }
37+
inline std::string light_gray() { return FLAGS_color ? "\e[37m" : ""; }
38+
inline std::string white() { return FLAGS_color ? "\e[37m" : ""; }
39+
inline std::string light_red() { return FLAGS_color ? "\e[91m" : ""; }
40+
inline std::string dim() { return FLAGS_color ? "\e[2m" : ""; }
41+
inline std::string bold() { return FLAGS_color ? "\e[1m" : ""; }
42+
inline std::string underline() { return FLAGS_color ? "\e[4m" : ""; }
43+
inline std::string blink() { return FLAGS_color ? "\e[5m" : ""; }
44+
inline std::string reset() { return FLAGS_color ? "\e[0m" : ""; }
45+
46+
using TextBlock = std::pair<std::string, std::string>;
47+
48+
struct Style {
49+
static std::string info() { return black(); }
50+
static std::string warn() { return b_red(); }
51+
static std::string suc() { return green(); }
52+
static std::string H1() { return bold() + purple(); }
53+
static std::string H2() { return green(); }
54+
static std::string H3() { return green(); }
55+
static std::string detail() { return light_gray(); }
56+
};
57+
58+
template <typename... Args>
59+
static void PrettyLogEndl(const std::string& style, const char* fmt,
60+
const Args&... args) {
61+
std::cerr << style << Sprintf(fmt, args...) << reset() << std::endl;
62+
}
63+
template <typename... Args>
64+
static void PrettyLog(const std::string& style, const char* fmt,
65+
const Args&... args) {
66+
std::cerr << style << Sprintf(fmt, args...) << reset();
67+
}
68+
69+
} // namespace string
70+
} // namespace paddle

0 commit comments

Comments
 (0)