Windows File API Samples Part 1: Common log function

Problem: You want to log file operation begin and end time. Define your operations in constant Strings in common.h. The file is used in every sample in the “Windows File API Samples” series.

Here’s the content for file common.h (include logging and time subtraction):

#define _WIN32_WINNT 0x0500
#include "windows.h"
#include "shlwapi.h"
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <string>
#include   <sstream>

#pragma comment (lib, "shlwapi.lib")

using namespace std;

const string STR_LOAD_LIBRARY("LoadLibrary ");
const string COPY_FILE("New (CopyFileEx) ");
const string CREATE_FILE_MAPPING("Write (CREATE_FILE_MAPPING) ");
const string OPEN_FILE_MAPPING("Write (OPEN_FILE_MAPPING) ");
const string MAP_VIEW_OF_FILE_EX("Write (MAP_VIEW_OF_FILE_EX) ");

const string MOVEFILE_COPY("copy and delete (MOVEFILE_COPY_ALLOWED) ");
const string STR_MOVEFILE_WITH_PROGRESS("move file with progress ");

const string STR_CREATE_NEW("New ");
const string STR_DELETE("Delete ");
const string STR_WRITE("Write ");
const string STR_DELETE_WRITE("Delete and Write ");
const string STR_LOCK_WRITE("Lock,  write and unlock ");
const string STR_LOCKEX_WRITE("Lockex, write and unlockex ");
const string STR_CREATE_WRITE_ASYNC("Write asynchronously ");
const string STR_GET_FILE_ATTRIBUTES("GetFileAttributes ");
const string STR_SET_FILE_ATTRIBUTES("SetFileAttributes ");
const string STR_SET_FILE_POINTER("SetFilePointer ");
const string STR_SET_FILE_VALID_DATA("SetFileValidData ");
const string STR_FIND_FIRST_FILE("FindFirstFile ");
const string STR_FIND_FIRST_FILE_Ex("FindFirstFileEx ");
const string STR_LZ_INIT("LZInit, LZSeek, LZRead ");
const string STR_GET_BINARY_TYPE("GetBinaryType ");
const string STR_GET_FILE_INFORMATION("GetFileInformationByHandle ");
const string STR_GET_FILE_SIZE("GetFileSize ");
const string STR_GET_FILE_TYPE("GetFileType ");
const string STR_GET_SHORT_PATH_NAME("GetShortPathName ");
const string STR_GET_LONG_PATH_NAME("GetLongPathName ");
const string STR_GET_EXPANDED_NAME("GetExpandedName ");
const string STR_GET_TEMP_FILE_NAME("New File (GetTempFileName) ");
const string STR_SET_FILE_SHORT_NAME("SetFileShortName ");
const string STR_GET_COMPRESSED_FILE_SIZE("GetCompressedFileSize ");

/************************************************************************/
/* subtract end - begin , return time in mini seconds                                                                     */
/************************************************************************/
LONGLONG subtract( SYSTEMTIME *end, SYSTEMTIME *begin)
{

	FILETIME ftBegin;
	FILETIME ftEnd;

	SystemTimeToFileTime(begin, &ftBegin);
	SystemTimeToFileTime(end, &ftEnd);

	ULARGE_INTEGER uBegin ;
	memcpy_s( &uBegin,sizeof(uBegin), &ftBegin		,sizeof(ftBegin));

	ULARGE_INTEGER uEnd;
	memcpy_s( &uEnd,sizeof(uEnd), &ftEnd, sizeof(ftEnd));

	LONGLONG systemTimeIn_ms( ( uEnd.QuadPart/10000 - uBegin.QuadPart/10000)  );

	return systemTimeIn_ms;

}

class Debugger
{
private:
	//fstream debug;
	fstream *debug;

public:
	Debugger() // :debug("debug.log",  fstream::in | fstream::out | fstream::trunc)
	{
		TCHAR szModuleName[MAX_PATH];
		TCHAR szLogFileName[MAX_PATH];
		GetModuleFileName( GetModuleHandle(NULL), szModuleName, sizeof(szModuleName)/sizeof(TCHAR) );
		 _stprintf_s( szLogFileName, MAX_PATH, _T("%s%s"), szModuleName, _T(".log") );

		debug = new fstream (szLogFileName,  fstream::in | fstream::out | fstream::trunc);
	}
	void log(string msg)
	{
		(*debug) << msg << endl;
	}

	void logBegin(string operation)
	{
		SYSTEMTIME begin;
		GetLocalTime(&begin);

		CHAR chTime[32+2];
		CHAR chDate[32+2];

		GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &begin, ("HH':'mm':'ss"), chTime, 32);
		GetDateFormatA(LOCALE_USER_DEFAULT, 0, &begin, ("yyyy-MM-dd"), chDate, 32);

		log("[" + string(chDate) + " " + chTime + "] " + operation + "begin "  );
	}

	void logEnd(string operation)
	{
		SYSTEMTIME begin;
		GetLocalTime(&begin);

		CHAR chTime[32+2];
		CHAR chDate[32+2];

		GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &begin, ("HH':'mm':'ss"), chTime, 32);
		GetDateFormatA(LOCALE_USER_DEFAULT, 0, &begin, ("yyyy-MM-dd"), chDate, 32);

		log("[" + string(chDate) + " " + chTime + "] " +operation + "end");
	}

	void close()
	{
		debug->close();
		delete debug;
	}
};
Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)