home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / Logging.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  6.6 KB  |  288 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. // Logging.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24.  
  25. #include "FileDlg.h"
  26. #include "Logging.h"
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // Logging dialog
  36.  
  37. Logging *Logging::instance = NULL;
  38. CString Logging::text;
  39.  
  40. Logging::Logging(CWnd* pParent /*=NULL*/)
  41.   : ResizeDlg(Logging::IDD, pParent)
  42. {
  43.   //{{AFX_DATA_INIT(Logging)
  44.   m_swi = FALSE;
  45.   m_unaligned_access = FALSE;
  46.   m_illegal_write = FALSE;
  47.   m_illegal_read = FALSE;
  48.   m_dma0 = FALSE;
  49.   m_dma1 = FALSE;
  50.   m_dma2 = FALSE;
  51.   m_dma3 = FALSE;
  52.   m_agbprint = FALSE;
  53.   m_undefined = FALSE;
  54.   //}}AFX_DATA_INIT
  55. }
  56.  
  57.  
  58. void Logging::DoDataExchange(CDataExchange* pDX)
  59. {
  60.   CDialog::DoDataExchange(pDX);
  61.   //{{AFX_DATA_MAP(Logging)
  62.   DDX_Control(pDX, IDC_LOG, m_log);
  63.   DDX_Check(pDX, IDC_VERBOSE_SWI, m_swi);
  64.   DDX_Check(pDX, IDC_VERBOSE_UNALIGNED_ACCESS, m_unaligned_access);
  65.   DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_WRITE, m_illegal_write);
  66.   DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_READ, m_illegal_read);
  67.   DDX_Check(pDX, IDC_VERBOSE_DMA0, m_dma0);
  68.   DDX_Check(pDX, IDC_VERBOSE_DMA1, m_dma1);
  69.   DDX_Check(pDX, IDC_VERBOSE_DMA2, m_dma2);
  70.   DDX_Check(pDX, IDC_VERBOSE_DMA3, m_dma3);
  71.   DDX_Check(pDX, IDC_VERBOSE_AGBPRINT, m_agbprint);
  72.   DDX_Check(pDX, IDC_VERBOSE_UNDEFINED, m_undefined);
  73.   //}}AFX_DATA_MAP
  74. }
  75.  
  76.  
  77. BEGIN_MESSAGE_MAP(Logging, CDialog)
  78.   //{{AFX_MSG_MAP(Logging)
  79.   ON_BN_CLICKED(ID_OK, OnOk)
  80.   ON_BN_CLICKED(IDC_CLEAR, OnClear)
  81.   ON_BN_CLICKED(IDC_VERBOSE_AGBPRINT, OnVerboseAgbprint)
  82.   ON_BN_CLICKED(IDC_VERBOSE_DMA0, OnVerboseDma0)
  83.   ON_BN_CLICKED(IDC_VERBOSE_DMA1, OnVerboseDma1)
  84.   ON_BN_CLICKED(IDC_VERBOSE_DMA2, OnVerboseDma2)
  85.   ON_BN_CLICKED(IDC_VERBOSE_DMA3, OnVerboseDma3)
  86.   ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_READ, OnVerboseIllegalRead)
  87.   ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_WRITE, OnVerboseIllegalWrite)
  88.   ON_BN_CLICKED(IDC_VERBOSE_SWI, OnVerboseSwi)
  89.   ON_BN_CLICKED(IDC_VERBOSE_UNALIGNED_ACCESS, OnVerboseUnalignedAccess)
  90.   ON_BN_CLICKED(IDC_VERBOSE_UNDEFINED, OnVerboseUndefined)
  91.   ON_BN_CLICKED(IDC_SAVE, OnSave)
  92.   ON_EN_ERRSPACE(IDC_LOG, OnErrspaceLog)
  93.   ON_EN_MAXTEXT(IDC_LOG, OnMaxtextLog)
  94.   ON_WM_CLOSE()
  95.   //}}AFX_MSG_MAP
  96.   END_MESSAGE_MAP()
  97.  
  98.   /////////////////////////////////////////////////////////////////////////////
  99. // Logging message handlers
  100.  
  101. void Logging::OnOk() 
  102. {
  103.   EndDialog(TRUE);
  104.  
  105.   instance = NULL;
  106. }
  107.  
  108. void Logging::OnClear() 
  109. {
  110.   text = "";
  111.   m_log.SetWindowText("");
  112. }
  113.  
  114. void Logging::OnVerboseAgbprint() 
  115. {
  116.   systemVerbose ^= 512;
  117. }
  118.  
  119. void Logging::OnVerboseDma0() 
  120. {
  121.   systemVerbose ^= 16;
  122. }
  123.  
  124. void Logging::OnVerboseDma1() 
  125. {
  126.   systemVerbose ^= 32;
  127. }
  128.  
  129. void Logging::OnVerboseDma2() 
  130. {
  131.   systemVerbose ^= 64;
  132. }
  133.  
  134. void Logging::OnVerboseDma3() 
  135. {
  136.   systemVerbose ^= 128;
  137. }
  138.  
  139. void Logging::OnVerboseIllegalRead() 
  140. {
  141.   systemVerbose ^= 8;
  142. }
  143.  
  144. void Logging::OnVerboseIllegalWrite() 
  145. {
  146.   systemVerbose ^= 4;
  147. }
  148.  
  149. void Logging::OnVerboseSwi() 
  150. {
  151.   systemVerbose ^= 1;
  152. }
  153.  
  154. void Logging::OnVerboseUnalignedAccess() 
  155. {
  156.   systemVerbose ^= 2;
  157. }
  158.  
  159. void Logging::OnVerboseUndefined() 
  160. {
  161.   systemVerbose ^= 256;
  162. }
  163.  
  164. void Logging::OnSave() 
  165. {
  166.   int len = m_log.GetWindowTextLength();
  167.  
  168.   char *mem = (char *)malloc(len);
  169.  
  170.   if(mem) {
  171.     LPCTSTR exts[] = { ".txt" };
  172.     m_log.GetWindowText(mem, len);
  173.     CString filter = "All Files|*.*||";
  174.     FileDlg dlg(this, "", filter, 0,
  175.                 NULL, exts, NULL, "Save output", true);
  176.  
  177.     if(dlg.DoModal() == IDOK) {
  178.       FILE *f = fopen(dlg.GetPathName(), "w");
  179.       if(f) {
  180.         fwrite(mem, 1, len, f);
  181.         fclose(f);
  182.       }
  183.     }
  184.   }
  185.  
  186.   free(mem);
  187. }
  188.  
  189. void Logging::OnErrspaceLog() 
  190. {
  191.   systemMessage(0, "Error allocating space");
  192. }
  193.  
  194. void Logging::OnMaxtextLog() 
  195. {
  196.   systemMessage(0, "Max text length reached %d", m_log.GetLimitText());
  197. }
  198.  
  199. void Logging::PostNcDestroy() 
  200. {
  201.   delete this;
  202. }
  203.  
  204. BOOL Logging::OnInitDialog() 
  205. {
  206.   CDialog::OnInitDialog();
  207.   
  208.   DIALOG_SIZER_START( sz )
  209.     DIALOG_SIZER_ENTRY( IDC_LOG, DS_SizeY|DS_SizeX)
  210.     DIALOG_SIZER_ENTRY( ID_OK, DS_MoveY)
  211.     DIALOG_SIZER_ENTRY( IDC_CLEAR, DS_MoveY)
  212.     DIALOG_SIZER_ENTRY( IDC_SAVE, DS_MoveY)
  213.     DIALOG_SIZER_END()
  214.     SetData(sz,
  215.             TRUE,
  216.             HKEY_CURRENT_USER,
  217.             "Software\\Emulators\\VisualBoyAdvance\\Viewer\\LogView",
  218.             NULL);
  219.   m_swi = (systemVerbose & 1) != 0;
  220.   m_unaligned_access = (systemVerbose & 2) != 0;
  221.   m_illegal_write = (systemVerbose & 4) != 0;
  222.   m_illegal_read = (systemVerbose & 8) != 0;
  223.   m_dma0 = (systemVerbose & 16) != 0;
  224.   m_dma1 = (systemVerbose & 32) != 0;
  225.   m_dma2 = (systemVerbose & 64) != 0;
  226.   m_dma3 = (systemVerbose & 128) != 0;
  227.   m_undefined = (systemVerbose & 256) != 0;
  228.   m_agbprint = (systemVerbose & 256) != 0;
  229.   UpdateData(FALSE);
  230.  
  231.   m_log.LimitText(-1);
  232.   m_log.SetWindowText(text);
  233.   
  234.   return TRUE;  // return TRUE unless you set the focus to a control
  235.                 // EXCEPTION: OCX Property Pages should return FALSE
  236. }
  237.  
  238. void Logging::log(const char *s)
  239. {
  240.   int size = ::SendMessage(m_log, WM_GETTEXTLENGTH, 0, 0);
  241.   m_log.SetSel(size, size);
  242.   m_log.ReplaceSel(s);
  243. }
  244.  
  245. void Logging::OnClose() 
  246. {
  247.   EndDialog(FALSE);
  248.  
  249.   instance = NULL;
  250.   
  251.   CDialog::OnClose();
  252. }
  253.  
  254. void toolsLogging()
  255. {
  256.   if(Logging::instance == NULL) {
  257.     Logging::instance = new Logging();
  258.     Logging::instance->Create(IDD_LOGGING, AfxGetApp()->m_pMainWnd);
  259.     Logging::instance->ShowWindow(SW_SHOW);
  260.   } else {
  261.     Logging::instance->SetForegroundWindow();
  262.   }
  263. }
  264.  
  265. void toolsLog(const char *s)
  266. {
  267.   CString str;
  268.   int state = 0;
  269.   if(s) {
  270.     char c = *s++;
  271.     while(c) {
  272.       if(c == '\n' && state == 0)
  273.         str += '\r';
  274.       else if(c == '\r')
  275.         state = 1;
  276.       else
  277.         state = 0;
  278.       str += c;
  279.       c = *s++;
  280.     }
  281.   }
  282.  
  283.   Logging::text += str;
  284.   if(Logging::instance != NULL) {
  285.     Logging::instance->log(str);
  286.   }
  287. }
  288.