OLD | NEW |
| (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 <stddef.h> | |
7 #include <time.h> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/tools/crash_service/caps/logger_win.h" | |
12 #include "components/version_info/version_info_values.h" | |
13 | |
14 namespace { | |
15 // Every message has this structure: | |
16 // <index>~ <tick_count> <message> ~\n" | |
17 const char kMessageHeader[] = "%03d~ %08x %s ~\n"; | |
18 | |
19 // Do not re-order these messages. Only add at the end. | |
20 const char* kMessages[] { | |
21 "start pid(%lu) version(%s) time(%s)", // 0 | |
22 "exit pid(%lu) time(%s)", // 1 | |
23 "instance found", // 2 | |
24 }; | |
25 | |
26 bool WriteLogLine(HANDLE file, const std::string& txt) { | |
27 if (txt.empty()) | |
28 return true; | |
29 DWORD written; | |
30 auto rc = ::WriteFile(file, txt.c_str(), | |
31 static_cast<DWORD>(txt.size()), | |
32 &written, | |
33 nullptr); | |
34 return (rc == TRUE); | |
35 } | |
36 | |
37 // Uses |kMessages| at |index| above to write to disk a formatted log line. | |
38 bool WriteFormattedLogLine(HANDLE file, size_t index, ...) { | |
39 va_list ap; | |
40 va_start(ap, index); | |
41 auto fmt = base::StringPrintf( | |
42 kMessageHeader, index, ::GetTickCount(), kMessages[index]); | |
43 auto msg = base::StringPrintV(fmt.c_str(), ap); | |
44 auto rc = WriteLogLine(file, msg.c_str()); | |
45 va_end(ap); | |
46 return rc; | |
47 } | |
48 | |
49 // Returns the current time and date formatted in standard C style. | |
50 std::string DateTime() { | |
51 time_t current_time = 0; | |
52 time(¤t_time); | |
53 struct tm local_time = {0}; | |
54 char time_buf[26] = {0}; | |
55 localtime_s(&local_time, ¤t_time); | |
56 asctime_s(time_buf, &local_time); | |
57 time_buf[24] = '\0'; | |
58 return time_buf; | |
59 } | |
60 | |
61 } // namespace | |
62 | |
63 namespace caps { | |
64 | |
65 Logger::Logger(const base::FilePath& path) : file_(INVALID_HANDLE_VALUE) { | |
66 auto logfile = path.Append(L"caps_log.txt"); | |
67 // Opening a file like so allows atomic appends, but can't be used | |
68 // for anything else. | |
69 DWORD kShareAll = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | |
70 file_ = ::CreateFile(logfile.value().c_str(), | |
71 FILE_APPEND_DATA, kShareAll, | |
72 nullptr, | |
73 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, | |
74 nullptr); | |
75 if (file_ == INVALID_HANDLE_VALUE) | |
76 return; | |
77 WriteFormattedLogLine( | |
78 file_, 0, ::GetCurrentProcessId(), PRODUCT_VERSION, DateTime().c_str()); | |
79 } | |
80 | |
81 Logger::~Logger() { | |
82 if (file_ != INVALID_HANDLE_VALUE) { | |
83 WriteFormattedLogLine( | |
84 file_, 1, ::GetCurrentProcessId(), DateTime().c_str()); | |
85 ::CloseHandle(file_); | |
86 } | |
87 } | |
88 | |
89 } // namespace caps | |
90 | |
OLD | NEW |