LogIt.cpp
上传用户:deligs
上传日期:2007-01-08
资源大小:43k
文件大小:13k
源码类别:

网络编程

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////////////
  2. // Class CLogIt Implementation File
  3. //
  4. // Created: 2/7/00
  5. //
  6. // Author: Daniel Madden
  7. //         Daniel.Madden@Compaq.Com
  8. //
  9. // History: 
  10. //
  11. // Modifications (by other programmers): 
  12. //
  13. //////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "LogIt.h"
  16. #include <direct.h>
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CLogIt Contruction
  19. CLogIt::CLogIt()
  20. {
  21. // The below value is the Pass Phrase for the encrypted "Pass Phrase"
  22. // I do it this way so that the executable doesn't have any 
  23. // plain text strings in it that tell someone what the pass phrase is
  24. 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);
  25. // Set the Log dir
  26. //m_csLogDir = _T("\\SERVER\Share\Logs\");
  27. //m_csLogDir = _T(DEFAULT_LOG_PATH);// Default Log Path
  28. m_csLogDir = _T("");
  29. m_bDirExists = FALSE;
  30. // Log file handle
  31. m_fLog = NULL;
  32. // used by the "GetSystemDomainUserName" function
  33. // cchUser = UNLEN;
  34. // cchDomain = DNLEN;
  35. // used by the "GetDomainUser" function
  36. m_csDomainUserName = _T("Domain\Username");
  37. }
  38. //**********************************************************************************
  39. //
  40. // nEntryType:
  41. //
  42. // 0 = Error
  43. // 1 = Warning
  44. // 2 = Information
  45. // 3 = Lock (Secure)
  46. // 4 = Dir Folder (Closed)
  47. // 5 = Dir Folder (Opened)
  48. // 6 = Paper (Log)
  49. // 7 = User (incognito)
  50. // 8 = Note
  51. //
  52. //**********************************************************************************
  53. void CLogIt::WriteLog(CString csMsg)
  54. {
  55. CTime ctNow = CTime::GetCurrentTime();
  56. CString csEntryTime = ctNow.Format( "%H:%M:%S" );
  57. CFile cfLog;
  58. CFileException e;
  59. // Write to it 
  60. // even if it's being writing to!
  61. // because we are using CFile
  62. if(TRUE){//!IsOpen()) {
  63. CString csText = _T("");
  64. if (m_bDirExists) {
  65. if( !cfLog.Open( m_csFullLogName, 
  66. CFile::modeCreate | 
  67. CFile::modeNoTruncate | 
  68. CFile::shareDenyNone | 
  69. CFile::modeWrite, &e ) )   
  70. {
  71. #ifdef _DEBUG   
  72. afxDump << "File could not be opened " << e.m_cause << "n";
  73. #endif   
  74. return;
  75. }
  76. csText.Format("%s - %srn", csEntryTime, csMsg);
  77. cfLog.SeekToEnd();
  78. cfLog.Write(csText, csText.GetLength());
  79. cfLog.Flush();
  80. cfLog.Close();
  81. }
  82. else {
  83. csText.Format("Logging Directory doesn't exist [%s]!", m_csLogDir);
  84. MessageBox(NULL, csText, "Error: Accessing Log Directory", MB_OK);
  85. }
  86. }
  87. }
  88. void CLogIt::WriteLogOld(CString csMsg)
  89. {
  90. CTime ctNow = CTime::GetCurrentTime();
  91. CString csEntryTime = ctNow.Format( "%H:%M:%S" );
  92. // We don't want to write to it 
  93. // if it's being writing to!
  94. if (!IsOpen()) {
  95. CString csText = _T("");
  96. if (m_bDirExists) {
  97. m_fLog = fopen(m_csFullLogName, "a");
  98. if (m_fLog != NULL) {
  99. csText.Format("%s - %s", csEntryTime, csMsg);
  100. fprintf(m_fLog, "%sn", csText);
  101. }
  102. int nClosed = fclose(m_fLog);
  103. if (nClosed == 0)
  104. m_fLog = NULL;
  105. else
  106. MessageBox(NULL, "Problem closing log file", "Error: Closing file", MB_OK);
  107. Sleep(100); // Give it time to close
  108. }
  109. else {
  110. csText.Format("Logging Directory doesn't exist [%s]!", m_csLogDir);
  111. MessageBox(NULL, csText, "Error: Accessing Log Directory", MB_OK);
  112. }
  113. }
  114. }
  115. BOOL CLogIt::CreateLogDirectory(CString csDirName)
  116. {
  117. BOOL bRet = CreateDirectory(csDirName, NULL);
  118. if (!bRet) {
  119. DWORD dwErrNum = GetLastError();
  120. if (dwErrNum != 183) {
  121. CString csErrTitle;
  122. csErrTitle.Format("Error: %u", dwErrNum);
  123. MessageBox(NULL, DisplayError(dwErrNum), csErrTitle, MB_OK|MB_ICONSTOP);
  124. }
  125. else 
  126. bRet = TRUE;
  127. }
  128. return bRet;
  129. }
  130. BOOL CLogIt::CreateLogPath(void)
  131. {
  132. if (!m_csLogDir.IsEmpty()) {
  133. // int's
  134. int n = -1;
  135. // CString's
  136. CString csDir = _T("");
  137. // CStringArray's
  138. CStringArray csaLogPath;
  139. // Gets the system time.
  140. CTime ctNow = CTime::GetCurrentTime();
  141. // Format the Month Directory name
  142. //
  143. // Formatted to: "July2000.logs"
  144. CString csMonthDir = ctNow.Format("%B%Y.logs\");
  145. int nHour = ctNow.GetHour();
  146. int nMinute = ctNow.GetMinute();
  147. int nSecond = ctNow.GetSecond();
  148. int nDay = ctNow.GetDay();
  149. int nMonth = ctNow.GetMonth();
  150. int nYear = ctNow.GetYear();
  151. m_csLogEntryTime = _T("");
  152. // Formatted to: "09:20:54"
  153. m_csLogEntryTime.Format("%.2i:%.2i:%.2i", nHour, nMinute, nSecond);
  154. // Formatted to: "lg03072000.log"
  155. m_csLogName.Format("log%.2i%.2i%i.log", nMonth, nDay, nYear);
  156. // Formatted to: "<Your Choice>July2000.logs"
  157. m_csLogPath = m_csLogDir + csMonthDir;
  158. // Formatted to: "<Your Choice>July2000.logslg03072000.log"
  159. m_csFullLogName = m_csLogDir + csMonthDir + m_csLogName;
  160. //////////////////////////////
  161. // Begin the work!
  162. //////////////////////////////
  163. //
  164. // put directory structure into an array (CString)
  165. Split(m_csFullLogName, _T("\"), csaLogPath, FALSE);
  166. // Get the Drive (or Server) name
  167. CString csTmp = csaLogPath.GetAt(0);
  168. int nUBound = csaLogPath.GetUpperBound();
  169. // Notice "n<nUBound" below...
  170. // the path I Split, contains a filename and
  171. // that is not needed when creating the Dir Structure
  172. //
  173. // If you wanted just the filename from the path...just do this:
  174. //
  175. // CString csFilename = csaLogPath.GetAt(csaLogPath.GetUpperBound());
  176. //
  177. for (n=0; n<nUBound; n++) {
  178. if (n == 0) {
  179. if ( (csTmp.Mid(1, 1) == ":") || (csTmp.Mid(0, 1) == ".")) { // We are working with (C:) format
  180. // Format to:  "C:"
  181. csDir = csaLogPath.GetAt(n) + "\";
  182. }
  183. else { // We are working with a "\Servershare"...
  184. // Format to:  "\SERVER"
  185. csDir = _T("\\");
  186. csDir += csaLogPath.GetAt(n) + "\";
  187. }
  188. }
  189. else {
  190. csDir += csaLogPath.GetAt(n) + "\";
  191. if ((csTmp.Mid(1, 1) == ":") || (csTmp.Mid(0, 1) == ".")) { // We are working with (C:) format
  192. // Format to:  "C:<Dir>..."
  193. //
  194. // and create it
  195. if (!CreateLogDirectory(csDir))
  196. return FALSE;
  197. }
  198. else { // We are working with a "\Servershare"...
  199. if (n == 1) { // Share Name
  200. // Format to:  "\SERVERShare"
  201. csDir += csaLogPath.GetAt(n) + "\";
  202. }
  203. else {
  204. // Format to:  "\SERVERShare<Dir>..."
  205. //
  206. // and create it
  207. csDir += csaLogPath.GetAt(n) + "\";
  208. if (!CreateLogDirectory(csDir))
  209. return FALSE;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. else {
  216. MessageBox(NULL, "You must have a directory name to Create the Log Path!", "Error: CreateLogPath", MB_OK|MB_ICONSTOP);
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }
  221. BOOL CLogIt::CreateLogPath(CString csLogDir)
  222. {
  223. // Simply sets the 'm_csLogDir' variable
  224. // and calls 'CreateLogPath()'
  225. BOOL bRet = FALSE;
  226. if (!csLogDir.IsEmpty()) {
  227. m_csLogDir = csLogDir;
  228. bRet = CreateLogPath();
  229. }
  230. m_bDirExists = bRet;
  231. return bRet;
  232. }
  233. void CLogIt::Split(CString Source, CString Deliminator, CStringArray& AddIt, BOOL bAddEmpty)
  234. {
  235. // initialize the variables
  236. CString  newCString = Source;
  237. CString  tmpCString = "";
  238. CString  AddCString = "";
  239. int pos1 = 0;
  240. int pos = 0;
  241. AddIt.RemoveAll();
  242. if (Deliminator.IsEmpty()) {
  243. // Add default [comma] if empty!
  244. // acknowledgement: Prasad [gprasad@rti.ie]
  245. Deliminator = ","; 
  246. }
  247. // do this loop as long as you have a deliminator
  248. do {
  249. // set to zero
  250. pos1 = 0;
  251. // position of deliminator starting at pos1 (0)
  252. pos = newCString.Find(Deliminator, pos1);
  253. // if the deliminator is found...
  254. if ( pos != -1 ) {
  255. // load a new var with the info left
  256. // of the position
  257. CString AddCString = newCString.Left(pos);// - 1);
  258. if (!AddCString.IsEmpty()) {
  259. // if there is a string to add, then
  260. // add it to the Array
  261. AddIt.Add(AddCString);
  262. }
  263. else if (bAddEmpty) {
  264. // if empty strings are ok, then add them
  265. AddIt.Add(AddCString);
  266. }
  267. // make a copy of the of this var. with the info
  268. // right of the deliminator
  269. tmpCString = newCString.Mid(pos + Deliminator.GetLength());
  270. // reset this var with new info
  271. newCString = tmpCString;
  272. }
  273. } while ( pos != -1 );
  274. if (!newCString.IsEmpty()) {
  275. // as long as the variable is not emty, add it
  276. AddIt.Add(newCString);
  277. }
  278. }
  279. BOOL CLogIt::GetOSType(CString& csOSVersion, int& nType)
  280. {
  281. OSVERSIONINFO versionInfo;
  282. CString csMsg = _T("");
  283. // set the size of OSVERSIONINFO, before calling the function
  284. versionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  285. // Get the version information
  286. if (!::GetVersionEx (&versionInfo)) {
  287. CString csErrMsg = _T("");;
  288. csMsg.Format("Error(%u): GetVersionEx: %s", GetLastError(), DisplayError(GetLastError()));
  289. WriteLog(csMsg);
  290. return FALSE;
  291. }
  292. // Check which OS it is
  293. if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
  294. // Windows NT test
  295. if ((versionInfo.dwMajorVersion > 4) && (versionInfo.dwMinorVersion >= 0)) {
  296. nType = 5; 
  297. csOSVersion = _T("Win2000");
  298. }
  299. else if ((versionInfo.dwMajorVersion == 4) && (versionInfo.dwMinorVersion >= 0)) {
  300. nType = 4; 
  301. csOSVersion = _T("WinNT4");
  302. }
  303. }
  304. else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
  305. nType = 2; // Win95
  306. csOSVersion = _T("Win95");
  307. // Windows 98 test
  308. if ((versionInfo.dwMajorVersion > 4) || ((versionInfo.dwMajorVersion == 4) &&
  309. (versionInfo.dwMinorVersion > 0))) {
  310. nType = 3; // Win98
  311. csOSVersion = _T("Win98");
  312. }
  313. }
  314. else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32s) {
  315. nType = 1; // Win32s
  316. csOSVersion = _T("Win32s");
  317. }
  318. else {
  319. nType = -1;
  320. csOSVersion = _T("Not Recognized");
  321. }
  322. return TRUE;
  323. }
  324. CString CLogIt::DisplayError(LONG lCode)
  325. {
  326. LPVOID lpMsgBuf;
  327. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
  328.           NULL,
  329.   lCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  330.   (LPTSTR) &lpMsgBuf,
  331.   0,
  332.   NULL );// Process any inserts in lpMsgBuf.
  333. CString csRet = (LPCTSTR)lpMsgBuf;
  334. // Free the buffer.
  335. LocalFree( lpMsgBuf );
  336. // Return the string.
  337. return csRet;
  338. }
  339. /*
  340. BOOL CLogIt::GetSystemDomainUserName(void)
  341. {
  342. LPTSTR UserName = User;
  343. LPDWORD cchUserName = &cchUser;
  344. LPTSTR DomainName = Domain;
  345. LPDWORD cchDomainName = &cchDomain;
  346. HANDLE hToken;
  347. #define MY_BUFSIZE 512  // highly unlikely to exceed 512 bytes
  348. UCHAR InfoBuffer[ MY_BUFSIZE ];
  349. DWORD cbInfoBuffer = MY_BUFSIZE;
  350. SID_NAME_USE snu;
  351. BOOL bSuccess;
  352. BOOL bRet = FALSE;
  353. CString csMsg = _T("");
  354. if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken)) {
  355. if(GetLastError() == ERROR_NO_TOKEN) {
  356. //
  357. // attempt to open the process token, since no thread token
  358. // exists
  359. //
  360. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken )) {
  361. csMsg.Format("Error(%u): OpenProcessToken: %s", GetLastError(), DisplayError(GetLastError()));
  362. WriteLog(csMsg, 0);
  363. return FALSE;
  364. }
  365. else {
  366.     //
  367.     // error trying to get thread token
  368.     //
  369. csMsg.Format("Error(%u): OpenThreadToken: %s", GetLastError(), DisplayError(GetLastError()));
  370. WriteLog(csMsg, 0);
  371.     return FALSE;
  372. }
  373. bSuccess = GetTokenInformation(hToken, TokenUser, InfoBuffer, cbInfoBuffer, &cbInfoBuffer);
  374. if(!bSuccess) {
  375. csMsg.Format("Error(%u): GetTokenInformation: %s", GetLastError(), DisplayError(GetLastError()));
  376. WriteLog(csMsg, 0);
  377. return FALSE;
  378. }
  379. else {
  380. bRet = LookupAccountSid(NULL, ((PTOKEN_USER)InfoBuffer)->User.Sid, UserName, cchUserName, DomainName, cchDomainName, &snu );
  381. if (!bRet) {
  382. csMsg.Format("Error(%u): LookupAccountSid: %s", GetLastError(), DisplayError(GetLastError()));
  383. WriteLog(csMsg, 0);
  384. CloseHandle(hToken);
  385. return FALSE;
  386. }
  387. else {
  388. m_csDomainUserName.Format("%s\%s", DomainName, UserName);
  389. }
  390. }
  391. }
  392. return TRUE;
  393. }
  394. using namespace CryptoPP;
  395. CString CLogIt::EncryptString(CString csInStr, CString csPassPhrase)
  396. {
  397. unsigned int unLen = csInStr.GetLength();
  398. char* szOutstr;
  399. DefaultEncryptorWithMAC encryptor((LPCTSTR)csPassPhrase, new HexEncoder());
  400. encryptor.Put((byte *)(LPCTSTR)csInStr, unLen);
  401. encryptor.Close();
  402. unsigned int unOutputLength = encryptor.MaxRetrieveable();
  403. szOutstr = new char[unOutputLength+1];
  404. encryptor.Get((byte *)szOutstr, unOutputLength);
  405. szOutstr[unOutputLength] = 0;
  406. CString csRet = szOutstr;
  407.     delete szOutstr;
  408. return csRet;
  409. }
  410. CString CLogIt::DecryptString(CString csInStr, CString csPassPhrase)
  411. {
  412. unsigned int unLen = csInStr.GetLength();
  413. char* szOutstr;
  414. DefaultDecryptorWithMAC *p;
  415. HexDecoder decryptor(p=new DefaultDecryptorWithMAC((LPCTSTR)csPassPhrase));
  416. decryptor.Put((byte *)(LPCTSTR)csInStr, unLen);
  417. decryptor.Close();
  418. assert(p->CurrentState() == DefaultDecryptorWithMAC::MAC_GOOD);
  419. unsigned int unOutputLength = decryptor.MaxRetrieveable();
  420. szOutstr = new char[unOutputLength+1];
  421. decryptor.Get((byte *)szOutstr, unOutputLength);
  422. szOutstr[unOutputLength] = 0;
  423. CString csRet = szOutstr;
  424.     delete szOutstr;
  425. return csRet;
  426. }
  427. */