diff src/win32/Logging.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/Logging.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,273 @@
     1.4 +// Logging.cpp : implementation file
     1.5 +//
     1.6 +
     1.7 +#include "stdafx.h"
     1.8 +#include "resource.h"
     1.9 +#include "Logging.h"
    1.10 +#include "FileDlg.h"
    1.11 +
    1.12 +#include "../common/System.h"
    1.13 +
    1.14 +/////////////////////////////////////////////////////////////////////////////
    1.15 +// Logging dialog
    1.16 +
    1.17 +Logging *Logging::instance = NULL;
    1.18 +CString Logging:: text;
    1.19 +
    1.20 +Logging::Logging(CWnd*pParent /*=NULL*/)
    1.21 +	: ResizeDlg(Logging::IDD, pParent)
    1.22 +{
    1.23 +	//{{AFX_DATA_INIT(Logging)
    1.24 +	m_swi = FALSE;
    1.25 +	m_unaligned_access = FALSE;
    1.26 +	m_illegal_write    = FALSE;
    1.27 +	m_illegal_read     = FALSE;
    1.28 +	m_dma0      = FALSE;
    1.29 +	m_dma1      = FALSE;
    1.30 +	m_dma2      = FALSE;
    1.31 +	m_dma3      = FALSE;
    1.32 +	m_agbprint  = FALSE;
    1.33 +	m_undefined = FALSE;
    1.34 +	//}}AFX_DATA_INIT
    1.35 +}
    1.36 +
    1.37 +void Logging::DoDataExchange(CDataExchange*pDX)
    1.38 +{
    1.39 +	CDialog::DoDataExchange(pDX);
    1.40 +	//{{AFX_DATA_MAP(Logging)
    1.41 +	DDX_Control(pDX, IDC_LOG, m_log);
    1.42 +	DDX_Check(pDX, IDC_VERBOSE_SWI, m_swi);
    1.43 +	DDX_Check(pDX, IDC_VERBOSE_UNALIGNED_ACCESS, m_unaligned_access);
    1.44 +	DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_WRITE, m_illegal_write);
    1.45 +	DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_READ, m_illegal_read);
    1.46 +	DDX_Check(pDX, IDC_VERBOSE_DMA0, m_dma0);
    1.47 +	DDX_Check(pDX, IDC_VERBOSE_DMA1, m_dma1);
    1.48 +	DDX_Check(pDX, IDC_VERBOSE_DMA2, m_dma2);
    1.49 +	DDX_Check(pDX, IDC_VERBOSE_DMA3, m_dma3);
    1.50 +	DDX_Check(pDX, IDC_VERBOSE_AGBPRINT, m_agbprint);
    1.51 +	DDX_Check(pDX, IDC_VERBOSE_UNDEFINED, m_undefined);
    1.52 +	//}}AFX_DATA_MAP
    1.53 +}
    1.54 +
    1.55 +BEGIN_MESSAGE_MAP(Logging, CDialog)
    1.56 +//{{AFX_MSG_MAP(Logging)
    1.57 +ON_BN_CLICKED(ID_OK, OnOk)
    1.58 +ON_BN_CLICKED(IDC_CLEAR, OnClear)
    1.59 +ON_BN_CLICKED(IDC_VERBOSE_AGBPRINT, OnVerboseAgbprint)
    1.60 +ON_BN_CLICKED(IDC_VERBOSE_DMA0, OnVerboseDma0)
    1.61 +ON_BN_CLICKED(IDC_VERBOSE_DMA1, OnVerboseDma1)
    1.62 +ON_BN_CLICKED(IDC_VERBOSE_DMA2, OnVerboseDma2)
    1.63 +ON_BN_CLICKED(IDC_VERBOSE_DMA3, OnVerboseDma3)
    1.64 +ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_READ, OnVerboseIllegalRead)
    1.65 +ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_WRITE, OnVerboseIllegalWrite)
    1.66 +ON_BN_CLICKED(IDC_VERBOSE_SWI, OnVerboseSwi)
    1.67 +ON_BN_CLICKED(IDC_VERBOSE_UNALIGNED_ACCESS, OnVerboseUnalignedAccess)
    1.68 +ON_BN_CLICKED(IDC_VERBOSE_UNDEFINED, OnVerboseUndefined)
    1.69 +ON_BN_CLICKED(IDC_SAVE, OnSave)
    1.70 +ON_EN_ERRSPACE(IDC_LOG, OnErrspaceLog)
    1.71 +ON_EN_MAXTEXT(IDC_LOG, OnMaxtextLog)
    1.72 +ON_WM_CLOSE()
    1.73 +//}}AFX_MSG_MAP
    1.74 +END_MESSAGE_MAP()
    1.75 +
    1.76 +/////////////////////////////////////////////////////////////////////////////
    1.77 +// Logging message handlers
    1.78 +
    1.79 +void Logging::OnOk()
    1.80 +{
    1.81 +	EndDialog(TRUE);
    1.82 +
    1.83 +	instance = NULL;
    1.84 +}
    1.85 +
    1.86 +void Logging::OnClear()
    1.87 +{
    1.88 +	text = "";
    1.89 +	m_log.SetWindowText("");
    1.90 +}
    1.91 +
    1.92 +void Logging::OnVerboseAgbprint()
    1.93 +{
    1.94 +	systemVerbose ^= 512;
    1.95 +}
    1.96 +
    1.97 +void Logging::OnVerboseDma0()
    1.98 +{
    1.99 +	systemVerbose ^= 16;
   1.100 +}
   1.101 +
   1.102 +void Logging::OnVerboseDma1()
   1.103 +{
   1.104 +	systemVerbose ^= 32;
   1.105 +}
   1.106 +
   1.107 +void Logging::OnVerboseDma2()
   1.108 +{
   1.109 +	systemVerbose ^= 64;
   1.110 +}
   1.111 +
   1.112 +void Logging::OnVerboseDma3()
   1.113 +{
   1.114 +	systemVerbose ^= 128;
   1.115 +}
   1.116 +
   1.117 +void Logging::OnVerboseIllegalRead()
   1.118 +{
   1.119 +	systemVerbose ^= 8;
   1.120 +}
   1.121 +
   1.122 +void Logging::OnVerboseIllegalWrite()
   1.123 +{
   1.124 +	systemVerbose ^= 4;
   1.125 +}
   1.126 +
   1.127 +void Logging::OnVerboseSwi()
   1.128 +{
   1.129 +	systemVerbose ^= 1;
   1.130 +}
   1.131 +
   1.132 +void Logging::OnVerboseUnalignedAccess()
   1.133 +{
   1.134 +	systemVerbose ^= 2;
   1.135 +}
   1.136 +
   1.137 +void Logging::OnVerboseUndefined()
   1.138 +{
   1.139 +	systemVerbose ^= 256;
   1.140 +}
   1.141 +
   1.142 +void Logging::OnSave()
   1.143 +{
   1.144 +	int len = m_log.GetWindowTextLength();
   1.145 +
   1.146 +	char *mem = (char *)malloc(len);
   1.147 +
   1.148 +	if (mem)
   1.149 +	{
   1.150 +		LPCTSTR exts[] = { ".txt", NULL };
   1.151 +		m_log.GetWindowText(mem, len);
   1.152 +		CString filter = "All Files|*.*||";
   1.153 +		FileDlg dlg(this, "", filter, 0,
   1.154 +		            NULL, exts, NULL, "Save output", true);
   1.155 +
   1.156 +		if (dlg.DoModal() == IDOK)
   1.157 +		{
   1.158 +			FILE *f = fopen(dlg.GetPathName(), "w");
   1.159 +			if (f)
   1.160 +			{
   1.161 +				fwrite(mem, 1, len, f);
   1.162 +				fclose(f);
   1.163 +			}
   1.164 +		}
   1.165 +	}
   1.166 +
   1.167 +	free(mem);
   1.168 +}
   1.169 +
   1.170 +void Logging::OnErrspaceLog()
   1.171 +{
   1.172 +	systemMessage(0, "Error allocating space");
   1.173 +}
   1.174 +
   1.175 +void Logging::OnMaxtextLog()
   1.176 +{
   1.177 +	systemMessage(0, "Max text length reached %d", m_log.GetLimitText());
   1.178 +}
   1.179 +
   1.180 +void Logging::PostNcDestroy()
   1.181 +{
   1.182 +	delete this;
   1.183 +}
   1.184 +
   1.185 +BOOL Logging::OnInitDialog()
   1.186 +{
   1.187 +	CDialog::OnInitDialog();
   1.188 +
   1.189 +	DIALOG_SIZER_START(sz)
   1.190 +	DIALOG_SIZER_ENTRY(IDC_LOG, DS_SizeY|DS_SizeX)
   1.191 +	DIALOG_SIZER_ENTRY(ID_OK, DS_MoveY)
   1.192 +	DIALOG_SIZER_ENTRY(IDC_CLEAR, DS_MoveY)
   1.193 +	DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY)
   1.194 +	DIALOG_SIZER_END()
   1.195 +	SetData(sz,
   1.196 +	        TRUE,
   1.197 +	        HKEY_CURRENT_USER,
   1.198 +	        "Software\\Emulators\\VisualBoyAdvance\\Viewer\\LogView",
   1.199 +	        NULL);
   1.200 +
   1.201 +	m_swi = (systemVerbose & 1) != 0;
   1.202 +	m_unaligned_access = (systemVerbose & 2) != 0;
   1.203 +	m_illegal_write    = (systemVerbose & 4) != 0;
   1.204 +	m_illegal_read     = (systemVerbose & 8) != 0;
   1.205 +	m_dma0      = (systemVerbose & 16) != 0;
   1.206 +	m_dma1      = (systemVerbose & 32) != 0;
   1.207 +	m_dma2      = (systemVerbose & 64) != 0;
   1.208 +	m_dma3      = (systemVerbose & 128) != 0;
   1.209 +	m_undefined = (systemVerbose & 256) != 0;
   1.210 +	m_agbprint  = (systemVerbose & 256) != 0;
   1.211 +	UpdateData(FALSE);
   1.212 +
   1.213 +	m_log.LimitText(-1);
   1.214 +	m_log.SetWindowText(text);
   1.215 +
   1.216 +	return TRUE; // return TRUE unless you set the focus to a control
   1.217 +	             // EXCEPTION: OCX Property Pages should return FALSE
   1.218 +}
   1.219 +
   1.220 +void Logging::log(const char *s)
   1.221 +{
   1.222 +	int size = ::SendMessage(m_log, WM_GETTEXTLENGTH, 0, 0);
   1.223 +	m_log.SetSel(size, size);
   1.224 +	m_log.ReplaceSel(s);
   1.225 +}
   1.226 +
   1.227 +void Logging::OnClose()
   1.228 +{
   1.229 +	EndDialog(FALSE);
   1.230 +
   1.231 +	instance = NULL;
   1.232 +
   1.233 +	CDialog::OnClose();
   1.234 +}
   1.235 +
   1.236 +void toolsLogging()
   1.237 +{
   1.238 +	if (Logging::instance == NULL)
   1.239 +	{
   1.240 +		Logging::instance = new Logging();
   1.241 +		Logging::instance->Create(IDD_LOGGING, AfxGetApp()->m_pMainWnd);
   1.242 +		Logging::instance->ShowWindow(SW_SHOW);
   1.243 +	}
   1.244 +	else
   1.245 +	{
   1.246 +		Logging::instance->SetForegroundWindow();
   1.247 +	}
   1.248 +}
   1.249 +
   1.250 +void toolsLog(const char *s)
   1.251 +{
   1.252 +	CString str;
   1.253 +	int     state = 0;
   1.254 +	if (s)
   1.255 +	{
   1.256 +		char c = *s++;
   1.257 +		while (c)
   1.258 +		{
   1.259 +			if (c == '\n' && state == 0)
   1.260 +				str += '\r';
   1.261 +			else if (c == '\r')
   1.262 +				state = 1;
   1.263 +			else
   1.264 +				state = 0;
   1.265 +			str += c;
   1.266 +			c    = *s++;
   1.267 +		}
   1.268 +	}
   1.269 +
   1.270 +	Logging::text += str;
   1.271 +	if (Logging::instance != NULL)
   1.272 +	{
   1.273 +		Logging::instance->log(str);
   1.274 +	}
   1.275 +}
   1.276 +