/*----------------------------------------------------------------------
|   NPT_LogTcpHandler::Log
+---------------------------------------------------------------------*/
void
NPT_LogTcpHandler::Log(const NPT_LogRecord& record)
{
    // ensure we're connected
    if (m_Stream.IsNull()) {
        if (NPT_FAILED(Connect())) return;
    }

    // format the record
    NPT_String msg;
    FormatRecord(record, msg);
    
    // log, and disconnect if this fails
    if (NPT_FAILED(m_Stream->WriteString(msg))) {
        m_Stream = NULL;
    }
}
/*----------------------------------------------------------------------
|   NPT_LogFileHandler::Log
+---------------------------------------------------------------------*/
void
NPT_LogFileHandler::Log(const NPT_LogRecord& record)
{
    if (m_MaxFilesize > 0) {
        /* get current file size */
        NPT_LargeSize size;
        NPT_File::GetSize(m_Filename, size);

        /* time to recycle ? */
        if (size > m_MaxFilesize) {
            /* release stream to force a reopen later 
               and to be able to rename file */
            m_Stream = NULL;

            /* rename file using current time */
            NPT_TimeStamp now;
            NPT_System::GetCurrentTimeStamp(now);
            NPT_String suffix = NPT_DateTime(now, true).ToString(NPT_DateTime::FORMAT_W3C);
            suffix.Replace(':', '_');
            NPT_String new_name = NPT_FilePath::Create(
                NPT_FilePath::DirName(m_Filename),
                NPT_FilePath::BaseName(m_Filename, false) + 
                    "-" + 
                    suffix + 
                    NPT_FilePath::FileExtension(m_Filename));

            NPT_File::Rename(m_Filename, new_name);
        }
    }
    
    /* try to reopen the file if it failed to open 
       previously or if we rotated it */
    if (m_Stream.IsNull()) {
        Open(m_Append);
    }
    
    if (m_Stream.AsPointer()) {
        NPT_Log::FormatRecordToStream(record, *m_Stream, false, m_FormatFilter);
        if (m_Flush) m_Stream->Flush();
    }
}