Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: chrome/tools/crash_service/caps/logger_win.cc

Issue 890213002: Add basic support for CAPS exe (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6 #include <time.h>
7
8 #include "base/files/file_path.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/common/chrome_version_info_values.h"
11 #include "chrome/tools/crash_service/caps/logger_win.h"
12
13 namespace {
14 // Every message has this structure.
15 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.
16
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.
17 // Do not re-order these messages. Only add at the end.
18 const char* kMessages[] {
19 "start pid(%lu) version(%s) time(%s)", // 0
20 "exit pid(%lu) time(%s)", // 1
21 "instance found", // 2
22 };
23
24 bool WriteLogLine(HANDLE file, const std::string& txt) {
25 if (txt.empty())
26 return true;
27 DWORD written;
28 auto rc = ::WriteFile(file, txt.c_str(), txt.size(), &written, nullptr);
29 return (rc == TRUE);
30 }
31
32 // Uses |kMessages| at |index| above to write to disk a formated log line.
33 // The format is: <index>~ <tick_count> <message> ~\n"
34 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.
35 va_list ap;
36 va_start(ap, index);
37 auto fmt = base::StringPrintf(
38 kMessageHeader, index, ::GetTickCount(), kMessages[index]);
39 auto msg = base::StringPrintV(fmt.c_str(), ap);
40 auto rc = WriteLogLine(file, msg.c_str());
41 va_end(ap);
42 return rc;
43 }
44
45 // Returns the current time and date formated in standard C style.
46 std::string DateTime() {
47 time_t current_time = 0;
48 time(&current_time);
49 struct tm local_time = {0};
50 char time_buf[26] = {0};
51 localtime_s(&local_time, &current_time);
52 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.
53 time_buf[24] = '\0';
54 return time_buf;
55 }
cpu_(ooo_6.6-7.5) 2015/02/01 19:08:41 the [24] = 0 is because stupid thing puts \n\0 alw
56
57 } // namespace
58
59 namespace caps {
60
61 Logger::Logger(const base::FilePath& path) : file_(INVALID_HANDLE_VALUE) {
62 auto logfile = path.Append(L"caps_log.txt");
63 // 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.
64 // 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.
65 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
66 file_ = ::CreateFile(logfile.value().c_str(),
67 FILE_APPEND_DATA, kShareAll,
68 nullptr,
69 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
70 NULL);
scottmg 2015/02/01 19:55:35 NULL->nullptr
71 if (file_ == INVALID_HANDLE_VALUE)
72 return;
73 WriteFormatedLogLine(
74 file_, 0, ::GetCurrentProcessId(), PRODUCT_VERSION, DateTime().c_str());
75 }
76
77 Logger::~Logger() {
78 if (file_ != INVALID_HANDLE_VALUE) {
79 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
80 ::CloseHandle(file_);
81 }
82 }
83
84 } // namespace caps
85
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698