⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debugviewlog.cpp

📁 介绍IStream接口的代码,方便学习,很有参考价值
💻 CPP
字号:
/*
********************************************************************************
模块名       : 
文件名       : DebugViewLog.cpp
相关文件     : DebugViewLog.h
文件实现功能 : 用于记录程序执行的调试信息 
作者         : Linger
版本         : 1.0
--------------------------------------------------------------------------------
使用方法     : 

1:引用日志头文件 
  #include "DebugViewLog.h"
2:定义一个日志类 
  CDebugViewLog log;
3:初始化调试信息显示方式,路径名,工程名 
  log.Init(true,NULL,"工程名");[其中文件路径参数在debugview模式可以为空]
  log.Init(false,"c:\\2.txt","工程名");
4:激活调试开关
  log.Enable(true);
5:输出调试信息
  log.DebugOut("AAAAAAAA");
  log.DebugOut("BBBBBBBB = %d",1);

--------------------------------------------------------------------------------
修改记录 : 
日 期        版本     修改人              修改内容
2007/10/12   1.0      Linger              创建
*******************************************************************************/

#include "stdafx.h"
#include "DebugViewLog.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDebugViewLog::CDebugViewLog():
							m_FileName(NULL),
							m_TitleName(NULL),
							m_Enable(false)
{
    InitializeCriticalSection(&m_cs);
}

CDebugViewLog::~CDebugViewLog()
{
	DeleteCriticalSection(&m_cs);
}

void CDebugViewLog::Init(bool mode,const char *pFileName,const char *pTitleName)
{
	m_Mode	    = mode;
	m_FileName  = pFileName;
	m_TitleName = pTitleName;
}

void CDebugViewLog::DebugOut(const char* txt, ...)
{
	if(!m_Mode)
	{
		//日志模式必须要指定文件名
		if (!m_FileName)return;
	}

	if (!m_Enable)return;
	if (!txt)return;
	
	EnterCriticalSection(&m_cs);
	{
		try
		{
			va_list argptr;
			va_start(argptr, txt);//得到值
			_vsnprintf(m_Buf, TXT_SIZE, txt, argptr);
			va_end(argptr);
		}
		catch (...)
		{
			m_Buf[0] = 0;
		}

		if(m_Mode)
		{
			static bool first = true;
			if(first)
			{
				OutputDebugString("********************************");
				if(!m_TitleName)
					m_TitleName = "";

				char* txt = new char[200];
				strcpy(txt,"* ");

				strcat(txt,m_TitleName);
				strcat(txt," Debug Trace Start                          ");
				OutputDebugString(txt);
				OutputDebugString("********************************");
				OutputDebugString("                                ");
				first = false;
				delete []txt;
			}
			OutputDebugString(m_Buf);
		}
		else
		{
			static bool first = true;
			if(first)
			{
			    FILE *fclean; 
				fclean = fopen(m_FileName,"w"); 
				fclose(fclean);
				first = false;
			}
			
			FILE *fp = fopen(m_FileName, "a");
			if (fp)
			{
				static bool Infomation = true;
				if(Infomation)
				{
					fprintf(fp,"********************************************************************\n");	
					fprintf(fp,"*                     %s Debug Trace Start                          \n", m_TitleName);
					fprintf(fp,"********************************************************************\n");
					fprintf(fp,"                                                                    \n");
					Infomation = false;
				}
				fprintf(fp, "%s\n", m_Buf);
				fclose(fp);
			}
		}
	}
	LeaveCriticalSection(&m_cs);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -