      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    <?php
    namespace Catapult;
    /**
     * Built in logger for Catapult SDK
     * will be used on exceptions, folders generated
     * relative to working directory.
     *
     * NOTE if you are using ./logs for something else
     * you can reset the directory using
     *
     * Catapult\Log::directory('new_directory');
     *
     * To set off/on the logger
     *
     * Catapult\Log::on(TRUE);
     *
     * example output:
     * /logs/catapult_log_12-12-2014-2:05.log
     */

    final class Log {

        public static $on = TRUE;
        public static $log_file;

        /**
         * General purpose options
         * for logger
         */
        public static $logger_opts = array(
          "path" => __DEFAULT_LOG_USER_PATH__,
          "mode" => "a" // usually append unless needed for read only
        );

        /**
         * close the open logger
         *
         */
        public static function close()
        {
          return fclose(self::$log_file);
        }

        /**
         * can we find a member
         * for this log.
         * match by Y-M-D
         *
         */
        public static function find()
        {
          $files = scandir(self::$logger_opts['path']);

          $current = new \DateTime();
          $current->setTimestamp(time());
          $term = __DEFAULT_LOG_PREFIX__ . $current->format("Y-M-D");

          foreach ($files as $f) {
            $matches = array();
            preg_match("/^$term.*$/", $f, $matches);

            if (sizeof($matches) > 0) {
              return $f;
            }
          }

          return FALSE;
        }

       /**
        * open the current
        * log. Set by day.
        * If we cannot open a folder,
        * file set catapult logging off
        *
        * by default this will output in the current
        * directory. If there is a logs directory in the
        * folder, it will use that
        */
        public static function open()
        {
          $date_of_log = new \DateTime();

          $date_of_log->setTimestamp(time());

          try {

            if (!(is_dir(self::$logger_opts['path']))) {
              mkdir(self::$logger_opts['path']);
            }

            if (($pfile = self::find())) {
              $file = realpath(self::$logger_opts['path']) . DIRECTORY_SEPARATOR  . $pfile;
            } else {
              $file = realpath(self::$logger_opts['path']) . DIRECTORY_SEPARATOR  . __DEFAULT_LOG_PREFIX__ . $date_of_log->format("Y-M-D-h-i-s") . ".log";
            }

            self::$log_file = fopen($file, self::$logger_opts['mode']);

          } catch (\Exception $e) {

            return self::on(FALSE);

          }
        }

        /**
         * Write to the log
         * accept a date, object and format
           *
           * Format: {file} - {app_id} [{time}] "{object}" {message}
         * @param time: unix timestamp
         * @param object: Catapult Model Object
         * @param file:
         */
        public static function write($time='', $object='CALL', $message='')
        {
          if (!(self::$on)) {
            return FALSE;
          }

          if (self::$log_file || !isset(self::$log_file)) {
            self::open();
          }

          /** who was the logger called from if we are running on a server **/
          /** this is easy with $_SERVER['PHP_SELF'], if running independatly, **/
          /** we need introspection **/
          if (isset($_SERVER['PHP_SELF'])) {
            $file = realpath($_SERVER['PHP_SELF']);
          } else {
            /** to implement **/
            /** show directory until optimal introspection is found **/
            $file = realpath(__DIR__);
          }

          if (is_int($time)) {
            $date = new \DateTime;
            $date->setTimestamp($time);
            $time = $date->format("M/m/y:H:i:s");
          }

          $cli = Client::get();

          $fulltext = "$file ($object) - APPLICATION:" . $cli->application_id . " [$time] - $message" . "\n";

          fwrite(self::$log_file, $fulltext);
        }

        public static function isOn()
        {
          return self::$on;
        }

        /**
         * turn on/off logging
         * @param on: TRUE|FALSE
         */
        public static function on($on=TRUE)
        {
          return self::$on = $on;
        }
      }