VB icon

Log

Email
Submitted on: 10/27/2019 8:25:15 AM
By: Philippe RIO  
Level: Beginner
User Rating: Unrated
Compatibility: C
Views: 242
author picture
 
     Add a simple log to your program.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Log
// Description:Add a simple log to your program.
// By: Philippe RIO
//**************************************

File : Log.h
void Log(LPSTR __lpszTxt) ;
void Log_Init(void) ;
void Log_Start(void) ;
void Log_Stop(void) ;
void Log_Exit(void) ;
void Log_SetFileName(LPSTR) ;
LPSTR Log_GetFileName(void) ;
-------------------------------------
File : Log.c
#include <windows.h>
#include "Log.h"
HANDLE	hLogFile ;
BOOL	bLog ;
char	szLogFileName[MAX_PATH] ;
void Log_SetFileName(LPSTR __lpszLogFileName)
{
	lstrcpy(szLogFileName,__lpszLogFileName) ;
	return ;
}
LPSTR Log_GetFileName(void)
{
	return (szLogFileName) ;
}
void Log_Stop(void)
{
	Log("*** Log Stoped ***") ;
	bLog = FALSE ;
	return ;
}
void Log_Start(void)
{
	bLog = TRUE ;
	Log("*** Log Started ***") ;
	return ;
}
void Log(LPSTR __lpszTxt)
{
	char		_szTmp[4096] ;
	char		_szTime[256] ;
	char		_szDate[256] ;
	SYSTEMTIME	_St ;
	DWORD		_dwTmp ;
	int			_iLen ;
	if(!hLogFile)	return ;
	if(!bLog)		return ;
	GetLocalTime(&_St) ;
	GetTimeFormat(LOCALE_USER_DEFAULT,TIME_FORCE24HOURFORMAT,&_St,NULL,_szTime,sizeof(_szTime)) ;
	GetDateFormat(LOCALE_USER_DEFAULT,DATE_LONGDATE,&_St,NULL,_szDate,sizeof(_szDate)) ;
	_iLen = wsprintf(_szTmp,"[%s %s] : %s\n",_szDate,_szTime,__lpszTxt) ;
	WriteFile(hLogFile,_szTmp,_iLen,&_dwTmp,NULL) ;
	return ;
}
void Log_Init(void)
{
	hLogFile = CreateFile(szLogFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL) ;
	if(hLogFile == INVALID_HANDLE_VALUE)
	{
		hLogFile = NULL ;
		return ;
	}
	if(hLogFile)	Log_Start() ;
	if(hLogFile)	Log("Openning Log") ;
	if(hLogFile)	Log("Log Ready") ;
	return ;
}
void Log_Exit(void)
{
	if(!hLogFile)	return ;
	if(!bLog)	Log_Start() ;
	Log("Closing Log file") ;
	Log_Stop() ;
	FlushFileBuffers(hLogFile) ;
	CloseHandle(hLogFile) ;
	hLogFile = NULL ;
	return ;
}
-------------------------------------------------


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.