Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:26:05

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-2023 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file VecGeom/management/LoggerMessage.h
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <iostream>
0011 #include <memory>
0012 #include <utility>
0013 
0014 #include "LoggerTypes.h"
0015 
0016 namespace vecgeom {
0017 
0018 namespace detail {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Stream-like helper class for writing log messages.
0022  *
0023  * This class should only be created by a Logger instance. When it destructs,
0024  * the handler is called to print the information.
0025  */
0026 class LoggerMessage {
0027 public:
0028   //!@{
0029   //! \name Type aliases
0030   using StreamManip = std::ostream &(*)(std::ostream &);
0031   //!@}
0032 
0033 public:
0034   // Construct with reference to function object, etc.
0035   LoggerMessage(LogHandler *handle, Provenance prov, LogLevel lev);
0036 
0037   // Flush message on destruction
0038   ~LoggerMessage();
0039 
0040   //!@{
0041   //! Prevent copying but allow moving
0042   LoggerMessage(LoggerMessage const &)            = delete;
0043   LoggerMessage &operator=(LoggerMessage const &) = delete;
0044   LoggerMessage(LoggerMessage &&)                 = default;
0045   LoggerMessage &operator=(LoggerMessage &&)      = default;
0046   //!@}
0047 
0048   // Write the object to the stream if applicable
0049   template <class T>
0050   inline LoggerMessage &operator<<(T &&rhs);
0051 
0052   // Accept manipulators such as std::endl, std::setw
0053   inline LoggerMessage &operator<<(StreamManip manip);
0054 
0055   // Update the steam state
0056   inline void setstate(std::ostream::iostate state);
0057 
0058 private:
0059   LogHandler *handle_;
0060   Provenance prov_;
0061   LogLevel lev_;
0062   std::unique_ptr<std::ostream> os_;
0063 };
0064 
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * Write the object to the stream if applicable.
0068  */
0069 template <class T>
0070 LoggerMessage &LoggerMessage::operator<<(T &&rhs)
0071 {
0072   if (os_) {
0073     *os_ << std::forward<T>(rhs);
0074   }
0075   return *this;
0076 }
0077 
0078 //---------------------------------------------------------------------------//
0079 /*!
0080  * Accept a stream manipulator.
0081  */
0082 LoggerMessage &LoggerMessage::operator<<(StreamManip manip)
0083 {
0084   if (os_) {
0085     manip(*os_);
0086   }
0087   return *this;
0088 }
0089 
0090 //---------------------------------------------------------------------------//
0091 /*!
0092  * Update the steam state (needed by some manipulators).
0093  */
0094 void LoggerMessage::setstate(std::ostream::iostate state)
0095 {
0096   if (os_) {
0097     os_->setstate(state);
0098   }
0099 }
0100 
0101 //---------------------------------------------------------------------------//
0102 }  // namespace detail
0103 } // namespace vecgeom