libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
EventLog.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #pragma once
20 
21 #include "RegistryKey.hpp"
22 #include "dll/EventLogMessages.h" // Automatically generated by "mc.exe" running on "EventLogMessages.mc"...
23 
24 #include "../../../core/non_copyable.hpp"
25 
26 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace NTUtils {
27 
28  class AFX_EXT_CLASS EventLog : protected non_copyable {
29  public:
31 
32  // The values assigned to the enumeration contants are "#defines" given in the automatically generated "EventLogMessages.h", which is generated from "EventLogMessages.mc".
33  // This enum may have to be manually updated if the categories change in the "EventLogMessages.mc" file.
34  enum error_types {
40  };
41  enum categories {
53  };
54 
55  __stdcall EventLog();
56  /**
57  Note that if the "EventLog" object accesses a remote server, it must run under an account that has the correct permissions to access the Event Log on the server, otherwise you'll get access violations & permission violations. The "system" account does not have network access so the local server should be used, i.e. pass "NULL", the default.
58  */
59  __stdcall EventLog(const tstring &event_src_name, const unsigned int types = err_error|err_warning|err_information, const tstring &server = _T(""), const tstring &appln_log = _T("Application"));
60  __stdcall ~EventLog();
61  // See the comment by the constructor.
62  void __fastcall Create(const tstring &event_src_name, const unsigned int types = err_error|err_warning|err_information, const tstring &server = _T(""), const tstring &appln_log = _T("Application"));
63  void __fastcall Delete();
64  /**
65  Details of these types can be found by earching for help on the Win32 API function "::ReportEvent(...)".
66  */
67  void __fastcall Log(const error_types wType, const categories Category, const tstring &str, const unsigned long dwDataSize = 0, void * const lpRawData = NULL, SID * const lpUserSid = NULL);
68  void __fastcall Log(const error_types wType, const categories Category, const tstringstream &strm, const unsigned long dwDataSize = 0, void * const lpRawData = NULL, SID * const lpUserSid = NULL) {
70  }
71  template<typename T> void __fastcall
72  Log(const error_types wType, const categories wCategory, const typename T::const_iterator &begin, const typename T::const_iterator &end, const unsigned long dwDataSize, void * const lpRawData, SID * const lpUserSid) {
73  assert(event_src);
74  std::auto_ptr<TCHAR *> strs;
75  if (begin!=end) {
76  strs=std::auto_ptr<TCHAR *>(new TCHAR *[end-begin]);
77  typename T::const_iterator i(begin);
78  do {
79  strs[i-begin]=new TCHAR[i->size()];
80  _tcscpy(strs[i-begin], i->c_str());
81  } while (++i!=end);
82  }
83  const bool ret=static_cast<bool>(::ReportEvent(event_src, (WORD)wType, (WORD)wCategory, DEFAULT_MSG, lpUserSid, end-begin, dwDataSize, const_cast<const TCHAR **>(strs.get()), lpRawData));
84  if (strs.get()) {
85  typename T::const_iterator i(begin);
86  do {
87  delete[] strs[i-begin];
88  } while (++i!=end);
89  }
90  if (!ret) {
91  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(EventLog::Log),info::function::argument(_T("const error_types wType"),tostring(wType)));
92  fn.add_arg(_T("const categories wCategory"),tostring(wCategory));
94  typename T::const_iterator i(begin);
95  do {
96  ss<<_T("'")<<*i<<_T("'")<<std::endl;
97  } while (++i!=end);
98  fn.add_arg(_T("String data:"),ss.str());
99  fn.add_arg(_T("const unsigned long dwDataSize"),tostring(dwDataSize));
100  fn.add_arg(_T("void * const lpRawData"),tostring(lpRawData));
101  fn.add_arg(_T("SID * const lpUserSid"),tostring(lpUserSid));
103  }
104  }
105 
106  private:
109 
110  void operator=(const EventLog &)=delete;
111  };
112 
113  inline __stdcall
115  : event_src(NULL) {
116  }
117 
118  inline __stdcall
120  Delete();
121  }
122 
123  inline void __fastcall
126  }
127 
128 } } }