root/experimental/NT-based/NTSpecific/EventLog.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. Log
  2. Log
  3. Delete

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/experimental/NT-based/NTSpecific/EventLog.hpp 2055 2017-05-13 19:35:47Z jmmcg $
   3 **
   4 ** Copyright (C) 2002 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #pragma once
  22 
  23 #include "RegistryKey.hpp"
  24 #include "dll/EventLogMessages.h"       // Automatically generated by "mc.exe" running on "EventLogMessages.mc"...
  25 
  26 #include "../../../core/non_copyable.hpp"
  27 
  28 namespace jmmcg {       namespace NTUtils {
  29 
  30         class AFX_EXT_CLASS EventLog : protected non_copyable {
  31         public:
  32                 typedef win_exception exception_type;
  33 
  34                 // The values assigned to the enumeration contants are "#defines" given in the automatically generated "EventLogMessages.h", which is generated from "EventLogMessages.mc".
  35                 // This enum may have to be manually updated if the categories change in the "EventLogMessages.mc" file.
  36                 enum error_types {
  37                         err_error=EVENTLOG_ERROR_TYPE,
  38                         err_warning=EVENTLOG_WARNING_TYPE,
  39                         err_information=EVENTLOG_INFORMATION_TYPE,
  40                         err_audit_success=EVENTLOG_AUDIT_SUCCESS,
  41                         err_audit_fail=EVENTLOG_AUDIT_FAILURE
  42                 };
  43                 enum categories {
  44                         cat_none=CAT_NONE,
  45                         cat_success=CAT_SUCCESS,
  46                         cat_warning=CAT_WARNING,
  47                         cat_error=CAT_ERROR,
  48                         cat_critical=CAT_CRITICAL,
  49                         cat_information=CAT_INFO,
  50                         cat_custom1=CAT_CUSTOM1,
  51                         cat_custom2=CAT_CUSTOM2,
  52                         cat_custom3=CAT_CUSTOM3,
  53                         cat_custom4=CAT_CUSTOM4,
  54                         cat_custom5=CAT_CUSTOM5
  55                 };
  56 
  57                 __stdcall EventLog();
  58                 /**
  59                         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.
  60                 */
  61                 __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"));
  62                 __stdcall ~EventLog();
  63                 // See the comment by the constructor.
  64                 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"));
  65                 void __fastcall Delete();
  66                 /**
  67                         Details of these types can be found by earching for help on the Win32 API function "::ReportEvent(...)".
  68                 */
  69                 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);
  70                 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) {
  71                         Log(wType, Category, strm.str(), dwDataSize, lpRawData, lpUserSid);
  72                 }
  73                 template<typename T> void __fastcall
  74                 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) {
  75                         assert(event_src);
  76                         std::auto_ptr<TCHAR *> strs;
  77                         if (begin!=end) {
  78                                 strs=std::auto_ptr<TCHAR *>(new TCHAR *[end-begin]);
  79                                 typename T::const_iterator i(begin);
  80                                 do {
  81                                         strs[i-begin]=new TCHAR[i->size()];
  82                                         _tcscpy(strs[i-begin], i->c_str());
  83                                 } while (++i!=end);
  84                         }
  85                         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));
  86                         if (strs.get()) {
  87                                 typename T::const_iterator i(begin);
  88                                 do {
  89                                         delete[] strs[i-begin];
  90                                 } while (++i!=end);
  91                         }
  92                         if (!ret) {
  93                                 jmmcg::info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(EventLog::Log),jmmcg::info::function::argument(_T("const error_types wType"),jmmcg::tostring(wType)));
  94                                 fn.add_arg(_T("const categories wCategory"),jmmcg::tostring(wCategory));
  95                                 tstringstream ss;
  96                                 typename T::const_iterator i(begin);
  97                                 do {
  98                                         ss<<_T("'")<<*i<<_T("'")<<std::endl;
  99                                 } while (++i!=end);
 100                                 fn.add_arg(_T("String data:"),ss.str());
 101                                 fn.add_arg(_T("const unsigned long dwDataSize"),jmmcg::tostring(dwDataSize));
 102                                 fn.add_arg(_T("void * const lpRawData"),jmmcg::tostring(lpRawData));
 103                                 fn.add_arg(_T("SID * const lpUserSid"),jmmcg::tostring(lpUserSid));
 104                                 throw exception(error_msgs[3],fn,JMMCG_REVISION(_JMMCG_REVISION_HDR));
 105                         }
 106                 }
 107 
 108         private:
 109                 RegistryKey key;
 110                 HANDLE event_src;
 111 
 112                 void operator=(const EventLog &)=delete;
 113         };
 114 
 115         inline __stdcall
 116         EventLog::EventLog()
 117         : event_src(NULL) {
 118         }
 119 
 120         inline __stdcall
 121         EventLog::~EventLog() {
 122                 Delete();
 123         }
 124 
 125         inline void __fastcall
 126         EventLog::Delete() {
 127                 ::DeregisterEventSource(event_src);
 128         }
 129 
 130 } }

/* [<][>][^][v][top][bottom][index][help] */