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

📄 logtofile.cpp

📁 Symbian平台下进行开发时很有用的一个日志系统
💻 CPP
字号:
// LOGTOFILE.CPP//// Copyright (c) 1999-2002 PanSoftware Ltd.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License version 2 as// published by the Free Software Foundation.#include <f32file.h>#include "logtofile.h"/** * Handles overflow in AppendFormatList() */class TOverFlowHandler : public TDes8Overflow	{public:	/** Stub */	virtual void Overflow(TDes8& /*aDes*/) {};	};/** * This class implements a file logger. */class TFileLog	{public:	TInt Init();	void WriteLog(const TDesC& aFileName, const TDesC8& aFmt);	void WriteLogF(const TDesC& aFileName, TRefByValue<const TDesC8> aFmt, VA_LIST aList);	void WriteDirect(const TDesC& aFileName, const TDesC8& aDes);	void LogHexData(TDes8& aBuffer, const TDesC8& aData);	void LogData(const TDesC& aFileName, const TDesC8& aData);	TBool IsPrint(TInt aChar);	static void WriteDesF(TDes8& aDes, TRefByValue<const TDesC8> aFmt, VA_LIST aList);	void SetLogging(TBool aStatus);	TBool IsLogging();	void Close();public:	RFs iFs;                 ///< File server handle.	RFile iFileLog;          ///< File handle.	TFileName iFileLogName;  ///< Name of the log file.	TBool iLogging;          ///< Enable or disable logging.	};TInt TFileLog::Init()//// Initialise connection to FileServer//	{	iLogging = TRUE;	return iFs.Connect();	}void TFileLog::WriteLog(const TDesC& aFileName, const TDesC8& aFmt)//// Append aString to the end of the file//	{	if(aFileName.CompareF(iFileLogName))		{		iFileLog.Close();		iFileLogName=aFileName;		TInt ret = iFileLog.Open(iFs,iFileLogName,EFileWrite|EFileShareAny);		if(ret == KErrNotFound)			ret = iFileLog.Create(iFs,iFileLogName,EFileWrite|EFileShareAny);		else if(ret!=KErrNone)			return;		}	TTime currentTime;	currentTime.HomeTime();	TDateTime now=currentTime.DateTime();	TBuf8<0x500> buf;	buf.Format(_L8("0x%+02x; %+02d:%+02d,%+02d.%+03d "), RThread().Id(),		   now.Hour(), now.Minute(), now.Second(), now.MicroSecond()/1000);	buf.Append(aFmt);	TInt len=buf.Length();	len += (len<buf.MaxLength()) ? 2 : 0;	buf.SetLength(len);	buf[len-2]='\r';	buf[len-1]='\n';	iFileLog.Write(KMaxTInt,buf);	}void TFileLog::WriteDesF(TDes8& aDes, TRefByValue<const TDesC8> aFmt, VA_LIST aList)//// Write log to a descriptor//	{	TOverFlowHandler overflow;	TTime currentTime;	currentTime.HomeTime();	TDateTime now=currentTime.DateTime();	aDes.Format(_L8("0x%+02x; %+02d:%+02d,%+02d.%+03d "), RThread().Id(),		    now.Hour(), now.Minute(), now.Second(), now.MicroSecond()/1000);	aDes.AppendFormatList(aFmt, aList, &overflow);	TInt len=aDes.Length();	len += (len<aDes.MaxLength()-1) ? 2 : 0;	aDes.SetLength(len);	aDes[len-2]='\r';	aDes[len-1]='\n';	}void TFileLog::WriteDirect(const TDesC& aFileName, const TDesC8& aDes)//// Append aDes directly (i.e. with no additional formatting) to the end of the file//	{	if(!iLogging)		return;	if(aFileName.CompareF(iFileLogName))		{		iFileLog.Close();		iFileLogName=aFileName;		TInt ret = iFileLog.Open(iFs,iFileLogName,EFileWrite|EFileShareAny);		if(ret == KErrNotFound)			ret = iFileLog.Create(iFs,iFileLogName,EFileWrite|EFileShareAny);		else if(ret==KErrPathNotFound)			{			iLogging = EFalse;			return;			}		else if(ret!=KErrNone)			return;		}	iFileLog.Write(KMaxTInt,aDes);	}void TFileLog::WriteLogF(const TDesC& aFileName, TRefByValue<const TDesC8> aFmt, VA_LIST aList)//// Append aString to the end of the file//	{	if(!iLogging)		return;	if(aFileName.CompareF(iFileLogName))		{		iFileLog.Close();		iFileLogName=aFileName;		TInt ret = iFileLog.Open(iFs,iFileLogName,EFileWrite|EFileShareAny);		if(ret == KErrNotFound)			iFileLog.Create(iFs,iFileLogName,EFileWrite|EFileShareAny);		else if(ret!=KErrNone)			return;		}	TOverFlowHandler overflow;	TTime currentTime;	currentTime.HomeTime();	TDateTime now=currentTime.DateTime();	TBuf8<0x500> buf;	buf.Format(_L8("0x%+02x; %+02d:%+02d,%+02d.%+03d "), RThread().Id(),		   now.Hour(), now.Minute(), now.Second(), now.MicroSecond()/1000);	buf.AppendFormatList(aFmt, aList, &overflow);	TInt len=buf.Length();	len += (len<buf.MaxLength()-1) ? 2 : 0;	buf.SetLength(len);	buf[len-2]='\r';	buf[len-1]='\n';	iFileLog.Write(KMaxTInt,buf);	}void TFileLog::SetLogging(TBool aStatus)//// Turns on or off the logging//	{	iLogging = aStatus;	}TBool TFileLog::IsLogging()//// Returns whether the logging is on or off//	{	return iLogging;	}void TFileLog::Close()//// Close all file log resources//	{	iFileLog.Close();	iFs.Close();	iFileLogName=_L("");	}LOCAL_D const TText gHexData[] = {  '0', '1', '2', '3', '4', '5', '6', '7',  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};TBool TFileLog::IsPrint(TInt /*aChar*/)//// first cut of an isprint, says everything is print...//	{	return true;	}void TFileLog::LogHexData(TDes8& aBuffer, const TDesC8& aData)//// write hex formatted loging data//	{	TInt dataLength=aData.Length();	if(dataLength==0)		return;	TBuf8<16> textBuffer;	TInt i=0;	for(i=0;i<dataLength;i++)		{		TInt byteVal=aData[i];		TInt highbits=byteVal >> 4;		aBuffer.Append(gHexData[highbits]);		TInt lowbits=byteVal%16;		aBuffer.Append(gHexData[lowbits]);		aBuffer.Append(' ');		if(IsPrint(byteVal))			textBuffer.Append(byteVal);		else			textBuffer.Append('.');		if(i>0 && ((i+1)%16)==0)			{			aBuffer.Append(_L(" \""));			aBuffer.Append(textBuffer);			aBuffer.Append(_L("\"\r\n"));			textBuffer.SetLength(0);			if(aBuffer.MaxLength() - aBuffer.Length() < 128)				{				aBuffer.Append(_L("More...\r\n"));				return;				}			}		}	if((i%16)!=0)		{		aBuffer.Append(_L(" \""));		aBuffer.Append(textBuffer);		aBuffer.Append(_L("\"\r\n"));		textBuffer.SetLength(0);		}	}void TFileLog::LogData(const TDesC& aFileName, const TDesC8& aData)//// Log Hex data to file//	{	if(!iLogging)		return;	TBuf8<1024> buf;	buf.Append(_L("\r\n"));	LogHexData(buf,aData);	WriteLog(aFileName,buf);	}////////////////////////////////////////////////////////////////////////////////// CLogToFileTls/////////////////////////////////////////////////////////////////////////////////** * File logger using TLS (Thread Local Storage) */class CLogToFileTls : public CBase	{public:	static CLogToFileTls* New();	~CLogToFileTls();public:	TFileLog iFileLog; ///< Pointer to the file logging class.	};CLogToFileTls* CLogToFileTls::New()//// Factory Constructor//	{	CLogToFileTls* tls = new CLogToFileTls;	if(tls == NULL)		return NULL;	TInt ret = tls->iFileLog.Init();	if(ret!=KErrNone)		{		delete tls;		tls = NULL;		}	return tls;	}CLogToFileTls::~CLogToFileTls()//// Destructor//	{	iFileLog.Close();	}////////////////////////////////////////////////////////////////////////////////// Local and Exported functions////////////////////////////////////////////////////////////////////////////////LOCAL_C TFileLog* GetFileLog()//// Return the thread local file log//	{	CLogToFileTls* tls = (CLogToFileTls*)Dll::Tls();	if(tls)		return &tls->iFileLog;	tls=CLogToFileTls::New();	if(tls == NULL)		return NULL;	TInt ret = Dll::SetTls(tls);	if(ret!=KErrNone)		{		delete tls;		Dll::FreeTls();		return NULL;		}	return &tls->iFileLog;	}EXPORT_C void WriteLog(const TDesC& aFileName, const TDesC8& aFmt)//// Write log to file//	{	TFileLog* fileLog=GetFileLog();	if(fileLog == NULL)		return;	if(fileLog->IsLogging())		fileLog->WriteLog(aFileName,aFmt);	}extern "C"EXPORT_C void WriteLog(const char* aFileName, const char* aFmt)//// Write log to file//	{	TBuf<KMaxFileName> fileName;	fileName.Copy(TPtrC8((const TText8 *)aFileName));	TPtrC8 format((const TText8 *)aFmt);	WriteLog(fileName,format);	}EXPORT_C void WriteLogData(const TDesC& aFileName, const TDesC8& aFmt)//// Write log to file//	{	TFileLog* fileLog=GetFileLog();	if(fileLog == NULL)		return;	if(fileLog->IsLogging())		fileLog->LogData(aFileName,aFmt);	}EXPORT_C void WriteDesData(TDes8& aBuffer, const TDesC8& aFmt)//// Write log to file//	{	TFileLog* fileLog=GetFileLog();	if(fileLog == NULL)		return;	if(fileLog->IsLogging())		fileLog->LogHexData(aBuffer,aFmt);	}EXPORT_C void WriteLogF(const TDesC& aFileName, TRefByValue<const TDesC8> aFmt, VA_LIST aList)//// Write log to file//	{	TFileLog* fileLog=GetFileLog();	if(fileLog == NULL)		return;	if(fileLog->IsLogging())		fileLog->WriteLogF(aFileName,aFmt,aList);	}EXPORT_C void WriteLogF(const TDesC& aFileName, TRefByValue<const TDesC8> aFormat, ...)//// Write log to file//	{	VA_LIST list;	VA_START(list, aFormat);	WriteLogF(aFileName,aFormat,list);	}extern "C"EXPORT_C void WriteLogF(const char* aFileName, const char* aFmt, ...)//// Write log to file//	{	TBuf<KMaxFileName> fileName;	fileName.Copy(TPtrC8((const TText8 *)aFileName));	VA_LIST list;	VA_START(list, aFmt);	TPtrC8 format((const TText8 *)aFmt);	WriteLogF(fileName,format,list);	}EXPORT_C void WriteDirect(const TDesC& aFileName, const TDesC8& aDes)//// Write log to file//	{	TFileLog* fileLog=GetFileLog();	if(fileLog == NULL)		return;	fileLog->WriteDirect(aFileName,aDes);	}EXPORT_C void WriteDesF(TDes8& aDes,TRefByValue<const TDesC8> aFmt, VA_LIST aList)//// Write log to a descriptor//	{	TFileLog::WriteDesF(aDes,aFmt,aList);	}EXPORT_C void WriteLogAllocTls()//// Make all tls allocations//	{	GetFileLog();	}EXPORT_C void WriteLogDeallocTls()//// Delete all tls//	{	CLogToFileTls* tls = (CLogToFileTls*)Dll::Tls();	if(tls)		delete tls;	Dll::FreeTls();	}extern "C" EXPORT_C void SetLogging(TBool aStatus)//// Sets whether to be logging or not...//	{	TFileLog* fileLog=GetFileLog();	if(fileLog == NULL)		return;	fileLog->SetLogging(aStatus);	}////////////////////////////////////////////////////////////////////////////////// E32Dll////////////////////////////////////////////////////////////////////////////////GLDEF_C TInt E32Dll(TDllReason /*aReason*/)//// DLL entry point//	{	return(KErrNone);	}

⌨️ 快捷键说明

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