Prashanth Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 1 | // 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 Hepler | 76c3a5c | 2021-05-26 09:57:11 -0700 | [diff] [blame] | 18 | #include "pw_log/proto/log.pwpb.h" |
Prashanth Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 19 | #include "pw_status/try.h" |
| 20 | |
| 21 | namespace pw::log_rpc { |
| 22 | namespace { |
| 23 | |
Wyatt Hepler | 0d4c916 | 2021-05-26 09:27:22 -0700 | [diff] [blame] | 24 | // TODO(prashanthsw): Handle dropped messages. |
| 25 | // Result<ConstByteSpan> GenerateDroppedEntryMessage(ByteSpan encode_buffer, |
| 26 | // size_t dropped_entries) { |
Armando Montanez | 9f3708c | 2021-08-02 14:37:36 -0700 | [diff] [blame] | 27 | // pw::log::LogEntry::MemoryEncoder encoder(encode_buffer); |
Wyatt Hepler | 0d4c916 | 2021-05-26 09:27:22 -0700 | [diff] [blame] | 28 | // encoder.WriteDropped(dropped_entries); |
Armando Montanez | 32988e0 | 2021-05-21 11:56:09 -0700 | [diff] [blame] | 29 | // if (encoder.status().ok()) { |
| 30 | // return ConstByteSpan(encoder); |
| 31 | // } |
| 32 | // return encoder.status(); |
Wyatt Hepler | 0d4c916 | 2021-05-26 09:27:22 -0700 | [diff] [blame] | 33 | // } |
Prashanth Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | void Logs::Get(ServerContext&, ConstByteSpan, rpc::RawServerWriter& writer) { |
| 38 | response_writer_ = std::move(writer); |
| 39 | } |
| 40 | |
| 41 | Status 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 Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 45 | return OkStatus(); |
Prashanth Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 46 | } |
| 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 Hepler | 0d4c916 | 2021-05-26 09:27:22 -0700 | [diff] [blame] | 50 | // 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 Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 58 | |
| 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 Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 66 | return OkStatus(); |
Prashanth Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 67 | } |
| 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 Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 76 | return OkStatus(); |
Prashanth Swaminathan | 652977d | 2020-10-16 13:18:05 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace pw::log_rpc |