logging.cc 26.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "config.h"

#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <memory>
#include <optional>
#include <regex>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

16
#include <dune/pdelab/common/destructiblesingletonholder.hh>
17 18 19
#include <dune/pdelab/common/utility.hh>
#include <dune/pdelab/logging.hh>
#include <dune/pdelab/logging/loggerbackend.hh>
20
#include <dune/pdelab/logging/loggingstreambuffer.hh>
21 22 23 24 25 26 27 28 29 30
#include <dune/pdelab/logging/filesinks.hh>

namespace Dune::PDELab {

  ////////////////////////////////////////////////////////////////////////////////
  // sink factories
  ////////////////////////////////////////////////////////////////////////////////

  // Factory function for file sinks
  // Handles splitting into multiple files on parallel runs
31
  static auto filePerRankSinkFactory(
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    std::string_view name,
    LogLevel level,
    int widest_logger,
    const ParameterTree& params
    )
  {
    if (not params.hasKey("file"))
      DUNE_THROW(LoggingError,"You must specify an output file name for file sink: " << name);
    auto file_name = params["file"];
    if (file_name.empty())
      DUNE_THROW(LoggingError,"You must specify an output file name for file sink: " << name);

    auto mode = std::ios::trunc;

    if (params.hasKey("mode"))
    {
      auto mode_name = params["mode"];
      if (mode_name == "truncate")
        mode = std::ios::trunc;
      else if (mode_name == "append")
        mode = std::ios::ate;
      else
        DUNE_THROW(LoggingError,"Unknown file open mode " << mode_name << ": " << name);
    }

    auto comm = Logging::comm();
    auto size = comm.size();

    auto pattern_in_name = file_name.find("{}") != std::string::npos;

    if (comm.size() > 1 or pattern_in_name)
    {
      auto size_digits = 1;
      while (size / 10 > 0)
        ++size_digits;

      if (file_name.find("{}") != std::string::npos)
        file_name = fmt::format(file_name,fmt::format("{0:{1}}",comm.rank(),size_digits));
      else
        file_name = fmt::format("{1:0>{2}}-{0}"_fmt,file_name,comm.rank(),size_digits);
    }

    auto sink = std::make_shared<FileSink>(name,level,widest_logger,file_name,mode);

76
    sink->setPatternFormatParameters(params);
77 78 79
    return sink;
  }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  // Factory function for file sinks
  // Handles splitting into multiple files on parallel runs
  static auto rank0FileSinkFactory(
    std::string_view name,
    LogLevel level,
    int widest_logger,
    const ParameterTree& params
    ) -> std::shared_ptr<Sink>
  {
    if (not params.hasKey("file"))
      DUNE_THROW(LoggingError,"You must specify an output file name for file sink: " << name);
    auto file_name = params["file"];
    if (file_name.empty())
      DUNE_THROW(LoggingError,"You must specify an output file name for file sink: " << name);

    auto mode = std::ios::trunc;

    if (params.hasKey("mode"))
    {
      auto mode_name = params["mode"];
      if (mode_name == "truncate")
        mode = std::ios::trunc;
      else if (mode_name == "append")
        mode = std::ios::ate;
      else
        DUNE_THROW(LoggingError,"Unknown file open mode " << mode_name << ": " << name);
    }

    auto comm = Logging::comm();

    if (comm.rank() == 0)
    {
      auto sink = std::make_shared<FileSink>(name,level,widest_logger,file_name,mode);
113
      sink->setPatternFormatParameters(params);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      return sink;
    }
    else
    {
      return std::make_shared<NullSink>(name);
    }

  }

  // Factory function for file sinks
  // Handles splitting into multiple files on parallel runs
  static auto nullSinkFactory(
    std::string_view name,
    LogLevel level,
    int widest_logger,
    const ParameterTree& params
    )
  {
    return std::make_shared<NullSink>(name);
  }


136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  ////////////////////////////////////////////////////////////////////////////////
  // internal structs
  ////////////////////////////////////////////////////////////////////////////////

  struct Logging::SinkFactoryRepository
    : public std::unordered_map<std::string,Logging::SinkFactory>
  {
    using Base = std::unordered_map<std::string,Logging::SinkFactory>;
    using Base::Base;
  };

  struct Logging::State
  {

    using BackendRegistry = std::unordered_map<std::string_view,std::unique_ptr<LoggerBackend>>;
    using SinkRegistry = std::unordered_map<std::string_view,std::shared_ptr<Sink>>;

    using NameStorage = std::unordered_set<std::string>;

    // In order to use string_view in the map keys, we need to make sure the underlying string
    // doesn't go away, so we store all of them in a set.
    std::string_view internalize(std::string_view name)
    {
      auto [internalized,inserted] = name_storage.emplace(name);
      return *internalized;
    }

    State(const State&) = delete;
    State& operator=(const State&) = delete;

    // The state constructor makes sure that the initialization happens from a call
    // to state() that got passed a pointer to a comm object, which only happens in
    // init(). This allows us to catch uninitialized usage of the system.
    State(const CollectiveCommunication* comm_)
170 171 172
      : cout_buf(true)
      , cerr_buf(true)
      , clog_buf(true)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    {
      if (not comm_)
        DUNE_THROW(LoggingError,"You must call Dune::PDELab::Logging::init() before using the logging system");
      comm.emplace(*comm_);
    }

    NameStorage name_storage;
    BackendRegistry backends;
    SinkRegistry sinks;
    LoggerBackend* default_backend = nullptr;
    std::size_t widest_logger = 0;
    std::shared_ptr<ConsoleSink> cout;
    std::shared_ptr<ConsoleSink> cerr;
    bool muted = false;
    LogLevel unmuted_cout = LogLevel::all;
    LogLevel unmuted_cerr = LogLevel::all;
    std::optional<CollectiveCommunication> comm;
    LogMessage::Time startup_time = LogMessage::Clock::now();
191
    Logger logger;
192 193 194 195 196 197
    LoggingStreamBuffer cout_buf;
    LoggingStreamBuffer cerr_buf;
    LoggingStreamBuffer clog_buf;
    std::streambuf* orig_cout_buf = nullptr;
    std::streambuf* orig_cerr_buf = nullptr;
    std::streambuf* orig_clog_buf = nullptr;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

  };

  ////////////////////////////////////////////////////////////////////////////////
  // internal helper functionality
  ////////////////////////////////////////////////////////////////////////////////

  // helper function that reaises an exception if passed an invalid name
  static void validateName(std::string_view name)
  {
    if (not Logging::isValidName(name))
      DUNE_THROW(LoggingError,"Invalid name for logging component: " << name);
  }

  // This function does the one-time setup for the sink factory, mostly
  // registering default factories.
214
  std::unique_ptr<Logging::SinkFactoryRepository> Logging::makeSinkFactoryRepository()
215
  {
216 217
    auto result = std::make_unique<Logging::SinkFactoryRepository>();
    auto& repo = *result;
218 219 220
    repo["file-per-rank"] = filePerRankSinkFactory;
    repo["rank-0-file"] = rank0FileSinkFactory;
    repo["null"] = nullSinkFactory;
221
    return std::move(result);
222 223 224 225
  }

  Logging::SinkFactoryRepository& Logging::sinkFactoryRepository()
  {
226 227 228 229
    auto& holder = destructibleSingleton<SinkFactoryRepository>(makeSinkFactoryRepository);
    if (not holder)
      holder.create();
    return holder.get();
230 231 232 233 234 235 236
  }

  Logging::SinkFactory& Logging::sinkFactory(const std::string& name)
  {
    return sinkFactoryRepository().at(name);
  }

237 238 239 240 241
  std::unique_ptr<Logging::State> Logging::makeState(const CollectiveCommunication* comm)
  {
    return std::make_unique<State>(comm);
  }

242 243
  Logging::State& Logging::state(const CollectiveCommunication* comm)
  {
244 245 246 247
    auto& holder = destructibleSingleton<State>(makeState);
    if (not holder)
      holder.create(comm);
    return holder.get();
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  }

  LoggerBackend& Logging::backend(std::string_view name)
  {
    using namespace std::literals;

    // The empty name gets forwarded to "default"
    if (name.empty())
      name = "default"sv;
    try {
      return *state().backends.at(name);
    } catch (std::out_of_range&) {
      DUNE_THROW(LoggingError,"Could not find backend in registry: " << name);
    }
  }

  ////////////////////////////////////////////////////////////////////////////////
  // initialization
  ////////////////////////////////////////////////////////////////////////////////

  void Logging::init(const CollectiveCommunication& comm, const ParameterTree& params)
  {
270 271 272 273 274 275
    using namespace std::literals;

    // initialize sink factory registry singleton if necessary
    sinkFactoryRepository();

    // initialize state singleton
276 277 278 279
    auto& s = state(&comm);

    // create default sinks for stdout and stderr
    {
280
      std::string default_pattern = "[{reltime:9%T}] {0}";
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
      auto        default_level   = LogLevel::all;

      auto level = default_level;
      if (params.hasKey("sink.stdout.level"))
        level = parseLogLevel(params["sink.stdout.level"]);

      auto pattern = params.get("sink.stdout.pattern",default_pattern);

      s.cout = std::make_shared<ConsoleSink>("stdout",stdout,pattern,level,0);
      registerSink(s.cout);

      level = default_level;
      if (params.hasKey("sink.stderr.level"))
        level = parseLogLevel(params["sink.stderr.level"]);

      pattern = params.get("sink.stderr.pattern",default_pattern);

      s.cerr = std::make_shared<ConsoleSink>("stderr",stderr,pattern,level,0);
      registerSink(s.cerr);
    }

    // create custom sinks
    if (params.hasSub("sink"))
    {
      auto& sinks = params.sub("sink");
      for (auto& name : sinks.getSubKeys())
      {
        if (name == "stdout" or name == "stderr")
          continue;
        validateName(name);
        auto& config = sinks.sub(name);
        makeSink(name,config);
      }
    }

    // create default logger backend
    {
      auto level = LogLevel::notice;
      if (params.hasKey("default.level"))
        level = parseLogLevel(params["default.level"]);

      auto enabled = params.get("default.enabled",true);
      auto indent  = params.get("default.indent",0);

      auto internalized_name = s.internalize("default");
      s.default_backend = s.backends.emplace(
        internalized_name,
        new LoggerBackend(
          "default",
          s.startup_time,
          enabled,
          level,
          indent
          )
        ).first->second.get();

      if (params.hasKey("default.sinks"))
      {
        auto sinks = parseConfigList(params["default.sinks"]);
        for (auto name : sinks)
          s.default_backend->_sinks.push_back(sink({begin(name),end(name)}));
      }
      else
      {
        // log to stdout by default
        s.default_backend->_sinks.push_back(cout());
      }

      if (params.hasKey("default.extra_sinks")){
        auto sinks = parseConfigList(params["default.extra_sinks"]);
        for (auto name : sinks)
          s.default_backend->_sinks.push_back(sink({begin(name),end(name)}));
      }
    }

    // create logger backends
    if (params.hasSub("backend"))
    {
      auto& backends = params.sub("backend");

      // create backends with default configuration (inherited from default logger)
      for (auto& name : backends.getValueKeys())
      {

        validateName(name);

        if (backends[name] != "default")
          DUNE_THROW(LoggingError,"When declaring a logger with default configuration, its assigned "
                                  "value must be \"default\": " << name);

        if (name == "default")
          DUNE_THROW(LoggingError,"You cannot declare the default logger as \"default\"");

        registerBackend(name, s.default_backend->_default_level, true);
      }

      // create backends with custom configuration
      for (auto& name : backends.getSubKeys())
      {
        validateName(name);

        if (name == "default")
          DUNE_THROW(
            LoggingError,
            "You cannot create a custom logger backend with the reserved name \"default\". Change "
            "the default backend configuration using the key group \"default\" without prepending "
            "\"backend.\"");

        auto& config = backends.sub(name);

        LogLevel level = s.default_backend->_default_level;
        if (config.hasKey("level"))
          level = parseLogLevel(config["level"]);

        bool enabled = config.get("enabled",s.default_backend->_enabled);
        int indent   = config.get("indent",s.default_backend->_default_indent);

        auto& backend = *registerBackend(name,level,not config.hasKey("sinks"))._backend;
        backend._default_indent = indent;
        backend._enabled = enabled;

        if (config.hasKey("sinks"))
        {
          auto sinks = parseConfigList(config["sinks"]);
          for (auto name : sinks)
            backend._sinks.push_back(sink({begin(name),end(name)}));
        }

        if (config.hasKey("extra_sinks")){
410
          auto sinks = parseConfigList(config["extra_sinks"]);
411
          for (auto name : sinks)
412
            backend._sinks.push_back(sink({begin(name),end(name)}));
413 414 415 416
        }
      }
    }

417 418 419 420 421 422 423 424 425 426 427 428 429
    // configure internal logger for logging system itself
    {
      auto level = parseLogLevel(params.get("internal.level","notice"));
      auto backend = params.get("internal.backend","logging");

      // create default-configured logging backend if necessary
      if (backend == "logging" and s.backends.count("logging") == 0)
        Logging::registerBackend("logging",s.default_backend->_default_level);

      s.logger = logger(backend);
      s.logger.setLevel(level);
    }

430 431 432 433
    if (params.get("muted",s.comm->rank() > 0))
    {
      mute();
      if (s.comm->size() > 0)
434
        s.logger.info("Muted console log sinks on MPI ranks > 0"_fmt);
435 436
    }

437 438
    // from here on out, it is allowed to log messages

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    if (params.hasKey("redirect"))
    {
      auto level = parseLogLevel(params["redirect"]);

      if (not (s.backends.count("cout") > 0))
        Logging::registerBackend("cout",s.default_backend->_default_level);
      Logging::redirectCout("cout",level);

      if (not (s.backends.count("cerr") > 0))
        Logging::registerBackend("cerr",s.default_backend->_default_level);
      Logging::redirectCerr("cerr",level);

      if (not (s.backends.count("clog") > 0))
        Logging::registerBackend("clog",s.default_backend->_default_level);
      Logging::redirectClog("clog",level);
    }
    else if (params.hasSub("redirect"))
    {
      auto& redirects = params.sub("redirect");

      if (redirects.hasSub("cout"))
      {
        auto& config = params.sub("cout");
        auto backend = config.get<std::string>("backend","");
        auto level   = parseLogLevel(config.get<std::string>("level","notice"));
        auto buffered = config.get("buffered",true);
        Logging::redirectCout(backend,level,buffered);
      }

      if (redirects.hasSub("cerr"))
      {
        auto& config = params.sub("cerr");
        auto backend = config.get<std::string>("backend","");
        auto level   = parseLogLevel(config.get<std::string>("level","notice"));
        auto buffered = config.get("buffered",true);
        Logging::redirectCerr(backend,level,buffered);
      }

      if (redirects.hasSub("clog"))
      {
        auto& config = params.sub("clog");
        auto backend = config.get<std::string>("backend","");
        auto level   = parseLogLevel(config.get<std::string>("level","notice"));
        auto buffered = config.get("buffered",true);
        Logging::redirectClog(backend,level,buffered);
      }
    }

487 488 489 490 491 492 493 494 495 496 497
    std::time_t startup_time = std::chrono::system_clock::to_time_t(s.startup_time);
    std::tm local_time;
#ifdef DUNE_HAVE_LOCALTIME_R
    localtime_r(&startup_time,&local_time);
#else
    std::tm* tm = std::localtime(&startup_time);
    std::memcpy(&local_time,tm,sizeof(std::tm));
#endif

    // We need to manually format the time, as doing so is not constexpr
    auto time_string = fmt::format("{:%a %F %T %Z}",local_time);
498
    s.logger.notice("Logging system initialized at {}"_fmt,time_string);
499
  }
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  void Logging::shutdown()
  {
    if (isCoutRedirected())
      restoreCout();
    if (isCerrRedirected())
      restoreCerr();
    if (isClogRedirected())
      restoreClog();
    state().logger.notice("Shutting down logging system"_fmt);
    destructibleSingleton<State>(makeState).destroy();
    destructibleSingleton<SinkFactoryRepository>(makeSinkFactoryRepository).destroy();
  }

  bool Logging::initialized()
  {
    return destructibleSingleton<State>(makeState);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  }

  // Make not to use state() in here, this function must work before init().
  void Logging::registerSinkFactory(const std::string& name, SinkFactory sink_factory)
  {
    validateName(name);
    auto factories = sinkFactoryRepository();
    if (factories.count(name) > 0)
      DUNE_THROW(LoggingError, "Cannot register sink factory, name already used: " << name);
    factories[name] = sink_factory;
  }

  ////////////////////////////////////////////////////////////////////////////////
  // Sinks
  ////////////////////////////////////////////////////////////////////////////////

  std::shared_ptr<Sink> Logging::makeSink(const std::string& name, const ParameterTree& params)
  {
    validateName(name);
    auto& sinks = state().sinks;
    if (sinks.count(name) > 0)
      DUNE_THROW(LoggingError, "Cannot register sink, name already used: " << name);

    auto level = LogLevel::all;
    if (params.hasKey("level"))
      level = parseLogLevel(params["level"]);

    auto [sink,_] = sinks.emplace(name,sinkFactory(params["type"])(name,level,state().widest_logger,params));
    return sink->second;;
  }

  void Logging::registerSink(std::shared_ptr<Sink> sink)
  {
    validateName(sink->name());
    auto &sinks = state().sinks;
    if (sinks.count(sink->name()) > 0)
      DUNE_THROW(LoggingError,
                 "Cannot register sink, name already used: " << sink->name());
    sinks[sink->name()] = sink;
    sink->setWidestLogger(state().widest_logger);
  }

  std::shared_ptr<Sink> Logging::sink(const std::string& name)
  {
    try {
      return state().sinks.at(name);
    } catch (std::out_of_range&) {
      DUNE_THROW(LoggingError,"Could not find sink in registry: " << name);
    }
  }

  bool Logging::retireSink(std::string_view sink)
  {
    return state().sinks.erase(sink) > 0;
  }

  std::shared_ptr<ConsoleSink> Logging::cout()
  {
    return state().cout;
  }

  std::shared_ptr<ConsoleSink> Logging::cerr()
  {
    return state().cerr;
  }

  ////////////////////////////////////////////////////////////////////////////////
  // Loggers
  ////////////////////////////////////////////////////////////////////////////////

  Logger Logging::logger()
  {
    auto& backend = *state().default_backend;
    return {backend,backend._default_level,backend._default_indent};
  }

  Logger Logging::logger(std::string_view name)
  {
595 596 597 598 599 600 601 602 603
    try {
      using namespace std::literals;
      if (name.empty())
        name = "default"sv;
      auto& backend = *state().backends.at(name);
      return {backend,backend._default_level,backend._default_indent};
    } catch (std::out_of_range&) {
      DUNE_THROW(LoggingError,"Logger backend not found in registry: " << name);
    }
604 605 606 607
  }

  Logger Logging::logger(const ParameterTree& params)
  {
608 609 610 611 612 613 614 615 616 617 618 619 620
    try {
      auto name = params.get<std::string>("log.backend","");
      auto log = logger(name);
      if (params.hasKey("log.level"))
        log.setLevel(parseLogLevel(params["log.level"]));
      if (params.hasKey("log.indent"))
        log.setIndent(params.get<int>("log.indent"));
      return log;
    } catch (std::out_of_range&) {
      DUNE_THROW(LoggingError,"Logger backend not found in registry: " << params.get<std::string>("log.backend",""));
    }
  }

621
  Logger Logging::componentLogger(const ParameterTree& params, std::string_view preferred)
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  {
    auto& s = state();
    LoggerBackend* backend = nullptr;
    if (params.hasKey("log.backend"))
    {
      try {
        std::string_view name = params["log.backend"];
        if (name.empty())
          name = "default";
        backend = s.backends.at(name).get();
      } catch (std::out_of_range&) {
        DUNE_THROW(LoggingError,"Logger backend not found in registry: " << params["log.backend"]);
      }
    }
    else
    {
      if (auto it = s.backends.find(preferred) ; it != s.backends.end())
        backend = it->second.get();
      else
        backend = s.default_backend;
    }

    auto level = backend->_default_level;
645
    if (params.hasKey("log.level"))
646 647 648
      level = parseLogLevel(params["log.level"]);

    auto indent = backend->_default_indent;
649
    if (params.hasKey("log.indent"))
650 651 652
      indent = params.get<int>("log.indent");

    return {*backend,level,indent};
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  }

  ////////////////////////////////////////////////////////////////////////////////
  // Backends
  ////////////////////////////////////////////////////////////////////////////////

  Logger Logging::registerBackend(
    std::string_view name,
    LogLevel level,
    bool attach_default_sinks
    )
  {
    auto& s = state();

    validateName(name);

    if (name == "default")
      DUNE_THROW(LoggingError, "You cannot create a custom logger backend with the reserved name \"default\".");

    auto internalized_name = s.internalize(name);
    auto& backend = *s.backends.emplace(
      internalized_name,
      new LoggerBackend(
        internalized_name,
        s.startup_time,
        s.default_backend->_enabled,
        level,
        s.default_backend->_default_indent
        )
      ).first->second;

    if (attach_default_sinks)
    {
      // inherit the sink configuration from the default backend
      auto& default_sinks = s.default_backend->_sinks;
      std::copy(begin(default_sinks),end(default_sinks),std::back_inserter(backend._sinks));
    }

    if (name.length() > s.widest_logger)
    {
      s.widest_logger = name.length();
      // inform all sinks about the new longest logger name
      for (auto& [_,sink] : s.sinks)
        sink->setWidestLogger(s.widest_logger);
    }

    return {backend,backend._default_level,backend._default_indent};
  }

  bool Logging::attachSink(std::string_view backend_name, std::string_view sink_name)
  {
    auto& b = backend(backend_name);
    auto  s = sink({begin(sink_name),end(sink_name)});

    if (std::any_of(begin(b._sinks),end(b._sinks),[=](auto& sink) { return sink->name() == sink_name; }))
      return false;

    b._sinks.push_back(s);

    return true;
  }

  bool Logging::detachSink(std::string_view backend_name, std::string_view sink_name)
  {
    auto& b = backend(backend_name);
    auto  s = sink({begin(sink_name),end(sink_name)});

    if (auto it = std::remove_if(
          begin(b._sinks),
          end(b._sinks),
          [=](auto& sink) { return sink->name() == sink_name; }
          ) ;
        it != end(b._sinks)
      )
    {
      b._sinks.erase(it,end(b._sinks));
      return true;
    }

    return false;
  }

  ////////////////////////////////////////////////////////////////////////////////
  // Muting
  ////////////////////////////////////////////////////////////////////////////////

  bool Logging::muted()
  {
    return state().muted;
  }

  void Logging::mute()
  {
    auto& s = state();
    s.unmuted_cout = s.cout->level();
    s.cout->setLevel(LogLevel::off);
    s.unmuted_cerr = s.cerr->level();
    s.cerr->setLevel(LogLevel::off);
    s.muted = true;
  }

  void Logging::unmute()
  {
    auto& s = state();
    s.cout->setLevel(s.unmuted_cout);
    s.cerr->setLevel(s.unmuted_cerr);
    s.muted = false;
  }

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
  ////////////////////////////////////////////////////////////////////////////////
  // Standard C++ stream redirection
  ////////////////////////////////////////////////////////////////////////////////


  void Logging::redirectCout(std::string_view backend, LogLevel level, bool buffered)
  {
    auto& s = state();
    Logger logger = Logging::logger(backend);
    logger.setDefaultLevel(level);
    s.cout_buf.setLogger(logger);
    s.cout_buf.setLineBuffered(buffered);
    if (not s.orig_cout_buf)
      s.orig_cout_buf = std::cout.rdbuf();
    std::cout.rdbuf(&s.cout_buf);
777
    s.logger.info("Redirected std::cout to backend {} with level {}, buffered: {}"_fmt,backend,level,buffered);
778 779 780 781 782 783 784 785 786 787 788 789
  }

  void Logging::redirectCerr(std::string_view backend, LogLevel level, bool buffered)
  {
    auto& s = state();
    Logger logger = Logging::logger(backend);
    logger.setDefaultLevel(level);
    s.cerr_buf.setLogger(logger);
    s.cerr_buf.setLineBuffered(buffered);
    if (not s.orig_cerr_buf)
      s.orig_cerr_buf = std::cerr.rdbuf();
    std::cerr.rdbuf(&s.cerr_buf);
790
    s.logger.info("Redirected std::cerr to backend {} with level {}, buffered: {}"_fmt,backend,level,buffered);
791 792 793 794 795 796 797 798 799 800 801 802
  }

  void Logging::redirectClog(std::string_view backend, LogLevel level, bool buffered)
  {
    auto& s = state();
    Logger logger = Logging::logger(backend);
    logger.setDefaultLevel(level);
    s.clog_buf.setLogger(logger);
    s.clog_buf.setLineBuffered(buffered);
    if (not s.orig_clog_buf)
      s.orig_clog_buf = std::clog.rdbuf();
    std::clog.rdbuf(&s.clog_buf);
803
    s.logger.info("Redirected std::clog to backend {} with level {}, buffered: {}"_fmt,backend,level,buffered);
804 805 806 807 808 809 810 811 812
  }

  void Logging::restoreCout()
  {
    auto& s = state();
    if (s.orig_cout_buf)
    {
      std::cout.rdbuf(s.orig_cout_buf);
      s.orig_cout_buf = nullptr;
813
      s.logger.notice("Stopped redirection of std::cout"_fmt);
814 815
    }
    else
816
      s.logger.info("Cannot stop redirection of std::cout, not redirected at the moment"_fmt);
817 818 819 820 821 822 823 824 825
  }

  void Logging::restoreCerr()
  {
    auto& s = state();
    if (s.orig_cerr_buf)
    {
      std::cerr.rdbuf(s.orig_cerr_buf);
      s.orig_cerr_buf = nullptr;
826
      s.logger.info("Stopped redirection of std::cerr"_fmt);
827 828
    }
    else
829
      s.logger.warning("Cannot stop redirection of std::cerr, not redirected at the moment"_fmt);
830 831 832 833 834 835 836 837 838
  }

  void Logging::restoreClog()
  {
    auto& s = state();
    if (s.orig_clog_buf)
    {
      std::clog.rdbuf(s.orig_clog_buf);
      s.orig_clog_buf = nullptr;
839
      s.logger.info("Stopped redirection of std::clog"_fmt);
840 841
    }
    else
842
      s.logger.warning("Cannot stop redirection of std::clog, not redirected at the moment"_fmt);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
  }

  bool Logging::isCoutRedirected()
  {
    return state().orig_cout_buf;
  }

  bool Logging::isCerrRedirected()
  {
    return state().orig_cerr_buf;
  }

  bool Logging::isClogRedirected()
  {
    return state().orig_clog_buf;
  }

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
  ////////////////////////////////////////////////////////////////////////////////
  // Utilities
  ////////////////////////////////////////////////////////////////////////////////

  bool Logging::isValidName(std::string_view name)
  {
    static const std::regex valid_pattern("[a-z](-?[a-z0-9])*",std::regex::nosubs);
    return std::regex_match(begin(name),end(name),valid_pattern);
  }

  const Logging::CollectiveCommunication& Logging::comm()
  {
    return *state().comm;
  }




} // end namespace Dune::PDELab