libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
EventLog.cpp
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 #include "stdafx.h"
20 
21 #include "EventLog.hpp"
22 
23 #include "../../../core/exception.hpp"
24 
25 using namespace libjmmcg;
26 using namespace libNTUtils;
27 
28 /////////////////////////////////////////////////////////////////////////////
29 
31 
32 /////////////////////////////////////////////////////////////////////////////
33 
34 namespace {
35  const TCHAR *error_msgs[]={
36  _T("Event log error."),
37  _T("Failed to register event log source."),
38  _T("Failed to get module name."),
39  _T("Failed to log event: ")
40  };
41 }
42 
43 // NOTE: This string is the embedded name of this dll!!! Don't change it!!!
44 // I need this because I can't get the name of this dll at run-time.
45 // "GetModuleHandle(...)" gives the name of the calling process if NULL is
46 // supplied which is not what I want.
47 const TCHAR module_name[]=_T("ntutils");
48 // More constants....
49 const TCHAR event_log_reg_path[]=_T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\");
50 const TCHAR event_msg_file_str[]=_T("EventMessageFile");
51 const TCHAR event_types_supported_str[]=_T("TypesSupported");
52 
53 /////////////////////////////////////////////////////////////////////////////
54 
55 inline
56 EventLog::EventLog(const tstring &event_src_name, const unsigned int types, const tstring &server, const tstring &appln_log) : event_src( NULL ) {
57  Create(event_src_name, types, server, appln_log);
58 }
59 
60 inline
61 void EventLog::Create(const tstring &event_src_name, const unsigned int types, const tstring &server, const tstring &appln_log) {
62  if (key.Create(HKEY_LOCAL_MACHINE, event_log_reg_path + appln_log + _T("\\") + event_src_name)==ERROR_SUCCESS) {
63  TCHAR mod_name[MAX_PATH];
64  if (GetModuleFileName(GetModuleHandle(module_name), mod_name, MAX_PATH)) {
65  key.SetValue(mod_name, event_msg_file_str);
66  key.SetValue(static_cast<unsigned long>(types), event_types_supported_str);
67  if (!(event_src=::RegisterEventSource(server.c_str(), event_src_name.c_str()))) {
68  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&EventLog::Create),info::function::argument(_T("const tstring &event_src_name"),event_src_name));
69  fn.add_arg(_T("const unsigned int types"),tostring(types));
70  fn.add_arg(_T("const tstring &server"),server);
71  fn.add_arg(_T("const tstring &appln_log"),appln_log);
72  throw exception_type(error_msgs[1],fn,__REV_INFO__);
73  }
74  assert(event_src);
75  } else {
76  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&EventLog::Create),info::function::argument(_T("const tstring &event_src_name"),event_src_name));
77  fn.add_arg(_T("const unsigned int types"),tostring(types));
78  fn.add_arg(_T("const tstring &server"),server);
79  fn.add_arg(_T("const tstring &appln_log"),appln_log);
80  throw exception_type(error_msgs[2],fn,__REV_INFO__);
81  }
82  }
83 }
84 
85 inline
86 void EventLog::Log(const error_types wType, const categories wCategory, const tstring &str, const unsigned long dwDataSize, void * const lpRawData, SID * const lpUserSid) {
87  assert(event_src);
88  const TCHAR *tmp=str.c_str();
89  if (!::ReportEvent(event_src, (WORD)wType, (WORD)wCategory, DEFAULT_MSG, lpUserSid, 1, dwDataSize, &tmp, lpRawData)) {
90  info::function fn(__LINE__,__PRETTY_FUNCTION__,typeid(&EventLog::Log),info::function::argument(_T("const error_types wType"),tostring(wType)));
91  fn.add_arg(_T("const categories wCategory"),tostring(wCategory));
92  fn.add_arg(_T("const tstring &str"),str);
93  fn.add_arg(_T("const unsigned long dwDataSize"),tostring(dwDataSize));
94  fn.add_arg(_T("void * const lpRawData"),tostring(lpRawData));
95  fn.add_arg(_T("SID * const lpUserSid"),tostring(lpUserSid));
96  throw exception_type(error_msgs[3],fn,__REV_INFO__);
97  }
98 }