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