Debian Codegraph @

src/atlas/runtime/AtlasTool.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include <algorithm>
12 #include <chrono>
13 #include <thread>
14
15 #include "eckit/config/LibEcKit.h"
16 #include "eckit/filesystem/LocalPathName.h"
17 #include "eckit/log/FileTarget.h"
18 #include "eckit/log/PrefixTarget.h"
19
20 #include "atlas/library/config.h"
21 #include "atlas/runtime/AtlasTool.h"
22
23 #include "eckit/system/LibraryManager.h"
24
25 namespace atlas {
26 static void usage(const std::string& name) {
27 Log::info() << "dummy usage" << std::endl;
28 }
29 } // namespace atlas
30
31 namespace {
32
33 int digits(int number) {
34 int d = 0;
35 while (number) {
36 number /= 10;
37 d++;
38 }
39 return d;
40 }
41
42 static std::string debug_prefix(const std::string& libname) {
43 std::string s = libname;
44 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
45 s += "_DEBUG";
46 return s;
47 }
48
49 void debug_addTarget(eckit::LogTarget* target) {
50 for (std::string libname : eckit::system::LibraryManager::list()) {
51 const eckit::system::Library& lib = eckit::system::LibraryManager::lookup(libname);
52 if (lib.debug()) {
53 lib.debugChannel().addTarget(new eckit::PrefixTarget(debug_prefix(libname), target));
54 }
55 }
56 if (eckit::Log::debug()) {
57 eckit::Log::debug().addTarget(target);
58 }
59 }
60
61 void debug_setTarget(eckit::LogTarget* target) {
62 for (std::string libname : eckit::system::LibraryManager::list()) {
63 const eckit::system::Library& lib = eckit::system::LibraryManager::lookup(libname);
64 if (lib.debug()) {
65 lib.debugChannel().setTarget(new eckit::PrefixTarget(debug_prefix(libname), target));
66 }
67 }
68 if (eckit::Log::debug()) {
69 eckit::Log::debug().setTarget(target);
70 }
71 }
72
73 void debug_reset() {
74 for (std::string libname : eckit::system::LibraryManager::list()) {
75 const eckit::system::Library& lib = eckit::system::LibraryManager::lookup(libname);
76 if (lib.debug()) {
77 lib.debugChannel().reset();
78 }
79 }
80 if (eckit::Log::debug()) {
81 eckit::Log::debug().reset();
82 }
83 }
84
85 bool getEnv(const std::string& env, bool default_value) {
86 const char* cenv = ::getenv(env.c_str());
87 if (cenv != nullptr) {
88 return eckit::Translator<std::string, bool>()(cenv);
89 }
90 return default_value;
91 }
92
93 int getEnv(const std::string& env, int default_value) {
94 const char* cenv = ::getenv(env.c_str());
95 if (cenv != nullptr) {
96 return eckit::Translator<std::string, int>()(cenv);
97 }
98 return default_value;
99 }
100
101 std::string getEnv(const std::string& env, const std::string& default_value) {
102 const char* cenv = ::getenv(env.c_str());
103 if (cenv != nullptr) {
104 return cenv;
105 }
106 return default_value;
107 }
108
109 void setEnv(const std::string& env, bool value) {
110 constexpr int DO_NOT_REPLACE_IF_EXISTS = 0;
111 ::setenv(env.c_str(), eckit::Translator<bool, std::string>()(value).c_str(), DO_NOT_REPLACE_IF_EXISTS);
112 }
113
114
115 } // namespace
116
117 namespace atlas {
118
119 static bool use_logfile;
120 static std::string logfile_name;
121 static std::string workdir;
122
123 [[noreturn]] void atlas_terminate() {
124 // This routine is called for uncaught exceptions.
125 // It can be set with std::set_terminate( &atlas_terminate )
126
127 Log::flush();
128
129 if (not use_logfile and mpi::size() > 1) {
130 eckit::LogTarget* logfile = new eckit::FileTarget(logfile_name);
131 Log::error().addTarget(logfile);
132 }
133 if (std::exception_ptr eptr = std::current_exception()) {
134 std::ostream& out = Log::error();
135 try {
136 std::rethrow_exception(eptr); // throw to recognise the type
137 }
138 catch (const eckit::Abort& exception) {
139 out << "\n"
140 << "=========================================\n"
141 << "[" << mpi::rank() << "] Aborting " << eckit::Main::instance().displayName() << "\n"
142 << "-----------------------------------------\n"
143 << exception.what() << "\n";
144 if (exception.location()) {
145 out << "-----------------------------------------\n"
146 << "LOCATION: " << exception.location() << "\n";
147 }
148 out << "-----------------------------------------\n"
149 << "BACKTRACE\n"
150 << "-----------------------------------------\n"
151 << exception.callStack() << "\n"
152 << "=========================================\n"
153 << std::endl;
154 }
155 catch (const eckit::Exception& exception) {
156 out << "\n"
157 << "=========================================\n"
158 << "[" << mpi::rank() << "] TERMINATING " << eckit::Main::instance().displayName() << "\n"
159 << "-----------------------------------------\n"
160 << exception.what() << "\n"
161 << "-----------------------------------------\n";
162
163 if (exception.location()) {
164 out << "LOCATION: " << exception.location() << "\n"
165 << "-----------------------------------------\n";
166 }
167
168 out << "BACKTRACE\n"
169 << "-----------------------------------------\n"
170 << exception.callStack() << "\n"
171 << "=========================================\n"
172 << std::endl;
173 }
174 catch (const std::exception& exception) {
175 out << "\n"
176 << "=========================================\n"
177 << "[" << mpi::rank() << "] TERMINATING " << eckit::Main::instance().displayName() << "\n"
178 << "-----------------------------------------\n"
179 << exception.what() << "\n"
180 << "-----------------------------------------\n"
181 << "BACKTRACE\n"
182 << "-----------------------------------------\n"
183 << backtrace() << "\n"
184 << "=========================================\n"
185 << std::endl;
186 }
187 catch (...) {
188 out << "\n"
189 << "=========================================\n"
190 << "[" << mpi::rank() << "] TERMINATING " << eckit::Main::instance().displayName() << "\n"
191 << "-----------------------------------------\n"
192 << "BACKTRACE\n"
193 << "-----------------------------------------\n"
194 << backtrace() << "\n"
195 << "=========================================" << std::endl;
196 }
197 }
198
199 eckit::LibEcKit::instance().abort();
200
201 // Just in case we end up here, as last resort, exit immediately without
202 // cleanup.
203 std::_Exit(EXIT_FAILURE);
204 }
205
206 } // namespace atlas
207
208
209 void atlas::AtlasTool::add_option(eckit::option::Option* option) {
210 options_.push_back(option);
211 }
212
213 void atlas::AtlasTool::help(std::ostream& out) {
214 auto indented = [&](const std::string& s) -> std::string {
215 std::string str = indent() + s;
216 size_t pos = 0;
217 while ((pos = str.find('\n', pos)) != std::string::npos) {
218 str.replace(pos, 1, '\n' + indent());
219 ++pos;
220 }
221 return str;
222 };
223
224
225 out << "NAME\n" << indented(name());
226 std::string brief = briefDescription();
227 if (brief.size()) {
228 out << " - " << brief << '\n';
229 }
230
231 std::string usg = usage();
232 if (usg.size()) {
233 out << '\n';
234 out << "SYNOPSIS\n" << indented(usg) << '\n';
235 }
236 std::string desc = longDescription();
237 if (desc.size()) {
238 out << '\n';
239 out << "DESCRIPTION\n" << indented(desc) << '\n';
240 }
241 out << '\n';
242 out << "OPTIONS\n";
243 for (Options::const_iterator it = options_.begin(); it != options_.end(); ++it) {
244 std::stringstream s;
245 s << **it;
246 out << indented(s.str()) << "\n\n";
247 }
248 out << std::flush;
249 }
250
251 bool atlas::AtlasTool::handle_help() {
252 for (int i = 1; i < argc(); ++i) {
253 if (argv(i) == "--help" || argv(i) == "-h") {
254 if (taskID() == 0) {
255 help(std::cout);
256 }
257 return true;
258 }
259 }
260 return false;
261 }
262
263 std::string atlas::AtlasTool::get_positional_arg(const Args& args, size_t p) const {
264 int c = 0;
265 for (int i = 0; i < args.count(); ++i) {
266 if (args(i)[0] == '-') {
267 i++;
268 }
269 else {
270 if (c == p) {
271 return args(i);
272 }
273 c++;
274 }
275 }
276 return std::string{};
277 }
278
279 atlas::AtlasTool::AtlasTool(int argc, char** argv): eckit::Tool(argc, argv) {
280 eckit::LibEcKit::instance().setAbortHandler([] {
281 std::cerr << "[" << atlas::mpi::rank() << "] "
282 << "calling MPI_Abort";
283 if (not use_logfile and mpi::size() > 1) {
284 std::cerr << ", logfile: " << logfile_name;
285 }
286 std::cerr << std::endl;
287 std::this_thread::sleep_for(std::chrono::milliseconds(3000));
288 atlas::mpi::comm().abort(1);
289 });
290 std::set_terminate(&atlas_terminate);
291 setEnv("ECKIT_EXCEPTION_IS_SILENT", true);
292 setEnv("ECKIT_ASSERT_FAILED_IS_SILENT", true);
293 setEnv("ATLAS_FPE", true);
294 setEnv("ATLAS_SIGNAL_HANDLER", true);
295 add_option(new SimpleOption<bool>("help", "Print this help"));
296 add_option(new SimpleOption<long>("debug", "Debug level"));
297 taskID(eckit::mpi::comm("world").rank());
298 workdir = getEnv("ATLAS_WORKDIR", eckit::PathName(eckit::LocalPathName::cwd()).fullName());
299
300 add_option(new SimpleOption<std::string>("display-name", "Name to give to program"));
301 }
302
303 int atlas::AtlasTool::start() {
304 try {
305 if (handle_help()) {
306 return success();
307 }
308
309 if (argc() - 1 < minimumPositionalArguments()) {
310 if (taskID() == 0) {
311 std::cout << "Usage: " << usage() << std::endl;
312 }
313 return failed();
314 }
315 atlas::initialize();
316 setupLogging();
317
318 Options opts = options_;
319 Args args(&atlas::usage, opts, numberOfPositionalArguments(), minimumPositionalArguments() > 0);
320
321 int err_code = execute(args);
322 atlas::finalize();
323 atlas::mpi::finalize();
324 return err_code;
325 }
326 catch (...) {
327 atlas_terminate();
328 }
329 }
330
331 void atlas::AtlasTool::run() {}
332
333 void atlas::AtlasTool::setupLogging() {
334 int log_rank = getEnv("ATLAS_LOG_RANK", 0);
335 use_logfile = getEnv("ATLAS_LOG_FILE", false);
336
337 int d = digits(mpi::size());
338 std::string rankstr = std::to_string(taskID());
339 for (int i = rankstr.size(); i < d; ++i) {
340 rankstr = "0" + rankstr;
341 }
342
343 logfile_name = workdir + "/" + displayName() + ".log.p" + rankstr;
344
345 if (use_logfile) {
346 eckit::LogTarget* logfile = new eckit::FileTarget(logfile_name);
347
348 if (int(mpi::rank()) == log_rank) {
349 if (Log::info()) {
350 Log::info().addTarget(logfile);
351 }
352 if (Log::warning()) {
353 Log::warning().addTarget(logfile);
354 }
355 if (Log::error()) {
356 Log::error().addTarget(logfile);
357 }
358 debug_addTarget(logfile);
359 }
360 else {
361 if (Log::info()) {
362 Log::info().setTarget(logfile);
363 }
364 if (Log::warning()) {
365 Log::warning().setTarget(logfile);
366 }
367 if (Log::error()) {
368 Log::error().setTarget(logfile);
369 }
370 debug_setTarget(logfile);
371 }
372 }
373 else {
374 if (int(mpi::rank()) != log_rank) {
375 if (Log::info()) {
376 Log::info().reset();
377 }
378 if (Log::warning()) {
379 Log::warning().reset();
380 }
381 if (Log::error()) {
382 Log::error().reset();
383 }
384 debug_reset();
385 }
386 }
387 // codechecker_false_positive [NewDeleteLeaks] Potential leak of memory pointed to by 'logfile'
388 }