Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
9.8 KiB

  1. // LogViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "wialogcfg.h"
  5. #include "LogViewer.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. static DWORD CALLBACK MyStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);
  12. CProgCtrl::CProgCtrl()
  13. {
  14. m_pProgressCtrl = NULL;
  15. }
  16. CProgCtrl::~CProgCtrl()
  17. {
  18. }
  19. void CProgCtrl::SetControl(CProgressCtrl *pProgressCtrl)
  20. {
  21. m_pProgressCtrl = pProgressCtrl;
  22. }
  23. void CProgCtrl::SetupProgressCtrl(PROGCTRL_SETUP_INFO *pSetupInfo)
  24. {
  25. m_pProgressCtrl->SetStep(pSetupInfo->iStepValue);
  26. m_pProgressCtrl->SetRange((short)pSetupInfo->iMinRange,(short)pSetupInfo->iMaxRange);
  27. m_MaxRange = pSetupInfo->iMaxRange;
  28. }
  29. void CProgCtrl::StepIt()
  30. {
  31. //TCHAR szBuffer[MAX_PATH];
  32. //sprintf(szBuffer,"Processing %d%",(m_pProgressCtrl->StepIt() * 100) / m_MaxRange);
  33. //m_pStaticText->SetWindowText(szBuffer);
  34. //m_pStaticText->Invalidate();
  35. m_pProgressCtrl->StepIt();
  36. }
  37. void CProgCtrl::DestroyME()
  38. {
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CLogViewer dialog
  42. CLogViewer::CLogViewer(CWnd* pParent /*=NULL*/)
  43. : CDialog(CLogViewer::IDD, pParent)
  44. {
  45. //{{AFX_DATA_INIT(CLogViewer)
  46. m_pProgDlg = NULL;
  47. m_bColorizeLog = FALSE;
  48. //}}AFX_DATA_INIT
  49. }
  50. void CLogViewer::DoDataExchange(CDataExchange* pDX)
  51. {
  52. CDialog::DoDataExchange(pDX);
  53. //{{AFX_DATA_MAP(CLogViewer)
  54. DDX_Control(pDX, IDC_RICHEDIT_LOGVIEWER, m_LogViewer);
  55. //}}AFX_DATA_MAP
  56. }
  57. BEGIN_MESSAGE_MAP(CLogViewer, CDialog)
  58. //{{AFX_MSG_MAP(CLogViewer)
  59. ON_WM_SIZE()
  60. ON_WM_SHOWWINDOW()
  61. //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CLogViewer message handlers
  65. BOOL CLogViewer::OnInitDialog()
  66. {
  67. CDialog::OnInitDialog();
  68. m_bKillInitialSelection = TRUE;
  69. //
  70. // Set FONT to fixed, for formatting reasons
  71. //
  72. HFONT hFixedFont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
  73. if(hFixedFont != NULL)
  74. m_LogViewer.SendMessage(WM_SETFONT,(WPARAM)hFixedFont,0);
  75. //
  76. // Get Windows Directory
  77. //
  78. TCHAR szLogFilePath[MAX_PATH];
  79. DWORD dwLength = 0;
  80. dwLength = ::GetWindowsDirectory(szLogFilePath,sizeof(szLogFilePath));
  81. if (( dwLength == 0) || !*szLogFilePath ) {
  82. OutputDebugString(TEXT("Could not GetWindowsDirectory()"));
  83. return TRUE;
  84. }
  85. //
  86. // Add log file name to Windows Directory
  87. //
  88. lstrcat(lstrcat(szLogFilePath,TEXT("\\")),TEXT("wiaservc.log"));
  89. // The file from which to load the contents of the rich edit control.
  90. CFile cFile(szLogFilePath, CFile::shareDenyNone|CFile::modeRead);
  91. EDITSTREAM es;
  92. es.dwCookie = (DWORD) (DWORD_PTR)&cFile;
  93. es.pfnCallback = MyStreamInCallback;
  94. m_LogViewer.StreamIn(SF_TEXT, es);
  95. UpdateData(TRUE);
  96. if(m_bColorizeLog)
  97. ParseLogToColor();
  98. return TRUE; // return TRUE unless you set the focus to a control
  99. // EXCEPTION: OCX Property Pages should return FALSE
  100. }
  101. static DWORD CALLBACK MyStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
  102. {
  103. CFile* pFile = (CFile*) dwCookie;
  104. *pcb = pFile->Read(pbBuff, cb);
  105. return 0;
  106. }
  107. void CLogViewer::OnSize(UINT nType, int cx, int cy)
  108. {
  109. CDialog::OnSize(nType, cx, cy);
  110. if(m_LogViewer.m_hWnd != NULL) {
  111. m_LogViewer.MoveWindow(0, 0, cx, cy);
  112. }
  113. }
  114. void CLogViewer::OnShowWindow(BOOL bShow, UINT nStatus)
  115. {
  116. CDialog::OnShowWindow(bShow, nStatus);
  117. if(m_bKillInitialSelection) {
  118. m_LogViewer.SetSel(0,0);
  119. m_bKillInitialSelection = FALSE;
  120. }
  121. }
  122. void CLogViewer::ColorizeText(BOOL bColorize)
  123. {
  124. m_bColorizeLog = bColorize;
  125. }
  126. void CLogViewer::ColorLine(int LineNumber, COLORREF rgbColor)
  127. {
  128. int iStartSel = 0;
  129. int iEndSel = -1;
  130. if(LineNumber >0) {
  131. iStartSel = m_LogViewer.LineIndex(LineNumber);
  132. iEndSel = iStartSel + m_LogViewer.LineLength(iStartSel);
  133. }
  134. CHARFORMAT cf;
  135. memset(&cf,0,sizeof(cf));
  136. cf.cbSize = sizeof(CHARFORMAT);
  137. cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
  138. cf.dwEffects =(unsigned long) ~( CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
  139. cf.crTextColor = rgbColor;
  140. m_LogViewer.SetSel(iStartSel,iEndSel);
  141. m_LogViewer.SetSelectionCharFormat(cf);
  142. }
  143. void CLogViewer::ColorLine(int iStartSel, int iEndSel, COLORREF rgbColor)
  144. {
  145. CHARFORMAT cf;
  146. memset(&cf,0,sizeof(cf));
  147. cf.cbSize = sizeof(CHARFORMAT);
  148. cf.dwMask = CFM_COLOR | CFM_UNDERLINE | CFM_BOLD;
  149. cf.dwEffects =(unsigned long) ~( CFE_AUTOCOLOR | CFE_UNDERLINE | CFE_BOLD);
  150. cf.crTextColor = rgbColor;
  151. m_LogViewer.SetSel(iStartSel,iEndSel);
  152. m_LogViewer.SetSelectionCharFormat(cf);
  153. }
  154. void CLogViewer::ParseLogToColor()
  155. {
  156. TCHAR szBuffer[MAX_PATH];
  157. int NumLines = m_LogViewer.GetLineCount();
  158. int iStartSel = 0;
  159. int iEndSel = 0;
  160. BOOL bTrace = FALSE;
  161. BOOL bError = FALSE;
  162. BOOL bhResult = FALSE;
  163. BOOL bWarning = FALSE;
  164. if(m_pProgDlg != NULL) {
  165. PROGCTRL_SETUP_INFO SetupInfo;
  166. SetupInfo.iMinRange = 0;
  167. SetupInfo.iMaxRange = NumLines;
  168. SetupInfo.iStepValue = 1;
  169. m_pProgDlg->SetupProgressCtrl(&SetupInfo);
  170. for(int LineNumber = 0;LineNumber < NumLines;LineNumber++) {
  171. m_pProgDlg->StepIt();
  172. //
  173. // get line to parse
  174. //
  175. int CharactersWritten = m_LogViewer.GetLine(LineNumber,szBuffer,MAX_PATH);
  176. szBuffer[CharactersWritten] = '\0';
  177. //
  178. // Search for TRACE
  179. //
  180. if(strstr(szBuffer,TEXT("TRACE"))!= NULL) {
  181. if(bhResult) {
  182. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  183. bhResult = FALSE;
  184. }
  185. if(bWarning) {
  186. ColorLine(iStartSel,iEndSel,RGB(255,127,0));
  187. bWarning = FALSE;
  188. }
  189. if(bError) {
  190. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  191. bError = FALSE;
  192. }
  193. if(bTrace == FALSE) {
  194. bTrace = TRUE;
  195. }
  196. }
  197. //
  198. // Search for ERROR
  199. //
  200. if(strstr(szBuffer,TEXT("ERROR")) != NULL) {
  201. if(bTrace)
  202. bTrace = FALSE;
  203. if(bhResult) {
  204. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  205. bhResult = FALSE;
  206. }
  207. if(bWarning) {
  208. ColorLine(iStartSel,iEndSel,RGB(255,127,0));
  209. bWarning = FALSE;
  210. }
  211. if(bError == FALSE) {
  212. iStartSel = m_LogViewer.LineIndex(LineNumber);
  213. bError = TRUE;
  214. iEndSel = iStartSel + m_LogViewer.LineLength(iStartSel);
  215. } else {
  216. int itempStartSel = m_LogViewer.LineIndex(LineNumber);
  217. iEndSel = itempStartSel + m_LogViewer.LineLength(itempStartSel);
  218. }
  219. }
  220. //
  221. // Search for HRESULT
  222. //
  223. if(strstr(szBuffer,TEXT("HRESULT")) != NULL) {
  224. if(bTrace)
  225. bTrace = FALSE;
  226. if(bError) {
  227. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  228. bError = FALSE;
  229. }
  230. if(bWarning) {
  231. ColorLine(iStartSel,iEndSel,RGB(255,127,0));
  232. bWarning = FALSE;
  233. }
  234. if(bhResult == FALSE) {
  235. iStartSel = m_LogViewer.LineIndex(LineNumber);
  236. bhResult = TRUE;
  237. iEndSel = iStartSel + m_LogViewer.LineLength(iStartSel);
  238. } else {
  239. int itempStartSel = m_LogViewer.LineIndex(LineNumber);
  240. iEndSel = itempStartSel + m_LogViewer.LineLength(itempStartSel);
  241. }
  242. }
  243. //
  244. // Search for WARNING
  245. //
  246. if(strstr(szBuffer,TEXT("WARNING")) != NULL) {
  247. if(bTrace)
  248. bTrace = FALSE;
  249. if(bError) {
  250. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  251. bError = FALSE;
  252. }
  253. if(bhResult) {
  254. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  255. bhResult = FALSE;
  256. }
  257. if(bWarning == FALSE) {
  258. iStartSel = m_LogViewer.LineIndex(LineNumber);
  259. bWarning = TRUE;
  260. iEndSel = iStartSel + m_LogViewer.LineLength(iStartSel);
  261. } else {
  262. int itempStartSel = m_LogViewer.LineIndex(LineNumber);
  263. iEndSel = itempStartSel + m_LogViewer.LineLength(itempStartSel);
  264. }
  265. }
  266. //
  267. // Column separators
  268. //
  269. if(strstr(szBuffer,TEXT("=====")) != NULL){
  270. ColorLine(LineNumber,RGB(0,0,255));
  271. ColorLine(LineNumber+1,RGB(0,0,255));
  272. ColorLine(LineNumber+2,RGB(0,0,255));
  273. LineNumber+=3;
  274. }
  275. }
  276. if(bError)
  277. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  278. else if (bhResult)
  279. ColorLine(iStartSel,iEndSel,RGB(255,0,0));
  280. else if (bWarning)
  281. ColorLine(iStartSel,iEndSel,RGB(255,110,0));
  282. m_pProgDlg->DestroyME();
  283. }
  284. }
  285. void CLogViewer::SetProgressCtrl(CProgCtrl *pProgCtrl)
  286. {
  287. m_pProgDlg = pProgCtrl;
  288. }