<?
class LogOutput
{
    var $strFileName;
    /**输出log
     * @param strMSG log信息
     * @param strType log类型
     * @param strExtra 扩展信息
     * @param strFileName log的存放路径及文件名。默认为common/log下（在没有设置，或只设置文件名的情况下）
     * @return void
     * @exception Exception 无异常处理
    */
    function LogOutput($strMSG="",$strType="I",$strExtra="",$strFileName="")
    {
        $this->strFileName = $strFileName;
        if($strMSG)
            $this->out($strMSG,$strType,$strExtra);
    }

    function out($strMSG="",$strType="I",$strExtra="")
    {
        if($strType=="") $strType = "I";
        if($this->strFileName)
        {
            $pos = strrpos($this->strFileName,"/");
            if($pos !== false)
            {
                $logDir = substr($this->strFileName,0,$pos);
                $logName = substr($this->strFileName,$pos+1);
                if(!file_exists($logDir))
                {
                    $aryDirs=explode("/",substr($logDir,1));
                    $strDir="/";
                    foreach($aryDirs as $value)
                    {
                        $strDir.=$value."/";
                        if(!@file_exists($strDir))
                        {
                            if(!@mkdir($strDir,0777))
                            {
                                $return = "mkdirError";
                            }
                        }
                    }
                }
                $pos = strrpos($this->strFileName,".");
                if($pos === false)
                    $filename = $this->strFileName.date("ymd");
                else
                    $filename = substr($this->strFileName,0,$pos).date("ymd").substr($this->strFileName,$pos);
            }
            else
            {
                $strPath=dirname(__FILE__);
                if(!@file_exists($strPath."/log"))
                {
                    if(!@mkdir($strPath,0777))
                    {
                        $return = "mkdirError";
                    }
                }
                $pos = strrpos($this->strFileName,".");
                if($pos === false)
                    $filename = $this->strFileName.date("ymd");
                else
                    $filename = $strPath."/log/".substr($this->strFileName,0,$pos).date("ymd").substr($this->strFileName,$pos);
            }
        }
        else
        {
            $strPath=dirname(__FILE__);
            if(!@file_exists($strPath."/log"))
            {
                if(!@mkdir($strPath,0777))
                {
                    $return = "mkdirError";
                }
            }
            $filename = $strPath."/log/log_".date("ymd").".log";
        }
        if($return == "mkdirError")
            $filename = dirname(__FILE__)."/log_".date("ymd").".log";
        $handle = fopen($filename, "a");
        $strContent = "[".date("Y-m-d H:i:s")."] [".$strType."] [".$_SERVER["REMOTE_ADDR"]."] MSG:[".$strMSG."]".$strExtra." Location:".$_SERVER["SCRIPT_FILENAME"]." QUERY_STRING:".$_SERVER["QUERY_STRING"]." HTTP_REFERER:".$_SERVER["HTTP_REFERER"]."\n";
        if(!fwrite($handle, $strContent)){print "日志数据写入失败<Br>";exit;}
        fclose($handle);
    }
}
?>
<?
/**输出log out
 * @param strMSG log信息
 * @param strLevel log级别
 * @return void
 * @exception Exception 无异常处理
*/
function out($strMSG="",$strLevel="I",$strExtra="")
{
    if($strType=="") $strType = "I";
    $strPath=dirname(__FILE__);
    if(!@file_exists($strPath."/log"))
    {
        @mkdir($strPath."/log",0777);
    }
    $filename=$strPath."/log/log_".date("ymd").".log";
    $handle = fopen($filename, "a");
    $strContent = "[".date("Y-m-d H:i:s")."] [".$strType."] [".$_SERVER["REMOTE_ADDR"]."] MSG:[".$strMSG."]".$strExtra." Location:".$_SERVER["SCRIPT_FILENAME"]."\n";
    if(!fwrite($handle, $strContent)){print "日志数据写入失败<Br>";exit;}
    fclose($handle);
}
?>