Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file defines a trait to assist with logging in tasks.
  19   *
  20   * @package    core
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\task;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * This trait includes functions to assist with logging in tasks.
  31   *
  32   * @package    core
  33   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  trait logging_trait {
  37  
  38      /**
  39       * @var \progress_trace
  40       */
  41      protected $trace = null;
  42  
  43      /**
  44       * @var \stdClass
  45       */
  46      protected $tracestats = null;
  47  
  48      /**
  49       * Get the progress_trace.
  50       *
  51       * @return  \progress_trace
  52       */
  53      protected function get_trace() {
  54          if (null === $this->trace) {
  55              $this->trace = new \text_progress_trace();
  56              $this->tracestats = new \stdClass();
  57          }
  58  
  59          return $this->trace;
  60      }
  61  
  62      /**
  63       * Log a message to the progress tracer.
  64       *
  65       * @param   string  $message
  66       * @param   int     $depth
  67       */
  68      protected function log($message, $depth = 1) {
  69          $this->get_trace()
  70              ->output($message, $depth);
  71      }
  72  
  73      /**
  74       * Log a start message to the progress tracer.
  75       *
  76       * @param   string  $message
  77       * @param   int     $depth
  78       */
  79      protected function log_start($message, $depth = 0) {
  80          $this->log($message, $depth);
  81  
  82          if (defined('MDL_PERFTOLOG') && MDL_PERFTOLOG) {
  83              $this->tracestats->$depth = [
  84                  'mem' => memory_get_usage(),
  85                  'time' => microtime(),
  86              ];
  87          }
  88      }
  89  
  90      /**
  91       * Log an end message to the progress tracer.
  92       *
  93       * @param   string  $message
  94       * @param   int     $depth
  95       */
  96      protected function log_finish($message, $depth = 0) {
  97          $this->log($message, $depth);
  98  
  99          if (isset($this->tracestats->$depth)) {
 100              $startstats = $this->tracestats->$depth;
 101              $this->log(
 102                      sprintf("Time taken %s, memory total: %s, Memory growth: %s, Memory peak: %s",
 103                          microtime_diff($startstats['time'], microtime()),
 104                          display_size(memory_get_usage()),
 105                          display_size(memory_get_usage() - $startstats['mem']),
 106                          display_size(memory_get_peak_usage())
 107                      ),
 108                      $depth + 1
 109                  );
 110          }
 111      }
 112  }