Skip to content

Commit 09e5f11

Browse files
committed
Optimize WARNING_QUIT fatal-exit output
1 parent 71f3524 commit 09e5f11

5 files changed

Lines changed: 37 additions & 40 deletions

File tree

source/source_base/test/math_chebyshev_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ TEST_F(MathChebyshevTest, recurs)
385385
testing::internal::CaptureStdout();
386386
EXPECT_EXIT(ModuleBase::Chebyshev<double> noneche(0), ::testing::ExitedWithCode(1), "");
387387
std::string output = testing::internal::GetCapturedStdout();
388-
EXPECT_THAT(output, testing::HasSubstr("NOTICE"));
388+
EXPECT_THAT(output, testing::HasSubstr("ERROR"));
389389

390390
int norder = 100;
391391
p_chetest = new ModuleBase::Chebyshev<double>(norder);

source/source_base/test/tool_check_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ TEST_F(ToolCheckTest, Name)
6161
testing::internal::CaptureStdout();
6262
EXPECT_EXIT(ModuleBase::CHECK_NAME(ifs, "abacus"), ::testing::ExitedWithCode(1), "");
6363
output = testing::internal::GetCapturedStdout();
64-
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
64+
EXPECT_THAT(output,testing::HasSubstr("ERROR"));
6565
ifs.close();
6666
}
6767

@@ -82,7 +82,7 @@ TEST_F(ToolCheckTest, Int)
8282
testing::internal::CaptureStdout();
8383
EXPECT_EXIT(ModuleBase::CHECK_INT(ifs, 80), ::testing::ExitedWithCode(1), "");
8484
output = testing::internal::GetCapturedStdout();
85-
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
85+
EXPECT_THAT(output,testing::HasSubstr("ERROR"));
8686
ifs.close();
8787
}
8888

@@ -103,7 +103,7 @@ TEST_F(ToolCheckTest, Double)
103103
testing::internal::CaptureStdout();
104104
EXPECT_EXIT(ModuleBase::CHECK_DOUBLE(ifs, 0.22998), ::testing::ExitedWithCode(1), "");
105105
output = testing::internal::GetCapturedStdout();
106-
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
106+
EXPECT_THAT(output,testing::HasSubstr("ERROR"));
107107
ifs.close();
108108
}
109109

@@ -124,6 +124,6 @@ TEST_F(ToolCheckTest, String)
124124
testing::internal::CaptureStdout();
125125
EXPECT_EXIT(ModuleBase::CHECK_STRING(ifs, "scf"), ::testing::ExitedWithCode(1), "");
126126
output = testing::internal::GetCapturedStdout();
127-
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
127+
EXPECT_THAT(output,testing::HasSubstr("ERROR"));
128128
ifs.close();
129129
}

source/source_base/test/tool_quit_test.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ TEST_F(ToolQuitTest,warningquit)
7878
EXPECT_EXIT(ModuleBase::WARNING_QUIT("INPUT","bad input parameter"),
7979
::testing::ExitedWithCode(1), "");
8080
output = testing::internal::GetCapturedStdout();
81-
// test output on screening
82-
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
81+
// error exits should keep the error message visible and skip timer output
82+
EXPECT_THAT(output, testing::HasSubstr("ERROR"));
83+
EXPECT_THAT(output, testing::Not(testing::HasSubstr("TIME STATISTICS")));
8384
GlobalV::ofs_warning.close();
8485
GlobalV::ofs_running.close();
8586
ifs.open("warning.log");
@@ -101,8 +102,9 @@ TEST_F(ToolQuitTest,warningquit_with_ret)
101102
EXPECT_EXIT(ModuleBase::WARNING_QUIT("INPUT","bad input parameter",1),
102103
::testing::ExitedWithCode(1), "");
103104
output = testing::internal::GetCapturedStdout();
104-
// test output on screening
105-
EXPECT_THAT(output,testing::HasSubstr("TIME STATISTICS"));
105+
// error exits should keep the error message visible and skip timer output
106+
EXPECT_THAT(output, testing::HasSubstr("ERROR"));
107+
EXPECT_THAT(output, testing::Not(testing::HasSubstr("TIME STATISTICS")));
106108
GlobalV::ofs_warning.close();
107109
GlobalV::ofs_running.close();
108110
ifs.open("warning.log");

source/source_base/tool_quit.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "tool_quit.h"
2+
#include <cstdlib>
23
#ifdef __MPI
34
#include "mpi.h"
45
#endif
@@ -18,6 +19,22 @@ namespace
1819
{
1920
std::string g_quit_out_dir;
2021
std::string g_quit_calculation;
22+
23+
[[noreturn]] void quit_impl(const int ret, const bool print_timer)
24+
{
25+
#ifdef __NORMAL
26+
(void)print_timer;
27+
#else
28+
if (print_timer)
29+
{
30+
ModuleBase::timer::finish(GlobalV::ofs_running, !GlobalV::MY_RANK, false);
31+
std::cout << " See output information in : " << g_quit_out_dir << std::endl;
32+
}
33+
ModuleBase::Global_File::close_all_log(GlobalV::MY_RANK);
34+
#endif
35+
36+
exit(ret);
37+
}
2138
}
2239

2340
void set_quit_out_dir(const std::string& dir)
@@ -64,43 +81,21 @@ void QUIT()
6481

6582
void QUIT(int ret)
6683
{
67-
68-
#ifdef __NORMAL
69-
#else
70-
ModuleBase::timer::finish(GlobalV::ofs_running , !GlobalV::MY_RANK, false);
71-
ModuleBase::Global_File::close_all_log(GlobalV::MY_RANK);
72-
std::cout<<" See output information in : "<<g_quit_out_dir<<std::endl;
73-
#endif
74-
75-
exit(ret);
84+
quit_impl(ret, true);
7685
}
7786

7887

7988
void WARNING_QUIT(const std::string &file,const std::string &description)
8089
{
81-
WARNING_QUIT(file, description, 1);
82-
83-
#ifdef __MPI /* if it is MPI run, finalize first, then exit */
84-
std::cout << "Detecting if MPI has been initialized..." << std::endl;
85-
int is_initialized = 0;
86-
MPI_Initialized(&is_initialized);
87-
if (is_initialized) {
88-
std::cout << "Terminating ABACUS with multiprocessing environment." << std::endl;
89-
MPI_Finalize();
90-
}
91-
else{
92-
std::cout << "MPI has not been initialized. Quit normally." << std::endl;
93-
}
94-
/* but seems this is the only correct way to terminate the MPI */
95-
#endif
90+
WARNING_QUIT(file, description, 1);
9691
}
9792

9893
void WARNING_QUIT(const std::string &file,const std::string &description,int ret)
9994
{
10095
#ifdef __NORMAL
10196

10297
std::cout << " ---------------------------------------------------------" << std::endl;
103-
std::cout << " !NOTICE! " << std::endl;
98+
std::cout << " !ERROR! " << std::endl;
10499
std::cout << " ---------------------------------------------------------" << std::endl;
105100
std::cout << " For detailed manual of ABACUS, please see the website" << std::endl;
106101
std::cout << " https://abacus.deepmodeling.com" << std::endl;
@@ -110,7 +105,7 @@ void WARNING_QUIT(const std::string &file,const std::string &description,int ret
110105
#else
111106
std::cout << " " << std::endl;
112107
std::cout << " ---------------------------------------------------------" << std::endl;
113-
std::cout << " !NOTICE! " << std::endl;
108+
std::cout << " !ERROR! " << std::endl;
114109
std::cout << " ---------------------------------------------------------" << std::endl;
115110
std::cout << " " << std::endl;
116111
std::cout << " " << description << std::endl;
@@ -121,12 +116,12 @@ void WARNING_QUIT(const std::string &file,const std::string &description,int ret
121116
std::cout << " For any questions, propose issues on the website" << std::endl;
122117
std::cout << " https://github.com/deepmodeling/abacus-develop/issues" << std::endl;
123118
std::cout << " ---------------------------------------------------------" << std::endl;
124-
std::cout << " !NOTICE! " << std::endl;
119+
std::cout << " !ERROR! " << std::endl;
125120
std::cout << " ---------------------------------------------------------" << std::endl;
126121

127122

128123
GlobalV::ofs_running << " ---------------------------------------------------------" << std::endl;
129-
GlobalV::ofs_running << " !NOTICE! " << std::endl;
124+
GlobalV::ofs_running << " !ERROR! " << std::endl;
130125
GlobalV::ofs_running << " ---------------------------------------------------------" << std::endl;
131126
GlobalV::ofs_running << std::endl;
132127
GlobalV::ofs_running << " " << description << std::endl;
@@ -137,15 +132,15 @@ void WARNING_QUIT(const std::string &file,const std::string &description,int ret
137132
GlobalV::ofs_running << " For any questions, propose issues on the website" << std::endl;
138133
GlobalV::ofs_running << " https://github.com/deepmodeling/abacus-develop/issues" << std::endl;
139134
GlobalV::ofs_running << " ---------------------------------------------------------" << std::endl;
140-
GlobalV::ofs_running << " NOTICE " << std::endl;
135+
GlobalV::ofs_running << " !ERROR! " << std::endl;
141136
GlobalV::ofs_running << " ---------------------------------------------------------" << std::endl;
142137

143138
WARNING(file,description);
144139
GlobalV::ofs_running<<" Check in file : "<<g_quit_out_dir<<"warning.log"<<std::endl;
145140

146141
#endif
147142

148-
QUIT(ret);
143+
quit_impl(ret, false);
149144
}
150145

151146
//Check and print warning information for all cores.

source/source_basis/module_pw/test/test-other.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ TEST_F(PWTEST,test_other)
2929
testing::internal::CaptureStdout();
3030
EXPECT_EXIT(pwtest.setuptransform(), ::testing::ExitedWithCode(1), "");
3131
string output = testing::internal::GetCapturedStdout();
32-
EXPECT_THAT(output,testing::HasSubstr("NOTICE"));
32+
EXPECT_THAT(output,testing::HasSubstr("ERROR"));
3333

3434
int nks = 2;
3535
ModuleBase::Vector3<double> *kvec_d = new ModuleBase::Vector3<double>[nks];

0 commit comments

Comments
 (0)