• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #852

17 Dec 2022 04:43PM UTC coverage: 85.912% (-0.7%) from 86.568%
#852

push

StellarBot
Merge #6106

6106: Modernizing modules of levels 0 to 5 r=hkaiser a=hkaiser

- flyby: HPX_FORWARD/HPX_MOVE now expand to std::forward and std::move if those are implemented as builtin functions

working towards #5497

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

87 of 87 new or added lines in 24 files covered. (100.0%)

173152 of 201546 relevant lines covered (85.91%)

1910264.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.81
/libs/core/init_runtime_local/src/init_logging.cpp
1
//  Copyright (c) 2007-2021 Hartmut Kaiser
2
//  Copyright (c)      2011 Bryce Lelbach
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
#include <hpx/config.hpp>
9

10
#if defined(HPX_HAVE_LOGGING)
11
#include <hpx/init_runtime_local/detail/init_logging.hpp>
12
#include <hpx/runtime_configuration/runtime_configuration.hpp>
13
#include <hpx/runtime_local/get_locality_id.hpp>
14
#include <hpx/runtime_local/get_worker_thread_num.hpp>
15
#include <hpx/threading_base/thread_data.hpp>
16

17
#include <cstddef>
18
#include <cstdint>
19
#include <cstdlib>
20
#include <iostream>
21
#include <string>
22

23
#if defined(ANDROID) || defined(__ANDROID__)
24
#include <android/log.h>
25
#endif
26

27
///////////////////////////////////////////////////////////////////////////////
28
namespace hpx { namespace util {
29

30
    using logger_writer_type = logging::writer::named_write;
31

32
    ///////////////////////////////////////////////////////////////////////////
33
    // custom formatter: shepherd
34
    struct shepherd_thread_id : logging::formatter::manipulator
12,200✔
35
    {
36
        void operator()(std::ostream& to) const override
×
37
        {
38
            error_code ec(throwmode::lightweight);
×
39
            std::size_t thread_num = hpx::get_worker_thread_num(ec);
×
40

41
            if (std::size_t(-1) != thread_num)
×
42
            {
43
                util::format_to(to, "{:016x}", thread_num);
×
44
            }
×
45
            else
46
            {
47
                to << std::string(16, '-');
×
48
            }
49
        }
×
50
    };
51

52
    ///////////////////////////////////////////////////////////////////////////
53
    // custom formatter: locality prefix
54
    struct locality_prefix : logging::formatter::manipulator
12,200✔
55
    {
56
        void operator()(std::ostream& to) const override
57,568,842✔
57
        {
58
            std::uint32_t locality_id = hpx::get_locality_id();
57,478,662✔
59

60
            if (~static_cast<std::uint32_t>(0) != locality_id)
57,478,662✔
61
            {
62
                util::format_to(to, "{:08x}", locality_id);
56,817,202✔
63
            }
56,817,202✔
64
            else
65
            {
66
                // called from outside a HPX thread
67
                to << std::string(8, '-');
648,414✔
68
            }
69
        }
57,465,616✔
70
    };
71

72
    ///////////////////////////////////////////////////////////////////////////
73
    // custom formatter: HPX thread id
74
    struct thread_id : logging::formatter::manipulator
12,200✔
75
    {
76
        void operator()(std::ostream& to) const override
57,492,718✔
77
        {
78
            threads::thread_self* self = threads::get_self_ptr();
57,504,489✔
79
            if (nullptr != self)
57,504,489✔
80
            {
81
                // called from inside a HPX thread
82
                threads::thread_id_type id = threads::get_self_id();
13,783,410✔
83
                if (id != threads::invalid_thread_id)
13,783,410✔
84
                {
85
                    std::ptrdiff_t value =
13,782,995✔
86
                        reinterpret_cast<std::ptrdiff_t>(id.get());
13,782,995✔
87
                    util::format_to(to, "{:016x}", value);
13,782,995✔
88
                    return;
13,782,995✔
89
                }
90
            }
×
91

92
            // called from outside a HPX thread or invalid thread id
93
            to << std::string(16, '-');
43,719,346✔
94
        }
57,580,567✔
95
    };
96

97
    ///////////////////////////////////////////////////////////////////////////
98
    // custom formatter: HPX thread phase
99
    struct thread_phase : logging::formatter::manipulator
12,200✔
100
    {
101
        void operator()(std::ostream& to) const override
57,524,957✔
102
        {
103
            threads::thread_self* self = threads::get_self_ptr();
57,532,372✔
104
            if (nullptr != self)
57,532,372✔
105
            {
106
                // called from inside a HPX thread
107
                std::size_t phase = self->get_thread_phase();
13,783,540✔
108
                if (0 != phase)
13,783,540✔
109
                {
110
                    util::format_to(to, "{:04x}", self->get_thread_phase());
×
111
                    return;
×
112
                }
113
            }
13,783,554✔
114

115
            // called from outside a HPX thread or no phase given
116
            to << std::string(4, '-');
57,531,861✔
117
        }
57,555,799✔
118
    };
119

120
    ///////////////////////////////////////////////////////////////////////////
121
    // custom formatter: locality prefix of parent thread
122
    struct parent_thread_locality : logging::formatter::manipulator
12,200✔
123
    {
124
        void operator()(std::ostream& to) const override
57,529,508✔
125
        {
126
            std::uint32_t parent_locality_id =
57,525,728✔
127
                threads::get_parent_locality_id();
57,525,728✔
128
            if (~static_cast<std::uint32_t>(0) != parent_locality_id)
57,525,728✔
129
            {
130
                // called from inside a HPX thread
131
                util::format_to(to, "{:08x}", parent_locality_id);
×
132
            }
×
133
            else
134
            {
135
                // called from outside a HPX thread
136
                to << std::string(8, '-');
57,525,728✔
137
            }
138
        }
57,588,813✔
139
    };
140

141
    ///////////////////////////////////////////////////////////////////////////
142
    // custom formatter: HPX parent thread id
143
    struct parent_thread_id : logging::formatter::manipulator
12,200✔
144
    {
145
        void operator()(std::ostream& to) const override
57,531,839✔
146
        {
147
            threads::thread_id_type parent_id = threads::get_parent_id();
57,528,064✔
148
            if (nullptr != parent_id)
57,528,064✔
149
            {
150
                // called from inside a HPX thread
151
                std::ptrdiff_t value =
×
152
                    reinterpret_cast<std::ptrdiff_t>(parent_id.get());
×
153
                util::format_to(to, "{:016x}", value);
×
154
            }
×
155
            else
156
            {
157
                // called from outside a HPX thread
158
                to << std::string(16, '-');
57,528,064✔
159
            }
160
        }
57,579,829✔
161
    };
162

163
    ///////////////////////////////////////////////////////////////////////////
164
    // custom formatter: HPX parent thread phase
165
    struct parent_thread_phase : logging::formatter::manipulator
12,200✔
166
    {
167
        void operator()(std::ostream& to) const override
57,551,707✔
168
        {
169
            std::size_t parent_phase = threads::get_parent_phase();
57,547,412✔
170
            if (0 != parent_phase)
57,547,412✔
171
            {
172
                // called from inside a HPX thread
173
                util::format_to(to, "{:04x}", parent_phase);
×
174
            }
×
175
            else
176
            {
177
                // called from outside a HPX thread
178
                to << std::string(4, '-');
57,547,412✔
179
            }
180
        }
57,592,286✔
181
    };
182

183
#if defined(ANDROID) || defined(__ANDROID__)
184
    // default log destination for Android
185
    struct android_log : logging::destination::manipulator
186
    {
187
        android_log(char const* tag_)
188
          : tag(tag_)
189
        {
190
        }
191

192
        void operator()(logging::message const& msg) override
193
        {
194
            __android_log_write(
195
                ANDROID_LOG_DEBUG, tag.c_str(), msg.full_string().c_str());
196
        }
197

198
        bool operator==(android_log const& rhs) const
199
        {
200
            return tag == rhs.tag;
201
        }
202

203
        std::string tag;
204
    };
205
#endif
206

207
    ///////////////////////////////////////////////////////////////////////////
208
    struct dummy_thread_component_id : logging::formatter::manipulator
6,220✔
209
    {
210
        void operator()(std::ostream& to) const override
25,875,255✔
211
        {
212
            to << std::string(16, '-');
25,875,255✔
213
        }
25,900,276✔
214
    };
215

216
    ///////////////////////////////////////////////////////////////////////////
217
    // custom log destination: send generated strings to console
218
    void console_local::operator()(logging::message const& msg)
×
219
    {
220
        switch (dest_)
×
221
        {
×
222
        default:
223
        case logging_destination::hpx:
224
            LHPX_CONSOLE_(level_) << msg;
×
225
            break;
×
226

227
        case logging_destination::timing:
228
            LTIM_CONSOLE_(level_) << msg;
×
229
            break;
×
230

231
        case logging_destination::agas:
232
            LAGAS_CONSOLE_(level_) << msg;
×
233
            break;
×
234

235
        case logging_destination::parcel:
236
            LPT_CONSOLE_(level_) << msg;
×
237
            break;
×
238

239
        case logging_destination::app:
240
            LAPP_CONSOLE_(level_) << msg;
×
241
            break;
×
242

243
        case logging_destination::debuglog:
244
            LDEB_CONSOLE_ << msg;
×
245
            break;
×
246
        }
247
    }
×
248

249
    namespace detail {
250

251
        // unescape config entry
252
        std::string unescape(std::string const& value)
14,640✔
253
        {
254
            std::string result;
14,640✔
255
            std::string::size_type pos = 0;
14,640✔
256
            std::string::size_type pos1 = value.find_first_of('\\', 0);
14,640✔
257
            if (std::string::npos != pos1)
14,640✔
258
            {
259
                do
7,320✔
260
                {
261
                    switch (value[pos1 + 1])
7,320✔
262
                    {
263
                    case '\\':
264
                    case '\"':
265
                    case '?':
266
                        result = result + value.substr(pos, pos1 - pos);
×
267
                        pos1 = value.find_first_of('\\', (pos = pos1 + 1) + 1);
×
268
                        break;
×
269

270
                    case 'n':
271
                        result = result + value.substr(pos, pos1 - pos) + "\n";
7,320✔
272
                        pos1 = value.find_first_of('\\', pos = pos1 + 1);
7,320✔
273
                        ++pos;
7,320✔
274
                        break;
7,320✔
275

276
                    default:
277
                        result = result + value.substr(pos, pos1 - pos + 1);
×
278
                        pos1 = value.find_first_of('\\', pos = pos1 + 1);
×
279
                    }
×
280

281
                } while (pos1 != std::string::npos);
7,320✔
282
                result = result + value.substr(pos);
7,320✔
283
            }
7,320✔
284
            else
285
            {
286
                // the string doesn't contain any escaped character sequences
287
                result = value;
7,320✔
288
            }
289
            return result;
14,640✔
290
        }
14,640✔
291

292
        void define_common_formatters(logger_writer_type& writer)
2,440✔
293
        {
294
            writer.set_formatter("osthread", shepherd_thread_id());
2,440✔
295
            writer.set_formatter("locality", locality_prefix());
2,440✔
296
            writer.set_formatter("hpxthread", thread_id());
2,440✔
297
            writer.set_formatter("hpxphase", thread_phase());
2,440✔
298
            writer.set_formatter("hpxparent", parent_thread_id());
2,440✔
299
            writer.set_formatter("hpxparentphase", parent_thread_phase());
2,440✔
300
            writer.set_formatter("parentloc", parent_thread_locality());
2,440✔
301
        }
2,440✔
302

303
        void define_formatters_local(logger_writer_type& writer)
1,244✔
304
        {
305
            define_common_formatters(writer);
1,244✔
306
            writer.set_formatter("hpxcomponent", dummy_thread_component_id());
1,244✔
307
        }
1,244✔
308

309
        ///////////////////////////////////////////////////////////////////////
310
        static std::string empty_string;
1,250✔
311

312
        log_settings get_log_settings(util::section const& ini, char const* sec)
14,640✔
313
        {
314
            log_settings result;
14,640✔
315
            if (ini.has_section(sec))
14,640✔
316
            {
317
                util::section const* logini = ini.get_section(sec);
14,640✔
318
                HPX_ASSERT(nullptr != logini);
14,640✔
319

320
                result.level_ = logini->get_entry("level", empty_string);
14,640✔
321
                if (!result.level_.empty())
14,640✔
322
                {
323
                    result.dest_ =
14,640✔
324
                        logini->get_entry("destination", empty_string);
14,640✔
325
                    result.format_ = detail::unescape(
14,640✔
326
                        logini->get_entry("format", empty_string));
14,640✔
327
                }
14,640✔
328
            }
14,640✔
329
            return result;
14,640✔
330
        }
14,640✔
331

332
        ///////////////////////////////////////////////////////////////////////
333
        void get_console_local(logger_writer_type& writer, char const* name,
1,244✔
334
            logging::level lvl, logging_destination dest)
335
        {
336
            writer.set_destination(name, console_local(lvl, dest));
1,244✔
337
        }
1,244✔
338

339
        ///////////////////////////////////////////////////////////////////////
340
        // initialize logging for AGAS
341
        void init_agas_log(logging::level lvl, std::string logdest,
1,220✔
342
            std::string logformat, bool isconsole,
343
            void (*set_console_dest)(logger_writer_type&, char const*,
344
                logging::level, logging_destination),
345
            void (*define_formatters)(logging::writer::named_write&))
346
        {
347
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
348
            {
349
                logger_writer_type& writer = agas_logger()->writer();
×
350

351
#if defined(ANDROID) || defined(__ANDROID__)
352
                if (logdest.empty())    // ensure minimal defaults
353
                    logdest = isconsole ? "android_log" : "console";
354
                agas_logger()->writer().set_destination(
355
                    "android_log", android_log("hpx.agas"));
356
#else
357
                if (logdest.empty())    // ensure minimal defaults
×
358
                    logdest = isconsole ? "cerr" : "console";
×
359
#endif
360
                if (logformat.empty())
×
361
                    logformat = "|\\n";
×
362

363
                set_console_dest(writer, "console", lvl,
×
364
                    logging_destination::agas);    //-V106
365
                writer.write(logformat, logdest);
×
366
                define_formatters(writer);
×
367

368
                agas_logger()->mark_as_initialized();
×
369
            }
×
370
            agas_logger()->set_enabled(lvl);
1,220✔
371
        }
1,220✔
372

373
        void init_agas_log(runtime_configuration& ini, bool isconsole,
1,220✔
374
            void (*set_console_dest)(logger_writer_type&, char const*,
375
                logging::level, logging_destination),
376
            void (*define_formatters)(logging::writer::named_write&))
377
        {
378
            auto settings = detail::get_log_settings(ini, "hpx.logging.agas");
1,220✔
379

380
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
381
            if (!settings.level_.empty())
1,220✔
382
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
383

384
            init_agas_log(lvl, HPX_MOVE(settings.dest_),
2,440✔
385
                HPX_MOVE(settings.format_), isconsole, set_console_dest,
1,220✔
386
                define_formatters);
1,220✔
387
        }
1,220✔
388

389
        ///////////////////////////////////////////////////////////////////////
390
        // initialize logging for the parcel transport
391
        void init_parcel_log(logging::level lvl, std::string logdest,
1,220✔
392
            std::string logformat, bool isconsole,
393
            void (*set_console_dest)(logger_writer_type&, char const*,
394
                logging::level, logging_destination),
395
            void (*define_formatters)(logging::writer::named_write&))
396
        {
397
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
398
            {
399
                logger_writer_type& writer = parcel_logger()->writer();
×
400

401
#if defined(ANDROID) || defined(__ANDROID__)
402
                if (logdest.empty())    // ensure minimal defaults
403
                    logdest = isconsole ? "android_log" : "console";
404
                parcel_logger()->writer().set_destination(
405
                    "android_log", android_log("hpx.parcel"));
406
#else
407
                if (logdest.empty())    // ensure minimal defaults
×
408
                    logdest = isconsole ? "cerr" : "console";
×
409
#endif
410
                if (logformat.empty())
×
411
                    logformat = "|\\n";
×
412

413
                set_console_dest(writer, "console", lvl,
×
414
                    logging_destination::parcel);    //-V106
415
                writer.write(logformat, logdest);
×
416
                define_formatters(writer);
×
417

418
                parcel_logger()->mark_as_initialized();
×
419
            }
×
420
            parcel_logger()->set_enabled(lvl);
1,220✔
421
        }
1,220✔
422

423
        void init_parcel_log(runtime_configuration& ini, bool isconsole,
1,220✔
424
            void (*set_console_dest)(logger_writer_type&, char const*,
425
                logging::level, logging_destination),
426
            void (*define_formatters)(logging::writer::named_write&))
427
        {
428
            auto settings = detail::get_log_settings(ini, "hpx.logging.parcel");
1,220✔
429

430
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
431
            if (!settings.level_.empty())
1,220✔
432
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
433

434
            init_parcel_log(lvl, HPX_MOVE(settings.dest_),
2,440✔
435
                HPX_MOVE(settings.format_), isconsole, set_console_dest,
1,220✔
436
                define_formatters);
1,220✔
437
        }
1,220✔
438

439
        ///////////////////////////////////////////////////////////////////////
440
        // initialize logging for performance measurements
441
        void init_timing_log(logging::level lvl, std::string logdest,
1,220✔
442
            std::string logformat, bool isconsole,
443
            void (*set_console_dest)(logger_writer_type&, char const*,
444
                logging::level, logging_destination),
445
            void (*define_formatters)(logging::writer::named_write&))
446
        {
447
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
448
            {
449
                logger_writer_type& writer = timing_logger()->writer();
×
450

451
#if defined(ANDROID) || defined(__ANDROID__)
452
                if (logdest.empty())    // ensure minimal defaults
453
                    logdest = isconsole ? "android_log" : "console";
454

455
                writer.set_destination(
456
                    "android_log", android_log("hpx.timing"));
457
#else
458
                if (logdest.empty())    // ensure minimal defaults
×
459
                    logdest = isconsole ? "cerr" : "console";
×
460
#endif
461
                if (logformat.empty())
×
462
                    logformat = "|\\n";
×
463

464
                set_console_dest(writer, "console", lvl,
×
465
                    logging_destination::timing);    //-V106
466
                writer.write(logformat, logdest);
×
467
                define_formatters(writer);
×
468

469
                timing_logger()->mark_as_initialized();
×
470
            }
×
471
            timing_logger()->set_enabled(lvl);
1,220✔
472
        }
1,220✔
473

474
        void init_timing_log(runtime_configuration& ini, bool isconsole,
1,220✔
475
            void (*set_console_dest)(logger_writer_type&, char const*,
476
                logging::level, logging_destination),
477
            void (*define_formatters)(logging::writer::named_write&))
478
        {
479
            auto settings = detail::get_log_settings(ini, "hpx.logging.timing");
1,220✔
480

481
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
482
            if (!settings.level_.empty())
1,220✔
483
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
484

485
            init_timing_log(lvl, HPX_MOVE(settings.dest_),
2,440✔
486
                HPX_MOVE(settings.format_), isconsole, set_console_dest,
1,220✔
487
                define_formatters);
1,220✔
488
        }
1,220✔
489

490
        ///////////////////////////////////////////////////////////////////////
491
        void init_hpx_log(logging::level lvl, std::string logdest,
1,220✔
492
            std::string logformat, bool isconsole,
493
            void (*set_console_dest)(logger_writer_type&, char const*,
494
                logging::level, logging_destination),
495
            void (*define_formatters)(logging::writer::named_write&))
496
        {
497
            logger_writer_type& writer = hpx_logger()->writer();
1,220✔
498
            logger_writer_type& error_writer = hpx_error_logger()->writer();
1,220✔
499

500
#if defined(ANDROID) || defined(__ANDROID__)
501
            if (logdest.empty())    // ensure minimal defaults
502
                logdest = isconsole ? "android_log" : "console";
503

504
            writer.set_destination("android_log", android_log("hpx"));
505
            error_writer.set_destination("android_log", android_log("hpx"));
506
#else
507
            if (logdest.empty())    // ensure minimal defaults
1,220✔
508
                logdest = isconsole ? "cerr" : "console";
×
509
#endif
510
            if (logformat.empty())
1,220✔
511
                logformat = "|\\n";
×
512

513
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
514
            {
515
                set_console_dest(writer, "console", lvl,
1,220✔
516
                    logging_destination::hpx);    //-V106
517
                writer.write(logformat, logdest);
1,220✔
518
                define_formatters(writer);
1,220✔
519

520
                hpx_logger()->mark_as_initialized();
1,220✔
521
                hpx_logger()->set_enabled(lvl);
1,220✔
522

523
                // errors are logged to the given destination and to cerr
524
                set_console_dest(error_writer, "console", lvl,
1,220✔
525
                    logging_destination::hpx);    //-V106
526
#if !defined(ANDROID) && !defined(__ANDROID__)
527
                if (logdest != "cerr")
1,220✔
528
                    error_writer.write(logformat, logdest + " cerr");
1,220✔
529
#endif
530
                define_formatters(error_writer);
1,220✔
531

532
                hpx_error_logger()->mark_as_initialized();
1,220✔
533
                hpx_error_logger()->set_enabled(lvl);
1,220✔
534
            }
1,220✔
535
            else
536
            {
537
                // errors are always logged to cerr
538
                if (!isconsole)
×
539
                {
540
                    set_console_dest(writer, "console", lvl,
×
541
                        logging_destination::hpx);    //-V106
542
                    error_writer.write(logformat, "console");
×
543
                }
×
544
                else
545
                {
546
#if defined(ANDROID) || defined(__ANDROID__)
547
                    error_writer.write(logformat, "android_log");
548
#else
549
                    error_writer.write(logformat, "cerr");
×
550
#endif
551
                }
552
                define_formatters(error_writer);
×
553

554
                hpx_error_logger()->mark_as_initialized();
×
555
                hpx_error_logger()->set_enabled(
×
556
                    hpx::util::logging::level::fatal);
557
            }
558
        }
1,220✔
559

560
        void init_hpx_log(runtime_configuration& ini, bool isconsole,
1,220✔
561
            void (*set_console_dest)(logger_writer_type&, char const*,
562
                logging::level, logging_destination),
563
            void (*define_formatters)(logging::writer::named_write&))
564
        {
565
            auto settings = detail::get_log_settings(ini, "hpx.logging");
1,220✔
566

567
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
568
            if (!settings.level_.empty())
1,220✔
569
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
570

571
            init_hpx_log(lvl, HPX_MOVE(settings.dest_),
2,440✔
572
                HPX_MOVE(settings.format_), isconsole, set_console_dest,
1,220✔
573
                define_formatters);
1,220✔
574
        }
1,220✔
575

576
        ///////////////////////////////////////////////////////////////////////
577
        // initialize logging for application
578
        void init_app_log(logging::level lvl, std::string logdest,
1,220✔
579
            std::string logformat, bool isconsole,
580
            void (*set_console_dest)(logger_writer_type&, char const*,
581
                logging::level, logging_destination),
582
            void (*define_formatters)(logging::writer::named_write&))
583
        {
584
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
585
            {
586
                logger_writer_type& writer = app_logger()->writer();
×
587

588
#if defined(ANDROID) || defined(__ANDROID__)
589
                if (logdest.empty())    // ensure minimal defaults
590
                    logdest = isconsole ? "android_log" : "console";
591
                writer.set_destination(
592
                    "android_log", android_log("hpx.application"));
593
#else
594
                if (logdest.empty())    // ensure minimal defaults
×
595
                    logdest = isconsole ? "cerr" : "console";
×
596
#endif
597
                if (logformat.empty())
×
598
                    logformat = "|\\n";
×
599

600
                set_console_dest(writer, "console", lvl,
×
601
                    logging_destination::app);    //-V106
602
                writer.write(logformat, logdest);
×
603
                define_formatters(writer);
×
604

605
                app_logger()->mark_as_initialized();
×
606
            }
×
607
            app_logger()->set_enabled(lvl);
1,220✔
608
        }
1,220✔
609

610
        void init_app_log(runtime_configuration& ini, bool isconsole,
1,220✔
611
            void (*set_console_dest)(logger_writer_type&, char const*,
612
                logging::level, logging_destination),
613
            void (*define_formatters)(logging::writer::named_write&))
614
        {
615
            auto settings =
616
                detail::get_log_settings(ini, "hpx.logging.application");
1,220✔
617

618
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
619
            if (!settings.level_.empty())
1,220✔
620
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
621

622
            init_app_log(lvl, HPX_MOVE(settings.dest_),
2,440✔
623
                HPX_MOVE(settings.format_), isconsole, set_console_dest,
1,220✔
624
                define_formatters);
1,220✔
625
        }
1,220✔
626

627
        ///////////////////////////////////////////////////////////////////////
628
        // initialize logging for application
629
        void init_debuglog_log(logging::level lvl, std::string logdest,
1,220✔
630
            std::string logformat, bool isconsole,
631
            void (*set_console_dest)(logger_writer_type&, char const*,
632
                logging::level, logging_destination),
633
            void (*define_formatters)(logging::writer::named_write&))
634
        {
635
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
636
            {
637
                logger_writer_type& writer = debuglog_logger()->writer();
×
638

639
#if defined(ANDROID) || defined(__ANDROID__)
640
                if (logdest.empty())    // ensure minimal defaults
641
                    logdest = isconsole ? "android_log" : "console";
642
                writer.set_destination(
643
                    "android_log", android_log("hpx.debuglog"));
644
#else
645
                if (logdest.empty())    // ensure minimal defaults
×
646
                    logdest = isconsole ? "cerr" : "console";
×
647
#endif
648
                if (logformat.empty())
×
649
                    logformat = "|\\n";
×
650

651
                set_console_dest(writer, "console", lvl,
×
652
                    logging_destination::debuglog);    //-V106
653
                writer.write(logformat, logdest);
×
654
                define_formatters(writer);
×
655

656
                debuglog_logger()->mark_as_initialized();
×
657
            }
×
658
            debuglog_logger()->set_enabled(lvl);
1,220✔
659
        }
1,220✔
660

661
        void init_debuglog_log(runtime_configuration& ini, bool isconsole,
1,220✔
662
            void (*set_console_dest)(logger_writer_type&, char const*,
663
                logging::level, logging_destination),
664
            void (*define_formatters)(logging::writer::named_write&))
665
        {
666
            auto settings =
667
                detail::get_log_settings(ini, "hpx.logging.debuglog");
1,220✔
668

669
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
670
            if (!settings.level_.empty())
1,220✔
671
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
672

673
            init_debuglog_log(lvl, HPX_MOVE(settings.dest_),
2,440✔
674
                HPX_MOVE(settings.format_), isconsole, set_console_dest,
1,220✔
675
                define_formatters);
1,220✔
676
        }
1,220✔
677

678
        ///////////////////////////////////////////////////////////////////////
679
        void init_agas_console_log(
1,220✔
680
            logging::level lvl, std::string logdest, std::string logformat)
681
        {
682
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
683
            {
684
                logger_writer_type& writer = agas_console_logger()->writer();
×
685

686
#if defined(ANDROID) || defined(__ANDROID__)
687
                if (logdest.empty())    // ensure minimal defaults
688
                    logdest = "android_log";
689
                writer.set_destination("android_log", android_log("hpx.agas"));
690
#else
691
                if (logdest.empty())    // ensure minimal defaults
×
692
                    logdest = "cerr";
×
693
#endif
694
                if (logformat.empty())
×
695
                    logformat = "|\\n";
×
696

697
                writer.write(logformat, logdest);
×
698

699
                agas_console_logger()->mark_as_initialized();
×
700
            }
×
701
            agas_console_logger()->set_enabled(lvl);
1,220✔
702
        }
1,220✔
703

704
        void init_agas_console_log(util::section const& ini)
1,220✔
705
        {
706
            auto settings =
707
                detail::get_log_settings(ini, "hpx.logging.console.agas");
1,220✔
708

709
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
710
            if (!settings.level_.empty())
1,220✔
711
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
712

713
            init_agas_console_log(
1,220✔
714
                lvl, HPX_MOVE(settings.dest_), HPX_MOVE(settings.format_));
1,220✔
715
        }
1,220✔
716

717
        ///////////////////////////////////////////////////////////////////////
718
        void init_parcel_console_log(
1,220✔
719
            logging::level lvl, std::string logdest, std::string logformat)
720
        {
721
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
722
            {
723
                logger_writer_type& writer = parcel_console_logger()->writer();
×
724

725
#if defined(ANDROID) || defined(__ANDROID__)
726
                if (logdest.empty())    // ensure minimal defaults
727
                    logdest = "android_log";
728
                writer.set_destination(
729
                    "android_log", android_log("hpx.parcel"));
730
#else
731
                if (logdest.empty())    // ensure minimal defaults
×
732
                    logdest = "cerr";
×
733
#endif
734
                if (logformat.empty())
×
735
                    logformat = "|\\n";
×
736

737
                writer.write(logformat, logdest);
×
738

739
                parcel_console_logger()->mark_as_initialized();
×
740
            }
×
741
            parcel_console_logger()->set_enabled(lvl);
1,220✔
742
        }
1,220✔
743

744
        void init_parcel_console_log(util::section const& ini)
1,220✔
745
        {
746
            auto settings =
747
                detail::get_log_settings(ini, "hpx.logging.console.parcel");
1,220✔
748

749
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
750
            if (!settings.level_.empty())
1,220✔
751
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
752

753
            init_parcel_console_log(
1,220✔
754
                lvl, HPX_MOVE(settings.dest_), HPX_MOVE(settings.format_));
1,220✔
755
        }
1,220✔
756

757
        ///////////////////////////////////////////////////////////////////////
758
        void init_timing_console_log(
1,220✔
759
            logging::level lvl, std::string logdest, std::string logformat)
760
        {
761
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
762
            {
763
                logger_writer_type& writer = timing_console_logger()->writer();
×
764

765
#if defined(ANDROID) || defined(__ANDROID__)
766
                if (logdest.empty())    // ensure minimal defaults
767
                    logdest = "android_log";
768
                writer.set_destination(
769
                    "android_log", android_log("hpx.timing"));
770
#else
771
                if (logdest.empty())    // ensure minimal defaults
×
772
                    logdest = "cerr";
×
773
#endif
774
                if (logformat.empty())
×
775
                    logformat = "|\\n";
×
776

777
                writer.write(logformat, logdest);
×
778

779
                timing_console_logger()->mark_as_initialized();
×
780
            }
×
781
            timing_console_logger()->set_enabled(lvl);
1,220✔
782
        }
1,220✔
783

784
        void init_timing_console_log(util::section const& ini)
1,220✔
785
        {
786
            auto settings =
787
                detail::get_log_settings(ini, "hpx.logging.console.timing");
1,220✔
788

789
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
790
            if (!settings.level_.empty())
1,220✔
791
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
792

793
            init_timing_console_log(
1,220✔
794
                lvl, HPX_MOVE(settings.dest_), HPX_MOVE(settings.format_));
1,220✔
795
        }
1,220✔
796

797
        ///////////////////////////////////////////////////////////////////////
798
        void init_hpx_console_log(
1,220✔
799
            logging::level lvl, std::string logdest, std::string logformat)
800
        {
801
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
802
            {
803
                logger_writer_type& writer = hpx_console_logger()->writer();
1,220✔
804

805
#if defined(ANDROID) || defined(__ANDROID__)
806
                if (logdest.empty())    // ensure minimal defaults
807
                    logdest = "android_log";
808
                writer.set_destination("android_log", android_log("hpx"));
809
#else
810
                if (logdest.empty())    // ensure minimal defaults
1,220✔
811
                    logdest = "cerr";
×
812
#endif
813
                if (logformat.empty())
1,220✔
814
                    logformat = "|\\n";
×
815

816
                writer.write(logformat, logdest);
1,220✔
817

818
                hpx_console_logger()->mark_as_initialized();
1,220✔
819
            }
1,220✔
820
            hpx_console_logger()->set_enabled(lvl);
1,220✔
821
        }
1,220✔
822

823
        void init_hpx_console_log(util::section const& ini)
1,220✔
824
        {
825
            auto settings =
826
                detail::get_log_settings(ini, "hpx.logging.console");
1,220✔
827

828
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
829
            if (!settings.level_.empty())
1,220✔
830
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
831

832
            init_hpx_console_log(
1,220✔
833
                lvl, HPX_MOVE(settings.dest_), HPX_MOVE(settings.format_));
1,220✔
834
        }
1,220✔
835

836
        ///////////////////////////////////////////////////////////////////////
837
        void init_app_console_log(
1,220✔
838
            logging::level lvl, std::string logdest, std::string logformat)
839
        {
840
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
841
            {
842
                logger_writer_type& writer = app_console_logger()->writer();
×
843

844
#if defined(ANDROID) || defined(__ANDROID__)
845
                if (logdest.empty())    // ensure minimal defaults
846
                    logdest = "android_log";
847
                writer.set_destination(
848
                    "android_log", android_log("hpx.application"));
849
#else
850
                if (logdest.empty())    // ensure minimal defaults
×
851
                    logdest = "cerr";
×
852
#endif
853
                if (logformat.empty())
×
854
                    logformat = "|\\n";
×
855

856
                writer.write(logformat, logdest);
×
857

858
                app_console_logger()->mark_as_initialized();
×
859
            }
×
860
            app_console_logger()->set_enabled(lvl);
1,220✔
861
        }
1,220✔
862

863
        void init_app_console_log(util::section const& ini)
1,220✔
864
        {
865
            auto settings = detail::get_log_settings(
1,220✔
866
                ini, "hpx.logging.console.application");
1,220✔
867

868
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
869
            if (!settings.level_.empty())
1,220✔
870
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
871

872
            init_app_console_log(
1,220✔
873
                lvl, HPX_MOVE(settings.dest_), HPX_MOVE(settings.format_));
1,220✔
874
        }
1,220✔
875

876
        ///////////////////////////////////////////////////////////////////////
877
        void init_debuglog_console_log(
1,220✔
878
            logging::level lvl, std::string logdest, std::string logformat)
879
        {
880
            if (hpx::util::logging::level::disable_all != lvl)
1,220✔
881
            {
882
                logger_writer_type& writer =
×
883
                    debuglog_console_logger()->writer();
×
884

885
#if defined(ANDROID) || defined(__ANDROID__)
886
                if (logdest.empty())    // ensure minimal defaults
887
                    logdest = "android_log";
888
                writer.set_destination(
889
                    "android_log", android_log("hpx.debuglog"));
890
#else
891
                if (logdest.empty())    // ensure minimal defaults
×
892
                    logdest = "cerr";
×
893
#endif
894
                if (logformat.empty())
×
895
                    logformat = "|\\n";
×
896

897
                writer.write(logformat, logdest);
×
898

899
                debuglog_console_logger()->mark_as_initialized();
×
900
            }
×
901
            debuglog_console_logger()->set_enabled(lvl);
1,220✔
902
        }
1,220✔
903

904
        void init_debuglog_console_log(util::section const& ini)
1,220✔
905
        {
906
            auto settings =
907
                detail::get_log_settings(ini, "hpx.logging.console.debuglog");
1,220✔
908

909
            auto lvl = hpx::util::logging::level::disable_all;
1,220✔
910
            if (!settings.level_.empty())
1,220✔
911
                lvl = detail::get_log_level(settings.level_, true);
1,220✔
912

913
            init_debuglog_console_log(
1,220✔
914
                lvl, HPX_MOVE(settings.dest_), HPX_MOVE(settings.format_));
1,220✔
915
        }
1,220✔
916

917
        ///////////////////////////////////////////////////////////////////////
918
        static void (*default_set_console_dest)(logger_writer_type&,
919
            char const*, logging::level,
920
            logging_destination) = get_console_local;
921

922
        static void (*default_define_formatters)(
923
            logging::writer::named_write&) = define_formatters_local;
924

925
        static bool default_isconsole = true;
926

927
        void init_logging(runtime_configuration& ini, bool isconsole,
1,220✔
928
            void (*set_console_dest)(logger_writer_type&, char const*,
929
                logging::level, logging_destination),
930
            void (*define_formatters)(logging::writer::named_write&))
931
        {
932
            default_isconsole = isconsole;
1,220✔
933
            default_set_console_dest = set_console_dest;
1,220✔
934
            default_define_formatters = define_formatters;
1,220✔
935

936
            // initialize normal logs
937
            init_agas_log(ini, isconsole, set_console_dest, define_formatters);
1,220✔
938
            init_parcel_log(
1,220✔
939
                ini, isconsole, set_console_dest, define_formatters);
1,220✔
940
            init_timing_log(
1,220✔
941
                ini, isconsole, set_console_dest, define_formatters);
1,220✔
942
            init_hpx_log(ini, isconsole, set_console_dest, define_formatters);
1,220✔
943
            init_app_log(ini, isconsole, set_console_dest, define_formatters);
1,220✔
944
            init_debuglog_log(
1,220✔
945
                ini, isconsole, set_console_dest, define_formatters);
1,220✔
946

947
            // initialize console logs
948
            init_agas_console_log(ini);
1,220✔
949
            init_parcel_console_log(ini);
1,220✔
950
            init_timing_console_log(ini);
1,220✔
951
            init_hpx_console_log(ini);
1,220✔
952
            init_app_console_log(ini);
1,220✔
953
            init_debuglog_console_log(ini);
1,220✔
954
        }
1,220✔
955

956
        void init_logging_local(runtime_configuration& ini)
622✔
957
        {
958
            init_logging(ini, true, util::detail::get_console_local,
622✔
959
                util::detail::define_formatters_local);
960
        }
622✔
961
    }    // namespace detail
962

963
    ///////////////////////////////////////////////////////////////////////////
964
    void disable_logging(logging_destination dest)
×
965
    {
966
        switch (dest)
×
967
        {
968
        case logging_destination::hpx:
969
            hpx_logger()->set_enabled(logging::level::disable_all);
×
970
            hpx_console_logger()->set_enabled(logging::level::disable_all);
×
971
            break;
×
972

973
        case logging_destination::timing:
974
            timing_logger()->set_enabled(logging::level::disable_all);
×
975
            timing_console_logger()->set_enabled(logging::level::disable_all);
×
976
            break;
×
977

978
        case logging_destination::agas:
979
            agas_logger()->set_enabled(logging::level::disable_all);
×
980
            agas_console_logger()->set_enabled(logging::level::disable_all);
×
981
            break;
×
982

983
        case logging_destination::parcel:
984
            parcel_logger()->set_enabled(logging::level::disable_all);
×
985
            parcel_console_logger()->set_enabled(logging::level::disable_all);
×
986
            break;
×
987

988
        case logging_destination::app:
989
            app_logger()->set_enabled(logging::level::disable_all);
×
990
            app_console_logger()->set_enabled(logging::level::disable_all);
×
991
            break;
×
992

993
        case logging_destination::debuglog:
994
            debuglog_logger()->set_enabled(logging::level::disable_all);
×
995
            debuglog_console_logger()->set_enabled(logging::level::disable_all);
×
996
            break;
×
997
        }
998
    }
×
999

1000
    void enable_logging(logging_destination dest, std::string const& level,
×
1001
        std::string logdest, std::string logformat)
1002
    {
1003
        auto lvl = hpx::util::logging::level::enable_all;
×
1004
        if (!level.empty())
×
1005
        {
1006
            lvl = detail::get_log_level(level, true);
×
1007
        }
×
1008

1009
        switch (dest)
×
1010
        {
1011
        case logging_destination::hpx:
1012
            detail::init_hpx_log(lvl, logdest, logformat,
×
1013
                detail::default_isconsole, detail::default_set_console_dest,
×
1014
                detail::default_define_formatters);
×
1015
            detail::init_hpx_console_log(
×
1016
                lvl, HPX_MOVE(logdest), HPX_MOVE(logformat));
×
1017
            break;
×
1018

1019
        case logging_destination::timing:
1020
            detail::init_debuglog_log(lvl, logdest, logformat,
×
1021
                detail::default_isconsole, detail::default_set_console_dest,
×
1022
                detail::default_define_formatters);
×
1023
            detail::init_debuglog_console_log(
×
1024
                lvl, HPX_MOVE(logdest), HPX_MOVE(logformat));
×
1025
            break;
×
1026

1027
        case logging_destination::agas:
1028
            detail::init_agas_log(lvl, logdest, logformat,
×
1029
                detail::default_isconsole, detail::default_set_console_dest,
×
1030
                detail::default_define_formatters);
×
1031
            detail::init_agas_console_log(
×
1032
                lvl, HPX_MOVE(logdest), HPX_MOVE(logformat));
×
1033
            break;
×
1034

1035
        case logging_destination::parcel:
1036
            detail::init_parcel_log(lvl, logdest, logformat,
×
1037
                detail::default_isconsole, detail::default_set_console_dest,
×
1038
                detail::default_define_formatters);
×
1039
            detail::init_parcel_console_log(
×
1040
                lvl, HPX_MOVE(logdest), HPX_MOVE(logformat));
×
1041
            break;
×
1042

1043
        case logging_destination::app:
1044
            detail::init_app_log(lvl, logdest, logformat,
×
1045
                detail::default_isconsole, detail::default_set_console_dest,
×
1046
                detail::default_define_formatters);
×
1047
            detail::init_app_console_log(
×
1048
                lvl, HPX_MOVE(logdest), HPX_MOVE(logformat));
×
1049
            break;
×
1050

1051
        case logging_destination::debuglog:
1052
            detail::init_debuglog_log(lvl, logdest, logformat,
×
1053
                detail::default_isconsole, detail::default_set_console_dest,
×
1054
                detail::default_define_formatters);
×
1055
            detail::init_debuglog_console_log(
×
1056
                lvl, HPX_MOVE(logdest), HPX_MOVE(logformat));
×
1057
            break;
×
1058
        }
1059
    }
×
1060
}}    // namespace hpx::util
1061

1062
#else
1063

1064
#include <hpx/init_runtime_local/detail/init_logging.hpp>
1065
#include <hpx/modules/logging.hpp>
1066
#include <hpx/util/get_entry_as.hpp>
1067

1068
#include <iostream>
1069
#include <string>
1070

1071
namespace hpx { namespace util {
1072

1073
    //////////////////////////////////////////////////////////////////////////
1074
    void enable_logging(
1075
        logging_destination, std::string const&, std::string, std::string)
1076
    {
1077
    }
1078

1079
    void disable_logging(logging_destination) {}
1080

1081
    //////////////////////////////////////////////////////////////////////////
1082
    namespace detail {
1083

1084
        void warn_if_logging_requested(runtime_configuration& ini)
1085
        {
1086
            using util::get_entry_as;
1087

1088
            // warn if logging is requested
1089
            if (get_entry_as<int>(ini, "hpx.logging.level", -1) > 0 ||
1090
                get_entry_as<int>(ini, "hpx.logging.timing.level", -1) > 0 ||
1091
                get_entry_as<int>(ini, "hpx.logging.agas.level", -1) > 0 ||
1092
                get_entry_as<int>(ini, "hpx.logging.debuglog.level", -1) > 0 ||
1093
                get_entry_as<int>(ini, "hpx.logging.application.level", -1) > 0)
1094
            {
1095
                std::cerr
1096
                    << "hpx::init_logging: warning: logging is requested even "
1097
                       "while it was disabled at compile time. If you "
1098
                       "need logging to be functional, please reconfigure and "
1099
                       "rebuild HPX with HPX_WITH_LOGGING set to ON."
1100
                    << std::endl;
1101
            }
1102
        }
1103
    }    // namespace detail
1104
}}       // namespace hpx::util
1105

1106
#endif    // HPX_HAVE_LOGGING
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc