Click here to Skip to main content
15,887,365 members
Articles / Desktop Programming / MFC

A Realtime Logfile Viewer

Rate me:
Please Sign up or sign in to vote.
4.98/5 (39 votes)
24 Feb 2004CPOL20 min read 234K   4.9K   151  
Part two of the logging service - the viewer
/*
 *	$Header :$
 *
 *	$History: LoadLogFilesDlg.cpp $
 * 
 * *****************  Version 2  *****************
 * User: Administrator Date: 12/02/03   Time: 9:20p
 * Updated in $/logger/logviewer
 * Added pretty icons, version resource, tooltip support, fixed various
 * bugs in continuation record support.
 * 
 * *****************  Version 1  *****************
 * User: Administrator Date: 11/25/03   Time: 5:20p
 * Created in $/logger/logviewer
 */
#include "stdafx.h"
#include "logviewer.h"
#include "LoadLogFilesDlg.h"
#include "..\common\constants.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLoadLogFilesDlg
IMPLEMENT_DYNAMIC(CLoadLogFilesDlg, CFileDialog)

CLoadLogFilesDlg::CLoadLogFilesDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) : CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
	m_ofn.lStructSize = sizeof(OPENFILENAME);
	m_ofn.Flags |= OFN_ENABLETEMPLATE | OFN_ALLOWMULTISELECT;
	m_ofn.hInstance = AfxGetInstanceHandle();
	m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_LOADLOGS);

	m_ofn.lpstrFile = new TCHAR[10000];
	m_ofn.nMaxFile = 10000;
	memset(m_ofn.lpstrFile, 0, 10000);
    m_retCode = IDCANCEL;
}

CLoadLogFilesDlg::~CLoadLogFilesDlg()
{
	delete m_ofn.lpstrFile;
}

void CLoadLogFilesDlg::DoDataExchange(CDataExchange* pDX)
{
	CFileDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

int CLoadLogFilesDlg::DoModal()
{
    int		retCode = CFileDialog::DoModal();
	CString csTemp;
    
    if (retCode == IDOK)
    {
        POSITION pos = GetStartPosition();

        while (pos != POSITION(NULL))
            m_saFileList.Add(GetNextPathName(pos));
    }

    return m_saFileList.GetSize() ? IDOK : IDCANCEL;
}

void CLoadLogFilesDlg::CreateFileList(BOOL bFiltered)
{
	CString csPattern;

	csPattern.Format(_T("%s\\*.log"), m_ofn.lpstrInitialDir);

	CFileFind ff;
	BOOL	  bWorking = ff.FindFile(csPattern);

	while (bWorking)
	{
		bWorking = ff.FindNextFile();

        if (bFiltered)
        {
            //  If we're filtering it means we want only todays files.
            //  So we get the second field in the filename delimited by
            //  a dot and check if it's log.  If so it's todays file
			if (!IsAged(ff.GetFilePath()))
		        m_saFileList.Add(ff.GetFilePath());
        }
        else
            m_saFileList.Add(ff.GetFilePath());
	}

    m_retCode = IDOK;
    GetParent()->PostMessage(WM_COMMAND, IDCANCEL);
}

BOOL CLoadLogFilesDlg::IsAged(LPCTSTR szFilePath)
{
	ASSERT(szFilePath);
	ASSERT(AfxIsValidString(szFilePath));

	CString csTemp = szFilePath;
	TCHAR	*tszTemp;

	_tcstok(LPTSTR(LPCTSTR(csTemp)), _T("."));
	tszTemp = _tcstok(NULL, _T("."));

    if (tszTemp != (TCHAR *) NULL && _tcsicmp(tszTemp, _T("log")) == 0)
		return FALSE;

	return TRUE;
}

BEGIN_MESSAGE_MAP(CLoadLogFilesDlg, CFileDialog)
	//{{AFX_MSG_MAP(CLoadLogFilesDlg)
	ON_COMMAND(IDC_LOADALL, OnLoadAll)
	ON_COMMAND(IDC_LOADTODAY, OnLoadToday)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CLoadLogFilesDlg::OnLoadAll()
{
    CreateFileList(FALSE);
}

void CLoadLogFilesDlg::OnLoadToday()
{
    CreateFileList(TRUE);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been programming for 35 years - started in machine language on the National Semiconductor SC/MP chip, moved via the 8080 to the Z80 - graduated through HP Rocky Mountain Basic and HPL - then to C and C++ and now C#.

I used (30 or so years ago when I worked for Hewlett Packard) to repair HP Oscilloscopes and Spectrum Analysers - for a while there I was the one repairing DC to daylight SpecAns in the Asia Pacific area.

Afterward I was the fourth team member added to the Australia Post EPOS project at Unisys Australia. We grew to become an A$400 million project. I wrote a few device drivers for the project under Microsoft OS/2 v 1.3 - did hardware qualification and was part of the rollout team dealing directly with the customer.

Born and bred in Melbourne Australia, now living in Scottsdale Arizona USA, became a US Citizen on September 29th, 2006.

I work for a medical insurance broker, learning how to create ASP.NET websites in VB.Net and C#. It's all good.

Oh, I'm also a Kentucky Colonel. http://www.kycolonels.org

Comments and Discussions