Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
logger.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_LOGGER_HH
8#define DUNE_FUFEM_LOGGER_HH
9
10#include <chrono>
11#include <mutex>
12#include <string>
13
15
16namespace Dune::Fufem {
17
41template<class Duration=std::chrono::duration<double>>
42auto makeLogger(std::ostream& stream, std::string format)
43{
44 auto startTimeStamp = std::chrono::high_resolution_clock::now();
45 auto lastTimeStamp = std::chrono::high_resolution_clock::now();
46 return [&, format, startTimeStamp, lastTimeStamp] (auto&& s) mutable {
47 using namespace std::chrono;
48 auto newTimeStamp = high_resolution_clock::now();
49 stream
50 << Dune::formatString( format,
51 duration_cast<Duration>(newTimeStamp - startTimeStamp),
52 duration_cast<Duration>(newTimeStamp - lastTimeStamp))
53 << s << std::endl;
54 lastTimeStamp = newTimeStamp;
55 };
56}
57
58
59
75template<class Duration=std::chrono::duration<double>>
77{
78 auto rawLogger=makeLogger<Duration>(stream, format);
79 return [rawLogger,&mutex](auto&& s) mutable {
80 auto lock = std::unique_lock(mutex);
81 rawLogger(s);
82 };
83}
84
85} // namespace Dune::Fufem
86
87#endif // DUNE_FUFEM_LOGGER_HH
88
static std::string formatString(const std::string &s, const T &... args)
Definition dunefunctionsboundaryfunctionalassembler.hh:29
auto makeSynchronizedLogger(std::ostream &stream, std::string format, std::mutex &mutex)
Create a simple logger callback.
Definition logger.hh:76
auto makeLogger(std::ostream &stream, std::string format)
Create a simple logger callback.
Definition logger.hh:42
T endl(T... args)