xref: /aosp_15_r20/external/federated-compute/fcp/client/engine/caching_error_reporter.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "fcp/client/engine/caching_error_reporter.h"
17 
18 #include <stdio.h>
19 
20 #include <string>
21 
22 namespace fcp {
23 namespace client {
24 namespace engine {
25 
Report(const char * format,va_list args)26 int CachingErrorReporter::Report(const char* format, va_list args) {
27   absl::MutexLock lock(&mutex_);
28   char error_msg[kBufferSize];
29   int num_characters = vsnprintf(error_msg, kBufferSize, format, args);
30   if (num_characters >= 0) {
31     error_messages_.push_back(std::string(error_msg));
32   } else {
33     // If num_characters is below zero, we can't trust the created string, so we
34     // push an "Unknown error" to the stored error messages.
35     // We don't want to crash here, because the TFLite execution will be
36     // terminated soon.
37     error_messages_.push_back("Unknown error.");
38   }
39   return num_characters;
40 }
41 
GetFirstErrorMessage()42 std::string CachingErrorReporter::GetFirstErrorMessage() {
43   absl::MutexLock lock(&mutex_);
44   if (error_messages_.empty()) {
45     return "";
46   } else {
47     return error_messages_[0];
48   }
49 }
50 
51 }  // namespace engine
52 }  // namespace client
53 }  // namespace fcp
54