Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:18:17

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/Logger.h
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <string>
0011 #include <utility>
0012 
0013 #include "LoggerTypes.h"
0014 #include "LoggerMessage.h"
0015 #include "NullLoggerMessage.h"
0016 
0017 //---------------------------------------------------------------------------//
0018 // MACROS
0019 //---------------------------------------------------------------------------//
0020 //! Inject the source code provenance (current file and line)
0021 #define VECGEOM_CODE_PROVENANCE \
0022   ::vecgeom::Provenance         \
0023   {                             \
0024     __FILE__, __LINE__          \
0025   }
0026 
0027 /*!
0028  * \def VECGEOM_LOG
0029  *
0030  * Return a LogMessage object for streaming into at the given level. The
0031  * regular \c VECGEOM_LOG call is for code paths that happen uniformly in
0032  * parallel.
0033  *
0034  * The logger will only format and print messages. It is not responsible
0035  * for cleaning up the state or exiting an app.
0036  *
0037  * \code
0038  VECGEOM_LOG(debug) << "Don't print this in general";
0039  VECGEOM_LOG(warning) << "You may want to reconsider your life choices";
0040  VECGEOM_LOG(critical) << "Caught a fatal exception: " << e.what();
0041  * \endcode
0042  */
0043 #define VECGEOM_LOG(LEVEL) ::vecgeom::logger()(VECGEOM_CODE_PROVENANCE, ::vecgeom::LogLevel::LEVEL)
0044 
0045 //---------------------------------------------------------------------------//
0046 /*!
0047  * \def VECGEOM_LOG_LOCAL
0048  *
0049  * Like \c VECGEOM_LOG but for code paths that may only happen on a single
0050  * process. Use sparingly.
0051  */
0052 #define VECGEOM_LOG_LOCAL(LEVEL) ::vecgeom::self_logger()(VECGEOM_CODE_PROVENANCE, ::vecgeom::LogLevel::LEVEL)
0053 
0054 // Allow VECGEOM_LOGto be present (but ignored) in device code
0055 #ifdef __CUDA_ARCH__
0056 #undef VECGEOM_LOG
0057 #define VECGEOM_LOG(LEVEL) ::vecgeom::detail::NullLoggerMessage()
0058 #undef VECGEOM_LOG_LOCAL
0059 #define VECGEOM_LOG_LOCAL(LEVEL) ::vecgeom::detail::NullLoggerMessage()
0060 #endif
0061 
0062 namespace vecgeom {
0063 
0064 //---------------------------------------------------------------------------//
0065 /*!
0066  * Manage logging in serial and parallel.
0067  *
0068  * This should generally be called by the \c world_logger and \c
0069  * self_logger functions below. The call \c operator() returns an object that
0070  * should be streamed into in order to create a log message.
0071  *
0072  * This object \em is assignable, so to replace the default log handler with a
0073  * different one, you can call \code
0074    world_logger = Logger(MpiCommunicator::comm_world(), my_handler);
0075  * \endcode
0076  */
0077 class Logger {
0078 public:
0079   //!@{
0080   //! \name Type aliases
0081   using Message = detail::LoggerMessage;
0082   //!@}
0083 
0084 public:
0085   //! Get the default log level
0086   static constexpr LogLevel default_level() { return LogLevel::status; }
0087 
0088   // Construct with default communicator
0089   explicit Logger(LogHandler handle);
0090 
0091   // Create a logger that flushes its contents when it destructs
0092   inline Message operator()(Provenance prov, LogLevel lev);
0093 
0094   //! Set the minimum logging verbosity
0095   void level(LogLevel lev) { min_level_ = lev; }
0096 
0097   //! Get the current logging verbosity
0098   LogLevel level() const { return min_level_; }
0099 
0100 private:
0101   LogHandler handle_;
0102   LogLevel min_level_{default_level()};
0103 };
0104 
0105 //---------------------------------------------------------------------------//
0106 // INLINE DEFINITIONS
0107 //---------------------------------------------------------------------------//
0108 //! Create a logger that flushes its contents when it destructs
0109 auto Logger::operator()(Provenance prov, LogLevel lev) -> Message
0110 {
0111   LogHandler *handle = nullptr;
0112   if (handle_ && lev >= min_level_) {
0113     handle = &handle_;
0114   }
0115   return {handle, std::move(prov), lev};
0116 }
0117 
0118 //---------------------------------------------------------------------------//
0119 // FREE FUNCTIONS
0120 //---------------------------------------------------------------------------//
0121 // Get the log level from an environment variable
0122 LogLevel log_level_from_env(std::string const &);
0123 
0124 // Create a logger with reasonable default behaviors.
0125 Logger make_default_logger();
0126 
0127 // Main VecGeom logger
0128 Logger &logger();
0129 
0130 //---------------------------------------------------------------------------//
0131 } // namespace vecgeom