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

Side by Side Diff: services/flog/flog_directory.cc

Issue 2046703002: Add 'flog' service implementation. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Fixes per feedback. Created 4 years, 6 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
« no previous file with comments | « services/flog/flog_directory.h ('k') | services/flog/flog_logger_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 <ctime>
6 #include <iomanip>
7 #include <sstream>
8
9 #include "base/logging.h"
10 #include "mojo/public/cpp/application/connect.h"
11 #include "services/flog/flog_directory.h"
12
13 namespace mojo {
14 namespace flog {
15
16 FlogDirectory::FlogDirectory(Shell* shell) {
17 ConnectToService(shell, "mojo:files", GetProxy(&files_));
18 files_->OpenFileSystem("app_persistent_cache", GetProxy(&file_system_),
19 [this](files::Error error) {
20 files_.reset();
21 if (error != files::Error::OK) {
22 DCHECK(false) << "Failed to open file system: "
23 << error;
24 delete this;
25 }
26 });
27 }
28
29 FlogDirectory::~FlogDirectory() {}
30
31 void FlogDirectory::GetExistingFiles(GetExistingFilesCallback callback) {
32 file_system_->Read([this, callback](files::Error error,
33 Array<files::DirectoryEntryPtr> entries) {
34 std::unique_ptr<std::map<uint32_t, std::string>> labels_by_id =
35 std::unique_ptr<std::map<uint32_t, std::string>>(
36 new std::map<uint32_t, std::string>);
37 for (const files::DirectoryEntryPtr& entry : entries) {
38 if (entry->type == files::FileType::REGULAR_FILE) {
39 uint32_t id;
40 std::string label;
41 if (ParseLogFileName(entry->name, &id, &label)) {
42 labels_by_id->insert(std::pair<uint32_t, std::string>(id, label));
43 }
44 }
45 }
46
47 callback(std::move(labels_by_id));
48 });
49 }
50
51 files::FilePtr FlogDirectory::GetFile(uint32_t id,
52 const std::string& label,
53 bool create) {
54 files::FilePtr file;
55 file_system_->OpenFile(
56 LogFileName(id, label), GetProxy(&file),
57 files::kOpenFlagRead |
58 (create ? (files::kOpenFlagWrite | files::kOpenFlagCreate) : 0),
59 [this](files::Error error) {
60 if (error != files::Error::OK) {
61 DCHECK(false) << "Failed to OpenFile" << error;
62 // TODO: Fail.
63 return;
64 }
65 });
66 file_system_.WaitForIncomingResponse();
67 return file.Pass();
68 }
69
70 std::string FlogDirectory::LogFileName(uint32_t id, const std::string& label) {
71 // Format is "<id>_<label>.flog" where <id> is the kLogIdWidth-digit,
72 // zero-padded info.id_ and <label> is the label.
73 std::ostringstream file_name_stream;
74 file_name_stream << std::setfill('0') << std::setw(kLogIdWidth) << id << "_"
75 << label << ".flog";
76 return file_name_stream.str();
77 }
78
79 bool FlogDirectory::ParseLogFileName(const std::string& name,
80 uint32_t* id_out,
81 std::string* label_out) {
82 DCHECK(id_out != nullptr);
83 DCHECK(label_out != nullptr);
84
85 if (name.size() < kLogIdWidth + 2) {
86 return false;
87 }
88
89 for (size_t i = 0; i < kLogIdWidth; i++) {
90 if (!isdigit(name[i])) {
91 return false;
92 }
93 }
94
95 if (name[kLogIdWidth] != '_') {
96 return false;
97 }
98
99 size_t after_label = name.find_first_of('.', kLogIdWidth + 1);
100 if (after_label == std::string::npos) {
101 return false;
102 }
103
104 *id_out = std::stoul(name);
105 *label_out = name.substr(kLogIdWidth + 1, after_label - (kLogIdWidth + 1));
106
107 return true;
108 }
109
110 } // namespace flog
111 } // namespace mojo
OLDNEW
« no previous file with comments | « services/flog/flog_directory.h ('k') | services/flog/flog_logger_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698