<?php
/**
 * 短い説明
 * 
 * 長い説明
 * 
 * @package aowp
 */
/**
 * 短い説明
 * 
 * 長い説明
 * 
 * @package aowp
 */
class AOWP_Logger {
	const DEBUG_MODE = 1;
	const PERFORMANCE_TEST_MODE = 2;
	const RUNTIME_MODE = 0;
	
	const PERFORMANCE_TEST_INDEX = 1245;
	
	private static $START_TIME = array();
	public static $LOGGING_FLAG = AOWP_Logger::RUNTIME_MODE;
	
	public static function setStartTime($timeIndex = 0) {
		AOWP_Logger::$START_TIME[$timeIndex] = microtime(true);
	}
	public static function getExecutionTime($timeIndex = 0, $durationFlag = false) {
		$executionTime = (microtime(true) - AOWP_Logger::$START_TIME[$timeIndex]);
		if (!$durationFlag) {
			AOWP_Logger::$START_TIME[$timeIndex] = null;
		}
		return $executionTime;
	}
	
	public static function logging($message, $timeIndex = 0) {
		if (AOWP_Logger::$LOGGING_FLAG === AOWP_Logger::DEBUG_MODE) {
			if (isset(AOWP_Logger::$START_TIME[$timeIndex]) && AOWP_Logger::$START_TIME !== null) {
				$message = AOWP_Logger::getExecutionTime($timeIndex) . ' sec, ' . $message;
			}
			echo (memory_get_usage() / 1000 / 1000) . ' Mbytes, ' . $message . "\n";
		}
	}
	
	public static function startPerformanceTest() {
		AOWP_Logger::setStartTime(AOWP_Logger::PERFORMANCE_TEST_INDEX);
	}
	public static function performanceTestResult($durationFlag = false) {
		echo ('Peak memory: ' . memory_get_peak_usage() / 1000 / 1000) . ' Mbytes, Total time: ' . AOWP_Logger::getExecutionTime(AOWP_Logger::PERFORMANCE_TEST_INDEX, $durationFlag) . ' sec' . "\n";
	}
	
	public static function logToFile($message) {
		file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'log.txt', '[' . time() . '] ' . $message . "\n", FILE_APPEND);
	}
	
}
?>