C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,801 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,658 people online right now. Registration is fast and FREE... Join Now!





EventLogger Class

Class programmed in C++ to log events & errors utilizing the C++ Standard Library.

Submitted By: DangerD
Actions:
Rating:
Views: 92

Language: C++

Last Modified: November 9, 2009
Instructions: I know there is alot of room for improvement... If you see ways to better my code or have any criticisms please post them. I was programming this class using wstrings to support UNICODE and when I got to printing to the file with wofstream there seems to be some kind of bug. Long story short the logging part is just regular old string and fstream.

Simple example for it's usage...

CODE

#include <windows.h>
#include "EventLogger.h"
EventLogger Log;

BOOL WINAPI DllMain(
HANDLE hinstDLL,
DWORD dwReason,
LPVOID lpvReserved
)
{
if(dwReason == DLL_PROCESS_ATTACH)
{
// initialize things we need for logging
// get the our dll's path
Log.FindFilePath(hinstDLL);
// replace the dll file name with log file name
Log.ReplaceFileName(Log.FilePath(), _T("EventLog.txt"));
// Open File for writing...
Log.OpenFile(Log.FilePath());
// write to the log!
Log.LogEvent("Attached to process!");
}

return 0;
}

Snippet


  1. [code]
  2. //////////////////////////////////////////////////////////////////////////
  3. // EventLog Class
  4. // by DangerD (DangerD@live.com)
  5. //////////////////////////////////////////////////////////////////////////
  6. // EventLog.h
  7.  
  8. #ifndef EVENTLOGGER_H
  9. #define EVENTLOGGER_H
  10.  
  11. #include <string>
  12. #include <fstream>
  13.  
  14. class EventLogger
  15. {
  16. public:
  17.         EventLogger();                // constructor
  18.         ~EventLogger();        // ~destructor
  19.         std::wstring FilePath();
  20.         bool FindFilePath(HMODULE hModule);
  21.         void ReplaceFileExt(std::wstring& FilePath,
  22.                 const std::wstring& NewExt);
  23.         void ReplaceFileName(std::wstring& FilePath,
  24.                 const std::wstring& NewName);
  25.         bool OpenFile(const std::wstring& FilePath);
  26.         std::string GetTime();
  27.         void LogEvent(const std::string& Event);
  28.  
  29. private:
  30.         std::wstring m_FilePath;
  31.         std::string m_Time;
  32.         std::ofstream m_File;
  33. };
  34.  
  35. #endif
  36.  
  37. // end of file
  38. //////////////////////////////////////////////////////////////////////////
  39. [/code]
  40. [code]
  41. //////////////////////////////////////////////////////////////////////////
  42. // EventLog Class
  43. // by DangerD (DangerD@live.com)
  44. //////////////////////////////////////////////////////////////////////////
  45. // EventLog.cpp
  46.  
  47. #include <Windows.h>
  48. #include <tchar.h>
  49. #include <time.h>
  50. //---
  51. #include <iostream>
  52. #include <fstream>
  53. #include <string>
  54. #include <vector>
  55. //---
  56. #include "EventLogger.h"
  57.  
  58. // constructor
  59. EventLogger::EventLogger()
  60. {
  61.  
  62. }
  63.  
  64. // ~destructor
  65. EventLogger::~EventLogger()
  66. {
  67.         m_File.close();
  68. }
  69.  
  70. std::wstring EventLogger::FilePath()
  71. {
  72.         return m_FilePath;
  73. }
  74.  
  75. bool EventLogger::FindFilePath(HMODULE hModule)
  76. {
  77.         std::vector<TCHAR> Path(MAX_PATH);
  78.         if(!GetModuleFileName(hModule, &Path[0], Path.size())) {
  79.                 // you can change this for however your program handles errors
  80.                 std::wcerr << _T("Error: Couldn't get file path.") << std::endl;
  81.                 return false;
  82.         }
  83.         m_FilePath = &Path[0];
  84.         return true;
  85. }
  86.  
  87. void EventLogger::ReplaceFileExt(std::wstring& FilePath,
  88.                                                           const std::wstring& NewExt)
  89. {
  90.         int Length = FilePath.length();
  91.         int FileExt = Length - 4;
  92.         FilePath.replace(FileExt, Length, NewExt);
  93.         m_FilePath = FilePath;
  94. }
  95.  
  96. void EventLogger::ReplaceFileName(std::wstring& FilePath,
  97.                                                            const std::wstring& NewName)
  98. {
  99.         int Pos = FilePath.rfind(_T("\\")); ++Pos;
  100.         FilePath.erase(Pos, std::string::npos);
  101.         FilePath.append(NewName);
  102.         m_FilePath = FilePath;
  103. }
  104.  
  105. bool EventLogger::OpenFile(const std::wstring& FilePath)
  106. {
  107.         m_File.open(FilePath.c_str(), std::ios::out | std::ios::app);
  108.         if (!m_File) {
  109.                 // you can change this for however your program handles errors
  110.                 std::wcerr << _T("Error: Couldn't open file!") << std::endl;
  111.                 return false;
  112.         }
  113.         return true;
  114. }
  115.  
  116. std::string EventLogger::GetTime()
  117. {
  118.         time_t rawtime;
  119.         struct tm timeinfo;
  120.         std::vector<char> Time(MAX_PATH);
  121.         time(&rawtime);
  122.         localtime_s(&timeinfo, &rawtime);
  123.         strftime(&Time[0], Time.size(),"%I:%M%p-> ", &timeinfo);
  124.         m_Time = &Time[0];
  125.         return m_Time;
  126. }
  127.  
  128. void EventLogger::LogEvent(const std::string& Event)
  129. {
  130.         GetTime();
  131.         m_File << m_Time << Event << std::endl;
  132. }
  133.  
  134. // end of file
  135. //////////////////////////////////////////////////////////////////////////
  136. [/code]

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month