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

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: review fixes 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 // <index>~ <tick_count> <message> ~\n"
16 const char kMessageHeader[] = "%03d~ %08x %s ~\n";
17
18 // Do not re-order these messages. Only add at the end.
19 const char* kMessages[] {
20 "start pid(%lu) version(%s) time(%s)", // 0
21 "exit pid(%lu) time(%s)", // 1
22 "instance found", // 2
23 };
24
25 bool WriteLogLine(HANDLE file, const std::string& txt) {
26 if (txt.empty())
27 return true;
28 DWORD written;
29 auto rc = ::WriteFile(file, txt.c_str(), txt.size(), &written, nullptr);
30 return (rc == TRUE);
31 }
32
33 // Uses |kMessages| at |index| above to write to disk a formatted log line.
34 bool WriteFormattedLogLine(HANDLE file, size_t index, ...) {
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 formatted 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, &local_time);
53 time_buf[24] = '\0';
54 return time_buf;
55 }
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
64 // for anything else.
65 DWORD kShareAll = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
66 file_ = ::CreateFile(logfile.value().c_str(),
67 FILE_APPEND_DATA, kShareAll,
68 nullptr,
69 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
70 nullptr);
71 if (file_ == INVALID_HANDLE_VALUE)
72 return;
73 WriteFormattedLogLine(
74 file_, 0, ::GetCurrentProcessId(), PRODUCT_VERSION, DateTime().c_str());
75 }
76
77 Logger::~Logger() {
78 if (file_ != INVALID_HANDLE_VALUE) {
79 WriteFormattedLogLine(
80 file_, 1, ::GetCurrentProcessId(), DateTime().c_str());
81 ::CloseHandle(file_);
82 }
83 }
84
85 } // namespace caps
86
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698