blob: 5630429d3f4a442ccbdbd7235d7f20be759f6a09 [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) {
Armando Montanez9f3708c2021-08-02 14:37:36 -070027// pw::log::LogEntry::MemoryEncoder encoder(encode_buffer);
Wyatt Hepler0d4c9162021-05-26 09:27:22 -070028// encoder.WriteDropped(dropped_entries);
Armando Montanez32988e02021-05-21 11:56:09 -070029// if (encoder.status().ok()) {
30// return ConstByteSpan(encoder);
31// }
32// return encoder.status();
Wyatt Hepler0d4c9162021-05-26 09:27:22 -070033// }
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070034
35} // namespace
36
37void Logs::Get(ServerContext&, ConstByteSpan, rpc::RawServerWriter& writer) {
38 response_writer_ = std::move(writer);
39}
40
41Status Logs::Flush() {
42 // If the response writer was not initialized or has since been closed,
43 // ignore the flush operation.
44 if (!response_writer_.open()) {
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080045 return OkStatus();
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070046 }
47
48 // If previous calls to flush resulted in dropped entries, generate a
49 // dropped entry message and write it before further log messages.
Wyatt Hepler0d4c9162021-05-26 09:27:22 -070050 // TODO(prashanthsw): Handle dropped messages.
51 // if (dropped_entries_ > 0) {
52 // ByteSpan payload = response_writer_.PayloadBuffer();
53 // Result dropped_log = GenerateDroppedEntryMessage(payload,
54 // dropped_entries_); PW_TRY(dropped_log.status());
55 // PW_TRY(response_writer_.Write(dropped_log.value()));
56 // dropped_entries_ = 0;
57 // }
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070058
59 // Write logs to the response writer. An important limitation of this
60 // implementation is that if this RPC call fails, the logs are lost -
61 // a subsequent call to the RPC will produce a drop count message.
62 ByteSpan payload = response_writer_.PayloadBuffer();
63 Result possible_logs = log_queue_.PopMultiple(payload);
64 PW_TRY(possible_logs.status());
65 if (possible_logs.value().entry_count == 0) {
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080066 return OkStatus();
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070067 }
68
69 Status status = response_writer_.Write(possible_logs.value().entries);
70 if (!status.ok()) {
71 // On a failure to send logs, track the dropped entries.
72 dropped_entries_ = possible_logs.value().entry_count;
73 return status;
74 }
75
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080076 return OkStatus();
Prashanth Swaminathan652977d2020-10-16 13:18:05 -070077}
78
79} // namespace pw::log_rpc