| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-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 #include "omaha/tools/goopdump/dump_log.h" | |
| 17 | |
| 18 #include <vector> | |
| 19 | |
| 20 #include "omaha/common/debug.h" | |
| 21 #include "omaha/common/file.h" | |
| 22 | |
| 23 namespace omaha { | |
| 24 | |
| 25 DumpLogHandler::DumpLogHandler() { | |
| 26 } | |
| 27 | |
| 28 DumpLogHandler::~DumpLogHandler() { | |
| 29 } | |
| 30 | |
| 31 | |
| 32 ConsoleDumpLogHandler::ConsoleDumpLogHandler() { | |
| 33 } | |
| 34 | |
| 35 ConsoleDumpLogHandler::~ConsoleDumpLogHandler() { | |
| 36 } | |
| 37 | |
| 38 void ConsoleDumpLogHandler::WriteLine(const TCHAR* line) { | |
| 39 _tprintf(_T("%s"), line); | |
| 40 } | |
| 41 | |
| 42 | |
| 43 DebugDumpLogHandler::DebugDumpLogHandler() { | |
| 44 } | |
| 45 | |
| 46 DebugDumpLogHandler::~DebugDumpLogHandler() { | |
| 47 } | |
| 48 | |
| 49 void DebugDumpLogHandler::WriteLine(const TCHAR* line) { | |
| 50 ::OutputDebugString(line); | |
| 51 } | |
| 52 | |
| 53 | |
| 54 FileDumpLogHandler::FileDumpLogHandler() { | |
| 55 } | |
| 56 | |
| 57 FileDumpLogHandler::~FileDumpLogHandler() { | |
| 58 } | |
| 59 | |
| 60 void FileDumpLogHandler::set_filename(const TCHAR* filename) { | |
| 61 filename_ = filename; | |
| 62 if (File::Exists(filename_)) { | |
| 63 File::Remove(filename_); | |
| 64 | |
| 65 // Write the UNICODE file marker at the beginning. | |
| 66 char buf[2] = {0xff, 0xfe}; | |
| 67 WriteBufToFile(buf, 2); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void FileDumpLogHandler::WriteLine(const TCHAR* line) { | |
| 72 if (filename_.IsEmpty()) { | |
| 73 return; | |
| 74 } | |
| 75 | |
| 76 WriteBufToFile(line, _tcslen(line) * sizeof(TCHAR)); | |
| 77 } | |
| 78 | |
| 79 void FileDumpLogHandler::WriteBufToFile(const void* buf, | |
| 80 DWORD num_bytes_to_write) { | |
| 81 HANDLE h = ::CreateFile(filename_, | |
| 82 GENERIC_WRITE, | |
| 83 FILE_SHARE_READ, | |
| 84 NULL, | |
| 85 OPEN_ALWAYS, | |
| 86 FILE_ATTRIBUTE_NORMAL, | |
| 87 NULL); | |
| 88 if (h == INVALID_HANDLE_VALUE) { | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 ::SetFilePointer(h, NULL, NULL, FILE_END); | |
| 93 DWORD bytes_written = 0; | |
| 94 ::WriteFile(h, buf, num_bytes_to_write, &bytes_written, NULL); | |
| 95 ::CloseHandle(h); | |
| 96 } | |
| 97 | |
| 98 | |
| 99 DumpLog::DumpLog() { | |
| 100 } | |
| 101 | |
| 102 DumpLog::~DumpLog() { | |
| 103 } | |
| 104 | |
| 105 void DumpLog::EnableConsole(bool enable) { | |
| 106 if (enable) { | |
| 107 AddLogHandler(&console_handler_); | |
| 108 } else { | |
| 109 RemoveLogHandler(&console_handler_); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void DumpLog::EnableDebug(bool enable) { | |
| 114 if (enable) { | |
| 115 AddLogHandler(&debug_handler_); | |
| 116 } else { | |
| 117 RemoveLogHandler(&debug_handler_); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void DumpLog::AddLogHandler(DumpLogHandler* log_handler) { | |
| 122 ASSERT1(log_handler); | |
| 123 std::vector<DumpLogHandler*>::iterator it = log_handlers_.begin(); | |
| 124 for (; it != log_handlers_.end(); ++it) { | |
| 125 DumpLogHandler* handler = *it; | |
| 126 if (handler == log_handler) { | |
| 127 return; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 log_handlers_.push_back(log_handler); | |
| 132 } | |
| 133 | |
| 134 void DumpLog::RemoveLogHandler(DumpLogHandler* log_handler) { | |
| 135 ASSERT1(log_handler); | |
| 136 std::vector<DumpLogHandler*>::iterator it = log_handlers_.begin(); | |
| 137 for (; it != log_handlers_.end(); ++it) { | |
| 138 DumpLogHandler* handler = *it; | |
| 139 if (handler == log_handler) { | |
| 140 log_handlers_.erase(it); | |
| 141 return; | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void DumpLog::WriteLine(const TCHAR* format, ...) const { | |
| 147 va_list arg_list; | |
| 148 va_start(arg_list, format); | |
| 149 | |
| 150 CString line; | |
| 151 line.FormatV(format, arg_list); | |
| 152 line.Append(_T("\r\n")); | |
| 153 | |
| 154 std::vector<DumpLogHandler*>::const_iterator it = log_handlers_.begin(); | |
| 155 for (; it != log_handlers_.end(); ++it) { | |
| 156 DumpLogHandler* handler = *it; | |
| 157 handler->WriteLine(line); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 } // namespace omaha | |
| 162 | |
| OLD | NEW |