📄 debug.cpp
字号:
/* deblog.cpp * Copyright (C) 2006 Michael H. Overlin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact at poster_printer@yahoo.com */#ifdef DBG#include "debug.h"#include "mathutils.h"#include "types.h"#include "utils.h"#include <windows.h>#include <stdarg.h>#include <stdio.h>#include <strsafe.h>#define MAX_DEBUGPRINT_CCH 256// ********************************************************************************************// ** DebugLog ******************************************************************************// ********************************************************************************************DebugLog AppendDebugLog(const char *pszLogName) { //FILE *pf = fopen(pszLogName, "a"); FILE *pf = NULL; pf = fopen(pszLogName, "a"); return (DebugLog) pf;}void CloseDebugLog(DebugLog *pdl) { FILE *pf = (FILE *) *pdl; if (pf != NULL) { fclose(pf); *pdl = NULL; }}void PrintDebugLog(DebugLog dl, const char *pszFormat, ...) { FILE *pf = (FILE *) dl; va_list vaList; va_start(vaList, pszFormat); vfprintf(pf, pszFormat, vaList); va_end(vaList); fflush(pf);}// ********************************************************************************************// ** MODULE PUBLIC ROUTINES ****************************************************************// ********************************************************************************************void DebugPrintf(const char *pszFormat, ...) { char astrBuff[MAX_DEBUGPRINT_CCH+1]; va_list vaList; va_start(vaList, pszFormat); ::StringCchVPrintfA(astrBuff, MAX_DEBUGPRINT_CCH, pszFormat, vaList); astrBuff[MAX_DEBUGPRINT_CCH] = 0; va_end(vaList); ::OutputDebugStringA(astrBuff);}void ErrorExit(LPTSTR lpszFunction) { LPTSTR lpFormat = TEXT("%s failed with error %d: %s"); LPTSTR lpMsgBuf; LPTSTR lpDisplayBuf; DWORD dw = GetLastError(); size_t dwcBuff; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); dwcBuff = StringBufferSize(lpMsgBuf)+ StringBufferSize(lpszFunction)+ StringBufferSize(lpFormat); lpDisplayBuf = (LPTSTR) LocalAlloc(LMEM_ZEROINIT, dwcBuff); StringCbPrintf(lpDisplayBuf, dwcBuff, lpFormat, lpszFunction, dw, lpMsgBuf); MessageBox(NULL, lpDisplayBuf, TEXT("Error"), MB_OK); LocalFree(lpMsgBuf); LocalFree(lpDisplayBuf); ExitProcess(dw); }#if 1// DEBUGVOID DrawLine(OUT HDC hdc, IN const POINT& pt1, IN const POINT& pt2) { POINT ptOld; ::MoveToEx(hdc, pt1.x, pt1.y, &ptOld); ::LineTo(hdc, pt2.x, pt2.y); ::MoveToEx(hdc, ptOld.x, ptOld.y, NULL);}VOID DrawRectangle(OUT HDC hdc, IN const RECT& r) { POINT pt; ::MoveToEx(hdc, r.left, r.top, &pt); ::LineTo(hdc, r.right, r.top); ::LineTo(hdc, r.right, r.bottom); ::LineTo(hdc, r.left, r.bottom); ::LineTo(hdc, r.left, r.top); ::MoveToEx(hdc, pt.x, pt.y, NULL);}VOID DrawX(OUT HDC hdc, IN const RECT &r) { POINT pt; ::MoveToEx(hdc, r.left, r.top, &pt); ::LineTo(hdc, r.right, r.bottom); ::MoveToEx(hdc, r.right, r.top, NULL); ::LineTo(hdc, r.left, r.bottom); ::MoveToEx(hdc, pt.x, pt.y, NULL);}VOID DrawEllipse(OUT HDC hdc, IN const RECT &r) { Ellipse(hdc, r.left, r.top, r.right, r.bottom);}VOID DrawTestImage(OUT HDC hdc) { static RECTD rdImage = { 0, 0, 4.0, 4.0 }; static double dTickSize = 0.25; static int NDIV = 5; static double dTextHeight = 0.10; static POINTD ptdLogPix = { 100, 100 }; ::SetMapMode(hdc, MM_LOENGLISH); ::SetViewportOrgEx(hdc, 0, 0, NULL); SetWindowOrgEx(hdc, 0, 0, NULL); LOGFONT lf; ZeroMemory(&lf, sizeof(lf)); StringCchCopy(lf.lfFaceName, 1000, TEXT("Times New Roman")); lf.lfHeight = RoundToLong(dTextHeight * ptdLogPix.y); HFONT hf = CreateFontIndirect(&lf); HFONT hfOld = (HFONT) SelectObject(hdc, hf); double dMinTick = 1.0 / (1 << (NDIV-1)); int nx = RoundToLong(RW(rdImage) / dMinTick + 1); int ny = RoundToLong(RH(rdImage) + 1); // HORIZONTAL RULES POINT pt; for(pt.y = 0; pt.y < ny; ++pt.y) { POINTD ptdLeft = { rdImage.left, pt.y }; POINTD ptdRight = { rdImage.right, pt.y }; POINT ptLeft, ptRight; RoundMult(ptLeft, ptdLeft, ptdLogPix); RoundMult(ptRight, ptdRight, ptdLogPix); DrawLine(hdc, ptLeft, ptRight); for(pt.x = 0; pt.x < nx; ++pt.x) { int nTickLevel = 0; while ( (pt.x & (1 << nTickLevel)) == 0 && nTickLevel < NDIV-1) { ++nTickLevel; } nTickLevel = NDIV - nTickLevel - 1; double dThisTickSize = dTickSize / ( 1 << nTickLevel ); POINTD ptd2 = { pt.x * dMinTick, pt.y }; POINTD ptdLow = { ptd2.x, ptd2.y - dThisTickSize/2 }; POINTD ptdHigh = { ptd2.x, ptd2.y + dThisTickSize/2 }; POINT ptLow, ptHigh; RoundMult(ptLow, ptdLow, ptdLogPix); RoundMult(ptHigh, ptdHigh, ptdLogPix); DrawLine(hdc, ptLow, ptHigh); if (nTickLevel == 0) { ptd2.y -= dTextHeight / 4; ptd2.x += dTextHeight / 4; POINT ptTickTo; RoundMult(ptTickTo, ptd2, ptdLogPix); TCHAR atstrBuff[32]; ::StringCchPrintf(atstrBuff, 32, TEXT("(%d,%d)"), (LONG) pt.x / (1 << (NDIV-1)), (LONG) pt.y); TextOut(hdc, ptTickTo.x, ptTickTo.y, atstrBuff, lstrlen(atstrBuff)); } } } // VERTICal RULES nx = RoundToLong(RW(rdImage) + 1); ny = RoundToLong(RH(rdImage) / dMinTick + 1); for(pt.x = 0; pt.x < nx; ++pt.x) { POINTD ptdTop = { pt.x, rdImage.top}; POINTD ptdBottom = { pt.x, rdImage.bottom }; POINT ptTop, ptBottom; RoundMult(ptTop, ptdTop, ptdLogPix); RoundMult(ptBottom, ptdBottom, ptdLogPix); DrawLine(hdc, ptTop, ptBottom); for(pt.y = 0; pt.y < ny; ++pt.y) { int nTickLevel = 0; while ( (pt.y & (1 << nTickLevel)) == 0 && nTickLevel < NDIV-1) { ++nTickLevel; } nTickLevel = NDIV - nTickLevel - 1; double dThisTickSize = dTickSize / ( 1 << nTickLevel ); POINTD ptd2 = { pt.x, pt.y * dMinTick }; POINTD ptdLow = { ptd2.x - dThisTickSize/2, ptd2.y }; POINTD ptdHigh = { ptd2.x + dThisTickSize/2, ptd2.y }; POINT ptLow, ptHigh; RoundMult(ptLow, ptdLow, ptdLogPix); RoundMult(ptHigh, ptdHigh, ptdLogPix); DrawLine(hdc, ptLow, ptHigh); } } SelectObject(hdc, hfOld); DeleteObject(hf);}#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -