public function flush()
    {
        if (null == $this->buildLogFile) {
            $this->resetLogQueue();

            return;
        }
        $messageElements = array();
        for ($i = count($this->logQueue) - 1; $i >= 0; --$i) {
            $message = $this->logQueue[$i];
            $messageString = '<message priority="'.$message->priority.'" ';
            $messageString .= 'timestamp="'.$message->timestamp.'" ';
            $messageString .= 'time="'.date('Y-m-d H:i:s - T', $message->timestamp).'"><![CDATA[';
            $messageString .= base64_encode($message->message);
            $messageString .= ']]></message>';

            $messageElements[] = $messageString;
        }

        $previousLogMessages = '';

        $dirName = dirname($this->buildLogFile);
        if (!file_exists($dirName)) {
            mkdir($dirName, 0755, true);
        }
        if (file_exists($this->buildLogFile)) {
            // copying to temporary file for later inclusion via fgets, less memory consuming
            copy($this->buildLogFile, $this->buildLogFile.'.temp');
        }
        $fh = fopen($this->buildLogFile, 'w+');
        if (is_resource($fh)) {
            fputs($fh, '<?xml version="1.0"?>');
            fputs($fh, "\n");
            fputs($fh, '<build>');
            fputs($fh, "\n");
            fputs($fh, implode("\n", $messageElements));
            fputs($fh, "\n");
            if (file_exists($this->buildLogFile.'.temp')) {
                $fht = fopen($this->buildLogFile.'.temp', 'r');
                if (is_resource($fht)) {
                    $lineCounter = 0;
                    while ($line = fgets($fht)) {
                        // skip first two lines (xml decl and build opening element
                        if ($lineCounter > 2) {
                            fputs($fh, $line);
                        }
                        ++$lineCounter;
                    }
                    fclose($fht);
                    unlink($this->buildLogFile.'.temp');
                } else {
                    self::error('Cannot include previous log messages');
                }
            } else {
                fputs($fh, '</build>');
            }
            //fputs($fh, $previousLogMessages);
            fclose($fh);
            //file_put_contents($this->buildLogFile, $buildXml);
        } else {
            self::error('Cannot open: '.$this->buildLogFile.' for writing.');
        }
        $this->resetLogQueue();
    }