GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Dec-2007, 11:30
.toString() .toString() is offline
New Member
 
Join Date: Dec 2007
Posts: 1
.toString() is on a distinguished road

Looking for a better method


Hey guys. This is my first post. I began learning programming in C#, but my job occasionally requires that I maintain some legacy C++ code. This is usually a struggle for me. Nonetheless, my task is simple - create a class in a specific project that writes out a log file. This is an ATL project with no MFC support. I'm passing the function an error message and the filename without a path. Please take a look at my code and alert me if I am doing something incorrectly. The compiler is not complaining about anything, but I still feel uncomfortable with this code. Thanks in advance!

CPP / C++ / C Code:
//	DetailsLog.cpp : Implementation of DetailsLog
//	12/03/2007
//	RA

#include "StdAfx.h"
#include "DetailsLog.h"
#include <iostream>

STDMETHODIMP DetailsLog::WriteLogEntry(CString logMsg, CString logFileName)
{
	HRESULT hr = S_OK;
	CString errorLogPath;
	CString dateDir;

	// Get the current date for the correct sub dir
	SYSTEMTIME cd;
	GetSystemTime(&cd);

	// Build the date string
	dateDir.Format("%s", cd.wYear);
	dateDir.AppendFormat("%s", cd.wMonth);
	dateDir.AppendFormat("%s", cd.wDay + "\\");

	// Build the path to the log directory
	errorLogPath.Format("%s", GetMACESSexpBasePath());
	errorLogPath.AppendFormat("\\Log\\" + dateDir);

	// Build the timestamp string
	CString timeStamp;
	timeStamp.Format("----------------  ");
	timeStamp.AppendFormat("%s", cd.wHour + ":");
	timeStamp.AppendFormat("%s", cd.wMinute + ":");
	timeStamp.AppendFormat("%s", cd.wSecond);
	timeStamp.AppendFormat("  ----------------\r\n");

	try
	{
		// First check and see if the log dir already exists
		if(!PathIsDirectory(errorLogPath))
		{
			// It does not already exist, so create it
			CreateDirectory(errorLogPath, NULL);
		}

		// Now that we've checked for the directory, 
                         //  append the file name to the path
		errorLogPath.AppendFormat(logFileName);
			
		// Now write to the log file
		ofstream logFile(errorLogPath);
		cout << timeStamp << endl;
		cout << logMsg << endl;

		// Close the open stream
		logFile.close();
	}
	catch(...)
	{
		return E_FAIL;
	}

	return hr;
}

 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save an existing file into a new file using "save as" method? asenthil MS Visual C++ / MFC Forum 3 26-Jan-2007 11:13
a tester class and then some. postage Java Forum 1 06-May-2006 16:48
convert get method to post method in a proxy server gururajanb .NET Forum 0 26-Apr-2006 11:14
Non-member method needs info from class... how? Elsydeon C++ Forum 3 19-Sep-2005 16:13
regarding main method jerry C++ Forum 18 09-Mar-2004 19:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.