Chromium Code Reviews| Index: chrome/tools/crash_service/caps/logger_win.cc |
| diff --git a/chrome/tools/crash_service/caps/logger_win.cc b/chrome/tools/crash_service/caps/logger_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c8c3f3936cd3a6706ce5617da8d3b0f71084723 |
| --- /dev/null |
| +++ b/chrome/tools/crash_service/caps/logger_win.cc |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <windows.h> |
| +#include <time.h> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "chrome/common/chrome_version_info_values.h" |
| +#include "chrome/tools/crash_service/caps/logger_win.h" |
| + |
| +namespace { |
| +// Every message has this structure. |
| +const char kMessageHeader[] = "%03d~ %04x %s ~\n"; |
|
scottmg
2015/02/01 19:55:35
GetTickCount will normally be larger than 4 digits
cpu_(ooo_6.6-7.5)
2015/02/03 02:44:58
Done.
|
| + |
|
cpu_(ooo_6.6-7.5)
2015/02/01 19:08:41
my loggers are never free form, that is, you can a
scottmg
2015/02/01 19:55:35
until version has a ')' in it!
cpu_(ooo_6.6-7.5)
2015/02/03 02:44:58
Acknowledged.
|
| +// Do not re-order these messages. Only add at the end. |
| +const char* kMessages[] { |
| + "start pid(%lu) version(%s) time(%s)", // 0 |
| + "exit pid(%lu) time(%s)", // 1 |
| + "instance found", // 2 |
| +}; |
| + |
| +bool WriteLogLine(HANDLE file, const std::string& txt) { |
| + if (txt.empty()) |
| + return true; |
| + DWORD written; |
| + auto rc = ::WriteFile(file, txt.c_str(), txt.size(), &written, nullptr); |
| + return (rc == TRUE); |
| +} |
| + |
| +// Uses |kMessages| at |index| above to write to disk a formated log line. |
| +// The format is: <index>~ <tick_count> <message> ~\n" |
| +bool WriteFormatedLogLine(HANDLE file, size_t index, ...) { |
|
scottmg
2015/02/01 19:55:35
Formated -> Formatted, and in comments
cpu_(ooo_6.6-7.5)
2015/02/03 02:44:58
Done.
|
| + va_list ap; |
| + va_start(ap, index); |
| + auto fmt = base::StringPrintf( |
| + kMessageHeader, index, ::GetTickCount(), kMessages[index]); |
| + auto msg = base::StringPrintV(fmt.c_str(), ap); |
| + auto rc = WriteLogLine(file, msg.c_str()); |
| + va_end(ap); |
| + return rc; |
| +} |
| + |
| +// Returns the current time and date formated in standard C style. |
| +std::string DateTime() { |
| + time_t current_time = 0; |
| + time(¤t_time); |
| + struct tm local_time = {0}; |
| + char time_buf[26] = {0}; |
| + localtime_s(&local_time, ¤t_time); |
| + asctime_s(time_buf, arraysize(time_buf), &local_time); |
|
scottmg
2015/02/01 19:55:35
you can just remove the arraysize arg, as there's
cpu_(ooo_6.6-7.5)
2015/02/03 02:44:58
Done.
|
| + time_buf[24] = '\0'; |
| + return time_buf; |
| +} |
|
cpu_(ooo_6.6-7.5)
2015/02/01 19:08:41
the [24] = 0 is because stupid thing puts \n\0 alw
|
| + |
| +} // namespace |
| + |
| +namespace caps { |
| + |
| +Logger::Logger(const base::FilePath& path) : file_(INVALID_HANDLE_VALUE) { |
| + auto logfile = path.Append(L"caps_log.txt"); |
| + // opening a file like so allows atomic appends, but can't be used |
|
scottmg
2015/02/01 19:55:35
capital Opening
cpu_(ooo_6.6-7.5)
2015/02/03 02:44:58
Done.
|
| + // for anything else. |
|
cpu_(ooo_6.6-7.5)
2015/02/01 19:08:41
I am having bruce change our logging to use This O
scottmg
2015/02/01 19:55:35
Nice, hadn't used that before.
|
| + auto kShareAll = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
|
scottmg
2015/02/01 19:55:35
I find all these auto's a little frightening, but
|
| + file_ = ::CreateFile(logfile.value().c_str(), |
| + FILE_APPEND_DATA, kShareAll, |
| + nullptr, |
| + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, |
| + NULL); |
|
scottmg
2015/02/01 19:55:35
NULL->nullptr
|
| + if (file_ == INVALID_HANDLE_VALUE) |
| + return; |
| + WriteFormatedLogLine( |
| + file_, 0, ::GetCurrentProcessId(), PRODUCT_VERSION, DateTime().c_str()); |
| +} |
| + |
| +Logger::~Logger() { |
| + if (file_ != INVALID_HANDLE_VALUE) { |
| + WriteFormatedLogLine(file_, 1, ::GetCurrentProcessId(), DateTime().c_str()); |
|
scottmg
2015/02/01 19:55:35
The 0, 1, etc. seem a bit non-obvious. I don't und
cpu_(ooo_6.6-7.5)
2015/02/03 02:44:58
So the idea is that the logger will have methods w
|
| + ::CloseHandle(file_); |
| + } |
| +} |
| + |
| +} // namespace caps |
| + |