LogIt.cpp
上传用户:deligs
上传日期:2007-01-08
资源大小:43k
文件大小:13k
- //////////////////////////////////////////////////////////////////////
- // Class CLogIt Implementation File
- //
- // Created: 2/7/00
- //
- // Author: Daniel Madden
- // Daniel.Madden@Compaq.Com
- //
- // History:
- //
- // Modifications (by other programmers):
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "LogIt.h"
- #include <direct.h>
- /////////////////////////////////////////////////////////////////////////////
- // CLogIt Contruction
- CLogIt::CLogIt()
- {
- // The below value is the Pass Phrase for the encrypted "Pass Phrase"
- // I do it this way so that the executable doesn't have any
- // plain text strings in it that tell someone what the pass phrase is
- m_csAppsPPhrase.Format("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",0x40,0x4D,0x49,0x4D,0x47,0x4D,0x50,0x4D,0x47,0x4D,0x50,0x4E,0x4D,0x53,0x50,0x4E,0x4D,0x53,0x65,0x63,0x4D,0x23,0x4D,0x31);
- // Set the Log dir
- //m_csLogDir = _T("\\SERVER\Share\Logs\");
- //m_csLogDir = _T(DEFAULT_LOG_PATH);// Default Log Path
- m_csLogDir = _T("");
- m_bDirExists = FALSE;
- // Log file handle
- m_fLog = NULL;
- // used by the "GetSystemDomainUserName" function
- // cchUser = UNLEN;
- // cchDomain = DNLEN;
- // used by the "GetDomainUser" function
- m_csDomainUserName = _T("Domain\Username");
- }
- //**********************************************************************************
- //
- // nEntryType:
- //
- // 0 = Error
- // 1 = Warning
- // 2 = Information
- // 3 = Lock (Secure)
- // 4 = Dir Folder (Closed)
- // 5 = Dir Folder (Opened)
- // 6 = Paper (Log)
- // 7 = User (incognito)
- // 8 = Note
- //
- //**********************************************************************************
- void CLogIt::WriteLog(CString csMsg)
- {
- CTime ctNow = CTime::GetCurrentTime();
- CString csEntryTime = ctNow.Format( "%H:%M:%S" );
- CFile cfLog;
- CFileException e;
-
- // Write to it
- // even if it's being writing to!
- // because we are using CFile
- if(TRUE){//!IsOpen()) {
- CString csText = _T("");
- if (m_bDirExists) {
- if( !cfLog.Open( m_csFullLogName,
- CFile::modeCreate |
- CFile::modeNoTruncate |
- CFile::shareDenyNone |
- CFile::modeWrite, &e ) )
- {
- #ifdef _DEBUG
- afxDump << "File could not be opened " << e.m_cause << "n";
- #endif
- return;
- }
- csText.Format("%s - %srn", csEntryTime, csMsg);
- cfLog.SeekToEnd();
- cfLog.Write(csText, csText.GetLength());
- cfLog.Flush();
- cfLog.Close();
- }
- else {
- csText.Format("Logging Directory doesn't exist [%s]!", m_csLogDir);
- MessageBox(NULL, csText, "Error: Accessing Log Directory", MB_OK);
- }
- }
- }
- void CLogIt::WriteLogOld(CString csMsg)
- {
- CTime ctNow = CTime::GetCurrentTime();
- CString csEntryTime = ctNow.Format( "%H:%M:%S" );
- // We don't want to write to it
- // if it's being writing to!
- if (!IsOpen()) {
- CString csText = _T("");
- if (m_bDirExists) {
- m_fLog = fopen(m_csFullLogName, "a");
- if (m_fLog != NULL) {
- csText.Format("%s - %s", csEntryTime, csMsg);
- fprintf(m_fLog, "%sn", csText);
- }
- int nClosed = fclose(m_fLog);
- if (nClosed == 0)
- m_fLog = NULL;
- else
- MessageBox(NULL, "Problem closing log file", "Error: Closing file", MB_OK);
- Sleep(100); // Give it time to close
- }
- else {
- csText.Format("Logging Directory doesn't exist [%s]!", m_csLogDir);
- MessageBox(NULL, csText, "Error: Accessing Log Directory", MB_OK);
- }
- }
- }
- BOOL CLogIt::CreateLogDirectory(CString csDirName)
- {
- BOOL bRet = CreateDirectory(csDirName, NULL);
- if (!bRet) {
- DWORD dwErrNum = GetLastError();
- if (dwErrNum != 183) {
- CString csErrTitle;
- csErrTitle.Format("Error: %u", dwErrNum);
- MessageBox(NULL, DisplayError(dwErrNum), csErrTitle, MB_OK|MB_ICONSTOP);
- }
- else
- bRet = TRUE;
- }
- return bRet;
- }
- BOOL CLogIt::CreateLogPath(void)
- {
- if (!m_csLogDir.IsEmpty()) {
- // int's
- int n = -1;
- // CString's
- CString csDir = _T("");
-
- // CStringArray's
- CStringArray csaLogPath;
- // Gets the system time.
- CTime ctNow = CTime::GetCurrentTime();
-
- // Format the Month Directory name
- //
- // Formatted to: "July2000.logs"
- CString csMonthDir = ctNow.Format("%B%Y.logs\");
- int nHour = ctNow.GetHour();
- int nMinute = ctNow.GetMinute();
- int nSecond = ctNow.GetSecond();
- int nDay = ctNow.GetDay();
- int nMonth = ctNow.GetMonth();
- int nYear = ctNow.GetYear();
- m_csLogEntryTime = _T("");
- // Formatted to: "09:20:54"
- m_csLogEntryTime.Format("%.2i:%.2i:%.2i", nHour, nMinute, nSecond);
-
- // Formatted to: "lg03072000.log"
- m_csLogName.Format("log%.2i%.2i%i.log", nMonth, nDay, nYear);
- // Formatted to: "<Your Choice>July2000.logs"
- m_csLogPath = m_csLogDir + csMonthDir;
- // Formatted to: "<Your Choice>July2000.logslg03072000.log"
- m_csFullLogName = m_csLogDir + csMonthDir + m_csLogName;
-
- //////////////////////////////
- // Begin the work!
- //////////////////////////////
- //
- // put directory structure into an array (CString)
- Split(m_csFullLogName, _T("\"), csaLogPath, FALSE);
- // Get the Drive (or Server) name
- CString csTmp = csaLogPath.GetAt(0);
- int nUBound = csaLogPath.GetUpperBound();
- // Notice "n<nUBound" below...
- // the path I Split, contains a filename and
- // that is not needed when creating the Dir Structure
- //
- // If you wanted just the filename from the path...just do this:
- //
- // CString csFilename = csaLogPath.GetAt(csaLogPath.GetUpperBound());
- //
- for (n=0; n<nUBound; n++) {
- if (n == 0) {
- if ( (csTmp.Mid(1, 1) == ":") || (csTmp.Mid(0, 1) == ".")) { // We are working with (C:) format
- // Format to: "C:"
- csDir = csaLogPath.GetAt(n) + "\";
- }
- else { // We are working with a "\Servershare"...
- // Format to: "\SERVER"
- csDir = _T("\\");
- csDir += csaLogPath.GetAt(n) + "\";
- }
- }
- else {
- csDir += csaLogPath.GetAt(n) + "\";
- if ((csTmp.Mid(1, 1) == ":") || (csTmp.Mid(0, 1) == ".")) { // We are working with (C:) format
- // Format to: "C:<Dir>..."
- //
- // and create it
- if (!CreateLogDirectory(csDir))
- return FALSE;
- }
- else { // We are working with a "\Servershare"...
- if (n == 1) { // Share Name
- // Format to: "\SERVERShare"
- csDir += csaLogPath.GetAt(n) + "\";
- }
- else {
- // Format to: "\SERVERShare<Dir>..."
- //
- // and create it
- csDir += csaLogPath.GetAt(n) + "\";
- if (!CreateLogDirectory(csDir))
- return FALSE;
- }
- }
- }
- }
- }
- else {
- MessageBox(NULL, "You must have a directory name to Create the Log Path!", "Error: CreateLogPath", MB_OK|MB_ICONSTOP);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CLogIt::CreateLogPath(CString csLogDir)
- {
- // Simply sets the 'm_csLogDir' variable
- // and calls 'CreateLogPath()'
- BOOL bRet = FALSE;
- if (!csLogDir.IsEmpty()) {
- m_csLogDir = csLogDir;
- bRet = CreateLogPath();
- }
- m_bDirExists = bRet;
- return bRet;
- }
- void CLogIt::Split(CString Source, CString Deliminator, CStringArray& AddIt, BOOL bAddEmpty)
- {
- // initialize the variables
- CString newCString = Source;
- CString tmpCString = "";
- CString AddCString = "";
- int pos1 = 0;
- int pos = 0;
- AddIt.RemoveAll();
- if (Deliminator.IsEmpty()) {
- // Add default [comma] if empty!
- // acknowledgement: Prasad [gprasad@rti.ie]
- Deliminator = ",";
- }
- // do this loop as long as you have a deliminator
- do {
- // set to zero
- pos1 = 0;
- // position of deliminator starting at pos1 (0)
- pos = newCString.Find(Deliminator, pos1);
- // if the deliminator is found...
- if ( pos != -1 ) {
- // load a new var with the info left
- // of the position
- CString AddCString = newCString.Left(pos);// - 1);
- if (!AddCString.IsEmpty()) {
- // if there is a string to add, then
- // add it to the Array
- AddIt.Add(AddCString);
- }
- else if (bAddEmpty) {
- // if empty strings are ok, then add them
- AddIt.Add(AddCString);
- }
- // make a copy of the of this var. with the info
- // right of the deliminator
- tmpCString = newCString.Mid(pos + Deliminator.GetLength());
-
- // reset this var with new info
- newCString = tmpCString;
- }
- } while ( pos != -1 );
-
- if (!newCString.IsEmpty()) {
- // as long as the variable is not emty, add it
- AddIt.Add(newCString);
- }
- }
- BOOL CLogIt::GetOSType(CString& csOSVersion, int& nType)
- {
- OSVERSIONINFO versionInfo;
- CString csMsg = _T("");
- // set the size of OSVERSIONINFO, before calling the function
- versionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
- // Get the version information
- if (!::GetVersionEx (&versionInfo)) {
- CString csErrMsg = _T("");;
- csMsg.Format("Error(%u): GetVersionEx: %s", GetLastError(), DisplayError(GetLastError()));
- WriteLog(csMsg);
- return FALSE;
- }
- // Check which OS it is
- if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
- // Windows NT test
- if ((versionInfo.dwMajorVersion > 4) && (versionInfo.dwMinorVersion >= 0)) {
- nType = 5;
- csOSVersion = _T("Win2000");
- }
- else if ((versionInfo.dwMajorVersion == 4) && (versionInfo.dwMinorVersion >= 0)) {
- nType = 4;
- csOSVersion = _T("WinNT4");
- }
- }
- else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
- nType = 2; // Win95
- csOSVersion = _T("Win95");
- // Windows 98 test
- if ((versionInfo.dwMajorVersion > 4) || ((versionInfo.dwMajorVersion == 4) &&
- (versionInfo.dwMinorVersion > 0))) {
- nType = 3; // Win98
- csOSVersion = _T("Win98");
- }
- }
- else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32s) {
- nType = 1; // Win32s
- csOSVersion = _T("Win32s");
- }
- else {
- nType = -1;
- csOSVersion = _T("Not Recognized");
- }
- return TRUE;
- }
- CString CLogIt::DisplayError(LONG lCode)
- {
-
- LPVOID lpMsgBuf;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- lCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL );// Process any inserts in lpMsgBuf.
- CString csRet = (LPCTSTR)lpMsgBuf;
- // Free the buffer.
- LocalFree( lpMsgBuf );
- // Return the string.
- return csRet;
- }
- /*
- BOOL CLogIt::GetSystemDomainUserName(void)
- {
- LPTSTR UserName = User;
- LPDWORD cchUserName = &cchUser;
- LPTSTR DomainName = Domain;
- LPDWORD cchDomainName = &cchDomain;
- HANDLE hToken;
- #define MY_BUFSIZE 512 // highly unlikely to exceed 512 bytes
- UCHAR InfoBuffer[ MY_BUFSIZE ];
- DWORD cbInfoBuffer = MY_BUFSIZE;
- SID_NAME_USE snu;
- BOOL bSuccess;
- BOOL bRet = FALSE;
- CString csMsg = _T("");
- if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken)) {
- if(GetLastError() == ERROR_NO_TOKEN) {
- //
- // attempt to open the process token, since no thread token
- // exists
- //
- if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken )) {
- csMsg.Format("Error(%u): OpenProcessToken: %s", GetLastError(), DisplayError(GetLastError()));
- WriteLog(csMsg, 0);
- return FALSE;
- }
- }
- else {
- //
- // error trying to get thread token
- //
- csMsg.Format("Error(%u): OpenThreadToken: %s", GetLastError(), DisplayError(GetLastError()));
- WriteLog(csMsg, 0);
- return FALSE;
- }
-
- bSuccess = GetTokenInformation(hToken, TokenUser, InfoBuffer, cbInfoBuffer, &cbInfoBuffer);
- if(!bSuccess) {
- csMsg.Format("Error(%u): GetTokenInformation: %s", GetLastError(), DisplayError(GetLastError()));
- WriteLog(csMsg, 0);
- return FALSE;
- }
- else {
- bRet = LookupAccountSid(NULL, ((PTOKEN_USER)InfoBuffer)->User.Sid, UserName, cchUserName, DomainName, cchDomainName, &snu );
- if (!bRet) {
- csMsg.Format("Error(%u): LookupAccountSid: %s", GetLastError(), DisplayError(GetLastError()));
- WriteLog(csMsg, 0);
- CloseHandle(hToken);
- return FALSE;
- }
- else {
- m_csDomainUserName.Format("%s\%s", DomainName, UserName);
- }
- }
- }
- return TRUE;
- }
- using namespace CryptoPP;
- CString CLogIt::EncryptString(CString csInStr, CString csPassPhrase)
- {
- unsigned int unLen = csInStr.GetLength();
- char* szOutstr;
- DefaultEncryptorWithMAC encryptor((LPCTSTR)csPassPhrase, new HexEncoder());
- encryptor.Put((byte *)(LPCTSTR)csInStr, unLen);
- encryptor.Close();
- unsigned int unOutputLength = encryptor.MaxRetrieveable();
- szOutstr = new char[unOutputLength+1];
- encryptor.Get((byte *)szOutstr, unOutputLength);
- szOutstr[unOutputLength] = 0;
- CString csRet = szOutstr;
- delete szOutstr;
- return csRet;
- }
- CString CLogIt::DecryptString(CString csInStr, CString csPassPhrase)
- {
- unsigned int unLen = csInStr.GetLength();
- char* szOutstr;
- DefaultDecryptorWithMAC *p;
- HexDecoder decryptor(p=new DefaultDecryptorWithMAC((LPCTSTR)csPassPhrase));
- decryptor.Put((byte *)(LPCTSTR)csInStr, unLen);
- decryptor.Close();
- assert(p->CurrentState() == DefaultDecryptorWithMAC::MAC_GOOD);
- unsigned int unOutputLength = decryptor.MaxRetrieveable();
- szOutstr = new char[unOutputLength+1];
- decryptor.Get((byte *)szOutstr, unOutputLength);
- szOutstr[unOutputLength] = 0;
- CString csRet = szOutstr;
- delete szOutstr;
- return csRet;
- }
- */