|
Introduction Back in 2001-2002, when I was working on a project that needed some Windows services to be built, I needed information on how the same service would run on Win 2K/XP, Pocket PC, and Win CE devices along with the data getting manipulated. I had written some functions that will log the required information into a text file. The function could take any number of arguments to be logged into the log file. This article describes how to log the required information along with a date and time stamp into a predefined text file. Using the code There are three functions shown below, and the detailed information about each function is mentioned in the function comment block. To use the code, replace CKishoreResearch in this code with your project class name, or you can place these three functions in some common.h file and use them as global functions in your project. In the .h file, declare as follows: // File pointer that will be used // in the log functions to write to a valid file FILE *m_fp; // Logs the Pointer to a null-terminated // string of 16-bit Unicode characters void log(LPWSTR str, ...); // Logs the Pointer to a null-terminated string // of 8-bit Windows (ANSI) characters. void log(LPSTR str, ...); In the .CPP file, in the class constructor, use the following code: m_fp = NULL; m_fp = _wfopen(L"C:\\KishoreLog.txt", L"a"); Following is the implemented code that actually does the logging functionality: /* Function : void log(LPWSTR str, ...) Return Type : void parameters : LPWSTR,Ellipsis Use : Logs the information onto a text file with any number of input parameters : Pointer to a constant null-terminated string of 16-bit Unicode characters. Author : Kishore Gaddam Date : 03 December 2002 Version : 1.0 */ void CKishoreResearch::log(LPWSTR str, ...) { if (m_fp) // m_fp Contains the pointer // to the log file to log the information { va_list arg_ptr; va_start(arg_ptr, str); SYSTEMTIME st; GetLocalTime(&st); fwprintf(m_fp, L"[%d/%d/%d - %d:%d:%d:%d]", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); vfwprintf(m_fp, str, arg_ptr); fwprintf(m_fp, L"\n"); fflush(m_fp); } } /* Function : void log(LPSTR str, ...) Return Type : void parameters : LPSTR,Ellipsis Use : Logs the information onto a text file with any number of input parameters : Pointer to a null-terminated string of 8-bit Windows (ANSI) characters. Author : Kishore Gaddam Date : 03 December 2002 Version : 1.0 */ void CKishoreResearch::log(LPSTR str, ...) { if (m_fp) // m_fp Contains the pointer // to the log file to log the information { va_list arg_ptr; va_start(arg_ptr, str); SYSTEMTIME st; GetLocalTime(&st); fprintf(m_fp, "[%d/%d/%d - %d:%d:%d:%d]", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); vfprintf(m_fp, str, arg_ptr); fprintf(m_fp, "\n"); fflush(m_fp); } } /* Function : ComErrorMessageLog(_com_error &e) Return Type : void parameters : _com_error Use : Logs the COM error information to a text file Author : Kishore Gaddam Date : 03 December 2004 Version : 1.0 */ void CKishoreResearch::ComErrorMessageLog(_com_error &e) { bstr_t bstrSource(e.Source()); bstr_t bstrDescription(e.Description()); log( _T("Code = 0x%08lx\n"), e.Error()); log( _T("Code meaning = %s\n"), e.ErrorMessage()); log( _T("IErrorInfo.Source = %s\n"), (LPTSTR)bstrSource ); log( _T("IErrorInfo.Description = %s"), (LPTSTR)bstrDescription ); } Copy the above three functions into your code. Again, to use the code, replace the class CKishoreResearch in the above code with your project class name. Once these three functions are copied, you can just make a call to the log(..) function and pass the required information that needs to be logged into the text file. Logging commonly used data types Listed below are the examples of some commonly used data types. For example, if you have a _bstr_t data type variable and you want to log the value of the same using the log function: //bsSelection is the value that needs //to be logged into the log file. _bstr_t bsSelection; //Make a call to Log function and pass //(LPTSTR)bsSelection as the parameter. log((LPTSTR)bsSelection); To log a TCHAR data type value: TCHAR szSql[256]; log(szSql); To log an int data type value along with some user defined strings: int nData; log("Item Select = %d", nData); To log multiple UINT data type values along with some user defined strings: UINT cbID1,cbID2; log(L" ID1 is %d and ID2 is %d", cbID1, cbID2); To log a TCHAR data type value along with some user defined strings: TCHAR *szFolderName log("Folder: [%s]", szFolderName); Points of interest Apart from logging the required information, you can also use these functions in exception handling to log the reason for the exception. Following is the code that will go in catch block, in case of a COM exception: catch(_com_error &e) { ::MessageBox(NULL, (char*)e.Description(), "Application Error", MB_OK|MB_ICONERROR); ComErrorMessageLog(e); CoUninitialize(); } Following is the code that will go in the catch block, in case of a normal exception, in MFC: catch(CException* e) / Handle all other types of exceptions here. { TCHAR szErr[1024]; e->GetErrorMessage(szErr, 1024); ::MessageBox(NULL, (char*)szErr, "Application Error", MB_OK|MB_ICONERROR); log(szErr); } Well, the list of things on using the log function will go on and on. So, I will stop here and let the users use the above code to log their information. :) About kishore Gaddam Kishore presently lives in Hyderabad, India, and is so spoiled by the generosity of Hyderabadi’s and delicious food, that he will probably never be able to live anywhere else wholeheartedly. He started QBASIC programming in 8th grade followed by Fortran 77. Graduated from Bangalore University with a Mechanical Engineering degree in 1999-2000, and immediately landed in a job as a programmer at LS Mechatronics, working in the Robots & CNC Machine Software division. He pretty much taught himself C, C++, Windows, MFC, COM, COM+, App Centre 2000, VB.NET, C#.NET,.NET CF, Windows Mobile (Pocket PC), Database programming etc. But in 2002, he left the nest to strike out on his own and build Swethark Technologies, a development company he co-founded along with friends from India & UK, with offices in India and USA. Swethark flagship product is Biometric software for automated attendance using image recognition from incoming video stream, followed by many other customized products developed for US, UK and India customers. He worked as a technical consultant in software industry during this period. Kishore joined GE in 2004 and is a Technology Lead at GE India Innovation Center, Hyderabad. He is the Architect, designer & developer of GearTrak Product being developed by GE Sensing. GearTrak is a wireless sensing solution to monitor temperature, humidity, fluid flow, pressure and various engineering parameters - primarily used in Industrial Automation & Process Control applications. Kishore was born and bred in VeerannaPalem, a small village in Costal Andhra. For relaxation he is into listening Music, watching movies, reading scientific American, MIT Technology Review, nasa.org, etc. while not at work, he finds interesting to do R & D on Windows Mobile and .NET Compact Framework technologies to develop some interesting applications. "Never be afraid to try something new. Remember, amateurs built the ark; Professionals built the Titanic."
Other popular Mobile / Embedded articles: Pocket PC TV Remote Control An article describing how to use the IR port on a pocket PC to control a TV. Infrared Communication with your Mobile Phone Learn how to make your Pocket PC speak with your mobile phone. CHM Reader for Pocket PC 2003 Allows the reading of CHM files on a Pocket PC2003. A comprehensive CE class library to replace ATL and MFC A collection of classes for CE that do not use ATL or MFC, plus an FTP client, database viewer, and sample application that solves beam deflection equations. 转自:http://www.codeproject.com/ce/GenericLogFunctionality.asp
|