|
|
|||
File indexing completed on 2025-12-16 10:12:02
0001 //------------------------------- -*- C++ -*- -------------------------------// 0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details 0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT) 0004 //---------------------------------------------------------------------------// 0005 //! \file corecel/io/ScopedTimeLog.hh 0006 //---------------------------------------------------------------------------// 0007 #pragma once 0008 0009 #include "corecel/Assert.hh" 0010 #include "corecel/Macros.hh" 0011 #include "corecel/sys/Stopwatch.hh" 0012 0013 #include "ColorUtils.hh" 0014 #include "Logger.hh" 0015 0016 namespace celeritas 0017 { 0018 //---------------------------------------------------------------------------// 0019 /*! 0020 * Print the elapsed time since construction when destructed. 0021 * 0022 * An optional construction argument specifies the minimum time needed to 0023 * bother printing. 0024 * \code 0025 { 0026 CELER_LOG(info) << "Doing something expensive"; 0027 ScopedTimeLog scoped_time; 0028 do_something_expensive(); 0029 } 0030 \endcode 0031 */ 0032 class ScopedTimeLog 0033 { 0034 public: 0035 // Construct with default threshold of 0.01 seconds 0036 inline ScopedTimeLog() = default; 0037 0038 // Construct with a reference to a particular logger (e.g. thread-local) 0039 explicit inline ScopedTimeLog(Logger* dest); 0040 0041 // Construct with manual threshold for printing time 0042 explicit inline ScopedTimeLog(double min_print_sec); 0043 0044 // Construct with logger and time threshold 0045 inline ScopedTimeLog(Logger* dest, double min_print_sec); 0046 0047 // Print on destruction 0048 inline ~ScopedTimeLog(); 0049 0050 //!@{ 0051 //! Prevent copying and moving for RAII class 0052 CELER_DELETE_COPY_MOVE(ScopedTimeLog); 0053 //!@} 0054 0055 private: 0056 Logger* logger_{nullptr}; 0057 double min_print_sec_{0.01}; 0058 Stopwatch get_time_; 0059 }; 0060 0061 //---------------------------------------------------------------------------// 0062 /*! 0063 * Construct with a manual threshold for printing time. 0064 */ 0065 ScopedTimeLog::ScopedTimeLog(double min_print_sec) 0066 : min_print_sec_(min_print_sec) 0067 { 0068 CELER_EXPECT(min_print_sec >= 0); 0069 } 0070 0071 //---------------------------------------------------------------------------// 0072 /*! 0073 * Construct with a reference to a particular logger. 0074 */ 0075 ScopedTimeLog::ScopedTimeLog(Logger* dest) : logger_(dest) 0076 { 0077 CELER_EXPECT(logger_); 0078 } 0079 0080 //---------------------------------------------------------------------------// 0081 /*! 0082 * Construct with a reference to a particular logger. 0083 */ 0084 ScopedTimeLog::ScopedTimeLog(Logger* dest, double min_print_sec) 0085 : logger_(dest), min_print_sec_(min_print_sec) 0086 { 0087 CELER_EXPECT(logger_); 0088 CELER_EXPECT(min_print_sec >= 0); 0089 } 0090 0091 //---------------------------------------------------------------------------// 0092 /*! 0093 * Print large enough times when exiting scope. 0094 */ 0095 ScopedTimeLog::~ScopedTimeLog() 0096 { 0097 double time_sec = get_time_(); 0098 if (time_sec > min_print_sec_) 0099 { 0100 using celeritas::color_code; 0101 auto msg = [this] { 0102 if (!logger_) 0103 { 0104 return CELER_LOG(diagnostic); 0105 } 0106 return (*logger_)(CELER_CODE_PROVENANCE, LogLevel::diagnostic); 0107 }(); 0108 msg << color_code('x') << "... " << time_sec << " s" << color_code(' '); 0109 } 0110 } 0111 0112 //---------------------------------------------------------------------------// 0113 } // namespace celeritas
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|