blob: d548c84f91a38e90ccec52d9baf4ea065e599d60 [file] [log] [blame]
Prashanth Swaminathan652977d2020-10-16 13:18:05 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "pw_log_rpc/logs_rpc.h"
16
17#include "pw_log/log.h"
Wyatt Hepler76c3a5c2021-05-26 09:57:11 -070018#include "pw_log/proto/log.pwpb.h"
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070019#include "pw_status/try.h"
20
21namespace pw::log_rpc {
22namespace {
23
Wyatt Hepler0d4c9162021-05-26 09:27:22 -070024// TODO(prashanthsw): Handle dropped messages.
25// Result<ConstByteSpan> GenerateDroppedEntryMessage(ByteSpan encode_buffer,
26// size_t dropped_entries) {
27// pw::protobuf::NestedEncoder nested_encoder(encode_buffer);
28// pw::log::LogEntry::Encoder encoder(&nested_encoder);
29// encoder.WriteDropped(dropped_entries);
30// return nested_encoder.Encode();
31// }
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070032
33} // namespace
34
35void Logs::Get(ServerContext&, ConstByteSpan, rpc::RawServerWriter& writer) {
36 response_writer_ = std::move(writer);
37}
38
39Status Logs::Flush() {
40 // If the response writer was not initialized or has since been closed,
41 // ignore the flush operation.
42 if (!response_writer_.open()) {
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080043 return OkStatus();
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070044 }
45
46 // If previous calls to flush resulted in dropped entries, generate a
47 // dropped entry message and write it before further log messages.
Wyatt Hepler0d4c9162021-05-26 09:27:22 -070048 // TODO(prashanthsw): Handle dropped messages.
49 // if (dropped_entries_ > 0) {
50 // ByteSpan payload = response_writer_.PayloadBuffer();
51 // Result dropped_log = GenerateDroppedEntryMessage(payload,
52 // dropped_entries_); PW_TRY(dropped_log.status());
53 // PW_TRY(response_writer_.Write(dropped_log.value()));
54 // dropped_entries_ = 0;
55 // }
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070056
57 // Write logs to the response writer. An important limitation of this
58 // implementation is that if this RPC call fails, the logs are lost -
59 // a subsequent call to the RPC will produce a drop count message.
60 ByteSpan payload = response_writer_.PayloadBuffer();
61 Result possible_logs = log_queue_.PopMultiple(payload);
62 PW_TRY(possible_logs.status());
63 if (possible_logs.value().entry_count == 0) {
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080064 return OkStatus();
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070065 }
66
67 Status status = response_writer_.Write(possible_logs.value().entries);
68 if (!status.ok()) {
69 // On a failure to send logs, track the dropped entries.
70 dropped_entries_ = possible_logs.value().entry_count;
71 return status;
72 }
73
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080074 return OkStatus();
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070075}
76
77} // namespace pw::log_rpc