Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-09 09:25:44

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/Utilities/Logger.hpp"
0010 
0011 #include <fstream>
0012 #include <memory>
0013 
0014 // Helper functions that take logger as const ref argument
0015 void processSomething(int value,
0016                       const Acts::Logger &logger = Acts::getDummyLogger()) {
0017   ACTS_DEBUG("Processing value: " << value);
0018   ACTS_INFO("Process completed successfully");
0019 }
0020 
0021 void optionalLogging(const Acts::Logger &logger = Acts::getDummyLogger()) {
0022   ACTS_VERBOSE("This message will be discarded with dummy logger");
0023   // Dummy logger discards all output - useful for performance-critical code
0024 }
0025 
0026 int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
0027   //! [Member Logger Pattern]
0028   struct MyClass {
0029     std::unique_ptr<const Acts::Logger> m_logger;
0030     const Acts::Logger &logger() const { return *m_logger; }
0031 
0032     MyClass(std::unique_ptr<const Acts::Logger> logger)
0033         : m_logger(std::move(logger)) {}
0034 
0035     void doWork() { ACTS_INFO("Doing work in MyClass"); }
0036   };
0037 
0038   // Usage
0039   MyClass obj(Acts::getDefaultLogger("MyClass", Acts::Logging::INFO));
0040   obj.doWork();
0041   //! [Member Logger Pattern]
0042 
0043   //! [Const Ref Argument Pattern]
0044   // Usage with custom logger
0045   auto customLogger = Acts::getDefaultLogger("Processor", Acts::Logging::DEBUG);
0046   processSomething(42, *customLogger);
0047 
0048   // Usage with default (dummy) logger
0049   processSomething(100);
0050   //! [Const Ref Argument Pattern]
0051 
0052   //! [getDummyLogger Pattern]
0053   // Call without logging overhead
0054   optionalLogging();
0055 
0056   // Or provide a real logger when needed
0057   auto debugLogger = Acts::getDefaultLogger("Debug", Acts::Logging::VERBOSE);
0058   optionalLogging(*debugLogger);
0059   //! [getDummyLogger Pattern]
0060 
0061   {
0062     auto verboseLogger =
0063         Acts::getDefaultLogger("ComponentB", Acts::Logging::VERBOSE);
0064     const auto &logger = *verboseLogger;
0065 
0066     int variable = 10;
0067     std::string errorMsg = "File not found";
0068 
0069     //! [Logging Macros]
0070     ACTS_VERBOSE("Detailed trace information");
0071     ACTS_DEBUG("Debug info: " << variable);
0072     ACTS_INFO("Operation completed");
0073     ACTS_WARNING("Potential issue detected");
0074     ACTS_ERROR("Operation failed: " << errorMsg);
0075     ACTS_FATAL("Critical failure");
0076     //! [Logging Macros]
0077   }
0078 
0079   //! [Logger Cloning]
0080   // Create a base logger
0081   auto baseLogger = Acts::getDefaultLogger("Fitter", Acts::Logging::INFO);
0082 
0083   // Clone with same name and level
0084   auto clonedLogger = baseLogger->clone();
0085 
0086   // Clone with a different name
0087   auto renamedLogger = baseLogger->clone("NewFitter");
0088 
0089   // Clone with a different log level (keeps same name)
0090   auto verboseClone = baseLogger->clone(Acts::Logging::VERBOSE);
0091 
0092   // Clone with both new name and new level
0093   auto customClone = baseLogger->clone("CustomFitter", Acts::Logging::DEBUG);
0094 
0095   // Clone with a suffix appended to the name
0096   auto actorLogger = baseLogger->cloneWithSuffix("Actor");
0097   // Result: logger named "FitterActor" with INFO level
0098 
0099   // Clone with a suffix and different level
0100   auto debugActorLogger =
0101       baseLogger->cloneWithSuffix("Actor", Acts::Logging::DEBUG);
0102   // Result: logger named "FitterActor" with DEBUG level
0103 
0104   // Common pattern: Create sub-component loggers
0105   auto updaterLogger = baseLogger->cloneWithSuffix("Updater");
0106   auto smootherLogger = baseLogger->cloneWithSuffix("Smoother");
0107   //! [Logger Cloning]
0108 
0109   //! [Logger Cloning Sub-component]
0110   // Example: Class with multiple internal components that need separate logging
0111   struct TrackFitter {
0112     std::unique_ptr<const Acts::Logger> m_logger;
0113     std::unique_ptr<const Acts::Logger> m_actorLogger;
0114     std::unique_ptr<const Acts::Logger> m_updaterLogger;
0115 
0116     explicit TrackFitter(std::unique_ptr<const Acts::Logger> logger)
0117         : m_logger(std::move(logger)),
0118           m_actorLogger(m_logger->cloneWithSuffix("Actor")),
0119           m_updaterLogger(m_logger->cloneWithSuffix("Updater")) {}
0120   };
0121   // Creates loggers: "Fitter", "FitterActor", "FitterUpdater"
0122   //! [Logger Cloning Sub-component]
0123 
0124   //! [Logger Cloning Per-component Levels]
0125   // Creating loggers with different verbosity levels for different components
0126   auto baseDetectorLogger =
0127       Acts::getDefaultLogger("Detector", Acts::Logging::INFO);
0128   auto surfaceLogger =
0129       baseDetectorLogger->clone("Surface", Acts::Logging::DEBUG);
0130   auto volumeLogger =
0131       baseDetectorLogger->clone("Volume", Acts::Logging::WARNING);
0132   //! [Logger Cloning Per-component Levels]
0133 
0134   //! [Logger Cloning Testing]
0135   // Creating test loggers with specific configurations
0136   auto productionLogger =
0137       Acts::getDefaultLogger("Production", Acts::Logging::WARNING);
0138   auto testLogger = productionLogger->clone("Test", Acts::Logging::VERBOSE);
0139   //! [Logger Cloning Testing]
0140 
0141   //! [Custom Output Streams]
0142   // Create a logger that writes to a file instead of stdout
0143   std::ofstream logFile("mylog.txt");
0144   auto fileLogger =
0145       Acts::getDefaultLogger("Component", Acts::Logging::INFO, &logFile);
0146   //! [Custom Output Streams]
0147 }
0148 
0149 //! [Local logger macro]
0150 void myFunction() {
0151   auto myLogger = Acts::getDefaultLogger("Production", Acts::Logging::WARNING);
0152   ACTS_LOCAL_LOGGER(std::move(myLogger));
0153 
0154   ACTS_VERBOSE("hello world!");
0155 }
0156 //! [Local logger macro]
0157 
0158 //! [Logger preload]
0159 namespace Acts {
0160 std::unique_ptr<const Logger> getDefaultLogger(const std::string &,
0161                                                const Logging::Level &,
0162                                                std::ostream *);
0163 }
0164 //! [Logger preload]