| OLD | NEW |
| (Empty) |
| 1 // Copyright 2010 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 // ETW log writer implementation. | |
| 17 | |
| 18 #include "omaha/base/etw_log_writer.h" | |
| 19 | |
| 20 namespace omaha { | |
| 21 | |
| 22 // {9B18BFF9-915E-4cc1-9C3E-F4AC112CB36C} | |
| 23 const GUID EtwLogWriter::kOmahaTraceGuid = | |
| 24 { 0x9b18bff9, 0x915e, 0x4cc1, | |
| 25 { 0x9c, 0x3e, 0xf4, 0xac, 0x11, 0x2c, 0xb3, 0x6c } }; | |
| 26 | |
| 27 const GUID EtwLogWriter::kLogEventId = | |
| 28 { 0x7fe69228, 0x633e, 0x4f06, | |
| 29 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; | |
| 30 | |
| 31 EtwLogWriter::EtwLogWriter(const GUID& provider_guid) | |
| 32 : EtwTraceProvider(provider_guid), | |
| 33 rtl_capture_stack_backtrace_(NULL) { | |
| 34 HMODULE kernel32 = ::GetModuleHandle(L"kernel32.dll"); | |
| 35 if (kernel32 != NULL) { | |
| 36 rtl_capture_stack_backtrace_ = | |
| 37 reinterpret_cast<RtlCaptureStackBackTraceFunc>( | |
| 38 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace")); | |
| 39 } | |
| 40 | |
| 41 EtwTraceProvider::Register(); | |
| 42 } | |
| 43 | |
| 44 void EtwLogWriter::Cleanup() { | |
| 45 EtwTraceProvider::Unregister(); | |
| 46 } | |
| 47 | |
| 48 EtwLogWriter* EtwLogWriter::Create() { | |
| 49 return new EtwLogWriter(kOmahaTraceGuid); | |
| 50 } | |
| 51 | |
| 52 bool EtwLogWriter::WantsToLogRegardless() const { | |
| 53 return session_handle() != 0; | |
| 54 } | |
| 55 | |
| 56 bool EtwLogWriter::IsCatLevelEnabled(LogCategory category, | |
| 57 LogLevel level) const { | |
| 58 if ((enable_flags() & CategoryToEnableFlag(category)) == 0 || | |
| 59 enable_level() < LogLevelToTraceLevel(level)) | |
| 60 return false; | |
| 61 | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 void EtwLogWriter::OutputMessage(const OutputInfo* output_info) { | |
| 66 if (!IsCatLevelEnabled(output_info->category, output_info->level)) | |
| 67 return; | |
| 68 | |
| 69 CStringA msg1(output_info->msg1); | |
| 70 CStringA msg2(output_info->msg2); | |
| 71 EtwEventLevel level = LogLevelToTraceLevel(output_info->level); | |
| 72 if (enable_flags() & kCaptureStackTraceMask) { | |
| 73 void* back_trace[32] = {0}; | |
| 74 DWORD depth = 0; | |
| 75 | |
| 76 if (rtl_capture_stack_backtrace_) { | |
| 77 depth = rtl_capture_stack_backtrace_(0, | |
| 78 arraysize(back_trace), | |
| 79 back_trace, | |
| 80 NULL); | |
| 81 } | |
| 82 | |
| 83 EtwMofEvent<4> mof_event(kLogEventId, kLogMessageWithStackTraceType, level); | |
| 84 mof_event.SetField(0, sizeof(depth), &depth); | |
| 85 mof_event.SetField(1, depth * sizeof(back_trace[0]), &back_trace); | |
| 86 mof_event.SetField(2, msg1.GetLength(), msg1.GetString()); | |
| 87 mof_event.SetField(3, msg2.GetLength() + 1, msg2.GetString()); | |
| 88 Log(mof_event.get()); | |
| 89 } else { | |
| 90 EtwMofEvent<2> mof_event(kLogEventId, kLogMessageType, level); | |
| 91 mof_event.SetField(0, msg1.GetLength(), msg1.GetString()); | |
| 92 mof_event.SetField(1, msg2.GetLength() + 1, msg2.GetString()); | |
| 93 Log(mof_event.get()); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void EtwLogWriter::OnEventsEnabled() { | |
| 98 CORE_LOG(L2, (_T("ETW logging enabled"))); | |
| 99 } | |
| 100 | |
| 101 void EtwLogWriter::OnEventsDisabled() { | |
| 102 CORE_LOG(L2, (_T("ETW logging disabled"))); | |
| 103 } | |
| 104 | |
| 105 EtwEventFlags EtwLogWriter::CategoryToEnableFlag(LogCategory category) { | |
| 106 // Bit zero is reserved for the capture stack trace enable flag. | |
| 107 return 1 << (category + 1); | |
| 108 } | |
| 109 | |
| 110 EtwEventLevel EtwLogWriter::LogLevelToTraceLevel(LogLevel level) { | |
| 111 switch (level) { | |
| 112 case LEVEL_FATALERROR: | |
| 113 return TRACE_LEVEL_FATAL; | |
| 114 case LEVEL_ERROR: | |
| 115 return TRACE_LEVEL_ERROR; | |
| 116 case LEVEL_WARNING: | |
| 117 return TRACE_LEVEL_WARNING; | |
| 118 case L1: | |
| 119 case L2: | |
| 120 return TRACE_LEVEL_INFORMATION; | |
| 121 case L3: | |
| 122 case L4: | |
| 123 case L5: | |
| 124 case L6: | |
| 125 return TRACE_LEVEL_VERBOSE; | |
| 126 | |
| 127 case LEVEL_ALL: | |
| 128 default: | |
| 129 return TRACE_LEVEL_NONE; | |
| 130 } | |
| 131 | |
| 132 // NOTREACHED | |
| 133 } | |
| 134 | |
| 135 } // namespace omaha | |
| OLD | NEW |