OLD | NEW |
| (Empty) |
1 // Copyright 2007-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Event Logger provides a simple mechanism to log events to Windows | |
17 // Event Log. A few overloads are defined to simplify logging by reducing | |
18 // the number of parameters that must be provided. The overloads are | |
19 // implemented in terms of the EventLogger class. | |
20 // | |
21 // The event logging works in both debug and optimized builds. This is not | |
22 // a substitute for the debug log. Instead it is a way to provide some level | |
23 // of transparency into what Google Update is doing at runtime and to help | |
24 // diagnosing end user issues. | |
25 // | |
26 // Familiarity with Windows Event Log is helpful in understanding how | |
27 // these wrappers are to be used. Windows Event Log uses localized strings | |
28 // in a message file and it substitutes string insterts that correspond to | |
29 // formatting characters in the message string. In addtion, the log is able | |
30 // to record raw data, herein provided by a context string, which may be | |
31 // useful to provide some context around the formatted message. | |
32 | |
33 // TODO(omaha): Provide some control for the verbosity level in the log. | |
34 // TODO(omaha): Perhaps there is a better way to define the overloaded | |
35 // wrappers below. I chose a compromise between the easy of use while not | |
36 // mixing up different string parameters that have different meanings. | |
37 | |
38 #ifndef OMAHA_COMMON_EVENT_LOGGER_H_ | |
39 #define OMAHA_COMMON_EVENT_LOGGER_H_ | |
40 | |
41 #include <atlstr.h> | |
42 #include "base/basictypes.h" | |
43 | |
44 namespace omaha { | |
45 | |
46 void LogEventHelper(WORD type, DWORD id, size_t count, const TCHAR** strings, | |
47 const TCHAR* ctx); | |
48 | |
49 // Logs an event to the Application log | |
50 inline void LogEvent(WORD type, DWORD id) { | |
51 LogEventHelper(type, id, 0, NULL, NULL); | |
52 } | |
53 | |
54 inline void LogEvent(WORD type, DWORD id, const TCHAR* s) { | |
55 const TCHAR* strings[] = {s}; | |
56 LogEventHelper(type, id, arraysize(strings), strings, NULL); | |
57 } | |
58 | |
59 inline void LogEvent(WORD type, DWORD id, const TCHAR* s1, const TCHAR* s2) { | |
60 const TCHAR* strings[] = {s1, s2}; | |
61 LogEventHelper(type, id, arraysize(strings), strings, NULL); | |
62 } | |
63 | |
64 inline void LogEvent(WORD type, DWORD id, const TCHAR* s1, const TCHAR* s2, | |
65 const TCHAR* s3) { | |
66 const TCHAR* strings[] = {s1, s2, s3}; | |
67 LogEventHelper(type, id, arraysize(strings), strings, NULL); | |
68 } | |
69 | |
70 // Logs an event to the Application log with a context string. | |
71 inline void LogEventContext(WORD type, DWORD id, const TCHAR* ctx) { | |
72 LogEventHelper(type, id, 0, NULL, ctx); | |
73 } | |
74 | |
75 inline void LogEventContext(WORD type, DWORD id, const TCHAR* s, | |
76 const TCHAR* ctx) { | |
77 const TCHAR* strings[] = {s}; | |
78 LogEventHelper(type, id, arraysize(strings), strings, ctx); | |
79 } | |
80 | |
81 inline void LogEventContext(WORD type, DWORD id, const TCHAR* s1, | |
82 const TCHAR* s2, const TCHAR* ctx) { | |
83 const TCHAR* strings[] = {s1, s2}; | |
84 LogEventHelper(type, id, arraysize(strings), strings, ctx); | |
85 } | |
86 | |
87 inline void LogEventContext(WORD type, DWORD id, const TCHAR* s1, | |
88 const TCHAR* s2, const TCHAR* s3, | |
89 const TCHAR* ctx) { | |
90 const TCHAR* strings[] = {s1, s2, s3}; | |
91 LogEventHelper(type, id, arraysize(strings), strings, ctx); | |
92 } | |
93 | |
94 class EventLogger { | |
95 public: | |
96 // Creates an event source for the "Application" log so that EventViewer can | |
97 // map event identifier codes to message strings. | |
98 static HRESULT AddEventSource( | |
99 const TCHAR* src_name, // Event source name. | |
100 const TCHAR* msg_dll_path); // Path for message DLL. | |
101 | |
102 static HRESULT RemoveEventSource( | |
103 const TCHAR* src_name); // Event source name. | |
104 | |
105 // Writes an entry at the end of event log that contains the source name. | |
106 static HRESULT ReportEvent( | |
107 const TCHAR* src_name, // Event source name. | |
108 WORD type, // Type of the event to be logged. | |
109 WORD category, // Event category. | |
110 DWORD id, // Event identifier. | |
111 WORD count, // Count of insert strings. | |
112 const TCHAR** strings, // Insert strings. | |
113 size_t buf_size, // Size of binary data to append. | |
114 void* buffer); // Buffer containing the binary data. | |
115 | |
116 // Reads the topmost event log record. | |
117 static HRESULT ReadLastEvent(const TCHAR* src_name, EVENTLOGRECORD* rec); | |
118 | |
119 // Default name for the event source. | |
120 static const TCHAR* const kSourceName; | |
121 | |
122 // Default event category. | |
123 static const WORD kDefaultCategory = 0; | |
124 }; | |
125 | |
126 class GoogleUpdateLogEvent { | |
127 public: | |
128 GoogleUpdateLogEvent(int type, int id, bool is_machine) | |
129 : type_(type), | |
130 id_(id), | |
131 is_machine_(is_machine) {} | |
132 GoogleUpdateLogEvent() : type_(0), id_(0), is_machine_(false) {} | |
133 ~GoogleUpdateLogEvent() {} | |
134 void WriteEvent(); | |
135 void set_event_desc(const CString& desc) { event_desc_ = desc; } | |
136 void set_event_text(const CString& text) { event_text_ = text; } | |
137 | |
138 private: | |
139 CString event_desc_; | |
140 CString event_text_; | |
141 int type_; | |
142 int id_; | |
143 bool is_machine_; | |
144 | |
145 DISALLOW_EVIL_CONSTRUCTORS(GoogleUpdateLogEvent); | |
146 }; | |
147 | |
148 } // namespace omaha | |
149 | |
150 #endif // OMAHA_COMMON_EVENT_LOGGER_H_ | |
151 | |
OLD | NEW |