Need a simple logger in Win32 ?

If you need like me, a logger for your deamons and services… just look at this simple logger:

#pragma once
#include <string>

class CLogger
{
public:
	CLogger();
	virtual ~CLogger();

public:
	void Init(std::wstring name);
	void WriteLog(std::wstring message);

private:
	std::wstring _name;
	std::wstring _path;
};

You put the name of the file in Init() method as then, you just call WriteLog() ! Let’s ook at the code impl:

#include "stdafx.h"
#include "Logger.h"


CLogger::CLogger()
{
}


CLogger::~CLogger()
{
}

void CLogger::Init(std::wstring name)
{
	_name = name;

	TCHAR szTemp[255];
	_stprintf_s(szTemp, _T("C:\\TEMP\\LOGS"));
	::CreateDirectory(szTemp, NULL);

	TCHAR szPath[255];
	_stprintf_s(szPath, _T("%s\\%s"), szTemp, name.c_str());

	_path = szPath;
}

void CLogger::WriteLog(std::wstring message)
{
	std::string path(_path.begin(), _path.end());

	SYSTEMTIME st;
	memset(&st, 0, sizeof(SYSTEMTIME));
	::GetSystemTime(&st);

	TCHAR sz[1024];
	_stprintf_s(sz, 
		_T("%02d:%02d:%02d.%03d - INFO - %s\r\n"), 
		st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, 
		message.c_str());

	std::wstring wsz = sz;
	std::string msgToWrite(wsz.begin(), wsz.end());

	HANDLE hFile = ::CreateFileA(path.c_str(), 
		GENERIC_WRITE, FILE_SHARE_WRITE, 
		NULL, OPEN_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL, NULL);

	LONG l = 0;
	::SetFilePointer(hFile, 0, &l, FILE_END);
	
	DWORD dwLen = 0;
	::WriteFile(hFile, 
		msgToWrite.c_str(), msgToWrite.length(), 
		&dwLen, NULL);
	
	::CloseHandle(hFile);

	printf_s(msgToWrite.c_str());
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: