Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-11 08:55:54

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 // Allow VECGEOM_LOGto be present (but ignored) in device code
0046 #ifdef __CUDA_ARCH__
0047 #undef VECGEOM_LOG
0048 #define VECGEOM_LOG(LEVEL) ::vecgeom::detail::NullLoggerMessage()
0049 #undef VECGEOM_LOG_LOCAL
0050 #define VECGEOM_LOG_LOCAL(LEVEL) ::vecgeom::detail::NullLoggerMessage()
0051 #endif
0052 
0053 namespace vecgeom {
0054 
0055 //---------------------------------------------------------------------------//
0056 /*!
0057  * Manage logging in serial and parallel.
0058  *
0059  * This should generally be called by the \c logger function below. The call \c operator() returns an object that
0060  * should be streamed into in order to create a log message.
0061  *
0062  * This object \em is assignable, so to replace the default log handler with a
0063  * different one, you can call \code
0064    world_logger = Logger(MpiCommunicator::comm_world(), my_handler);
0065  * \endcode
0066  */
0067 class Logger {
0068 public:
0069   //!@{
0070   //! \name Type aliases
0071   using Message = detail::LoggerMessage;
0072   //!@}
0073 
0074 public:
0075   //! Get the default log level
0076   static constexpr LogLevel default_level() { return LogLevel::status; }
0077 
0078   // Construct with default communicator
0079   explicit Logger(LogHandler handle);
0080 
0081   // Create a logger that flushes its contents when it destructs
0082   inline Message operator()(Provenance prov, LogLevel lev);
0083 
0084   //! Set the minimum logging verbosity
0085   void level(LogLevel lev) { min_level_ = lev; }
0086 
0087   //! Get the current logging verbosity
0088   LogLevel level() const { return min_level_; }
0089 
0090 private:
0091   LogHandler handle_;
0092   LogLevel min_level_{default_level()};
0093 };
0094 
0095 //---------------------------------------------------------------------------//
0096 // INLINE DEFINITIONS
0097 //---------------------------------------------------------------------------//
0098 //! Create a logger that flushes its contents when it destructs
0099 auto Logger::operator()(Provenance prov, LogLevel lev) -> Message
0100 {
0101   LogHandler *handle = nullptr;
0102   if (handle_ && lev >= min_level_) {
0103     handle = &handle_;
0104   }
0105   return {handle, std::move(prov), lev};
0106 }
0107 
0108 //---------------------------------------------------------------------------//
0109 // FREE FUNCTIONS
0110 //---------------------------------------------------------------------------//
0111 // Get the log level from an environment variable
0112 LogLevel log_level_from_env(std::string const &);
0113 
0114 // Create a logger with reasonable default behaviors.
0115 Logger make_default_logger();
0116 
0117 // Main VecGeom logger
0118 Logger &logger();
0119 
0120 //---------------------------------------------------------------------------//
0121 } // namespace vecgeom