Link to home
Create AccountLog in
Avatar of Rmocherla
Rmocherla

asked on

Can Windows "Binary Circular File" log file Format be used in a VC++ program?

Is there an easy way to implement the Windows log file fomat "Binary Circular File" without having to code it from scratch?
The proposed application also maintains a log file.

Many thanks



ASKER CERTIFIED SOLUTION
Avatar of grg99
grg99

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of jkr
Hmm, coding that format from scratch is not a big deal, e.g.

class BinaryCircularLog {

struct BCL_header {

    unsigned int nRecordSize;
    unsigned int nLastEntry;
    unsigned int nMaxRecords;
};

public:

    BinaryCircularLog ( LPCSTR pszName, unsigned int nRecordSize = 0, unsigned int nMaxRecords = 0) {

        DWORD dwOpenMode = nRecordSize ? CREATE_NEW : OPEN_EXISTING;

        m_hFile = CreateFile ( pszName, GENERIC_ALL, 0, NULL, dwOpenMode, 0, NULL);

        if ( CREATE_NEW == dwOpenMode) { // create new, write header

            m_hdr.nRecordSize = nRecordSize;
            m_hdr.nLastEntry = 0;
            m_hdr.nMaxRecords = nMaxRecords;

            DWORD dwWritten;

            WriteFile ( m_hFile, &m_hdr, sizeof ( BCL_header), &dwWritten, NULL);

        } else { // open existing, read header

            DWORD dwRead;

            ReadFile ( m_hFile, &m_hdr, sizeof ( BCL_header), &dwRead, NULL);
        }
    }
    ~BinaryCircularLog () {

        CloseHandle ( m_hFile);
    }
    bool AddEntry ( BYTE* pbRecord) {

        if ( m_hdr.nLastEntry == m_hdr.nMaxRecords) { // full, wrap around

            m_hdr.nLastEntry = 0;
        }

        DWORD dwWritten;

        SetFilePointer ( m_hFile, sizeof ( BCL_header) + m_hdr.nLastEntry * m_hdr.nRecordSize, FILE_BEGIN);

        WriteFile ( m_hFile, pbRecord, m_hdr.nRecordSize, &dwWritten, NULL);

        if ( dwWritten != m_hdr.nRecordSize) return false;

        // update header
        SetFilePointer ( m_hFile, 0, FILE_BEGIN);

        WriteFile ( m_hFile, &m_hdr, sizeof ( BCL_header), &dwWritten, NULL);

        if ( dwWritten != m_hdr.nRecordSize) return false;

        return true;
    }

protected:

    HANDLE m_hFile;
    BCL_header m_hdr;
};
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Objection: I provided the whole implementation. A split is OK with me, but more like 1:3 than 1:1
modulo - does *anybody* read the comments we're supposed to leave?
:-(