xref: /aosp_15_r20/external/federated-compute/fcp/client/engine/caching_error_reporter.h (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 #ifndef FCP_CLIENT_ENGINE_CACHING_ERROR_REPORTER_H_
17 #define FCP_CLIENT_ENGINE_CACHING_ERROR_REPORTER_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "absl/synchronization/mutex.h"
23 #include "tensorflow/lite/core/api/error_reporter.h"
24 
25 namespace fcp {
26 namespace client {
27 namespace engine {
28 
29 // This implementation of ErrorReporter stores all the error messages.
30 class CachingErrorReporter : public tflite::ErrorReporter {
31  public:
32   int Report(const char* format, va_list args) override
33       ABSL_LOCKS_EXCLUDED(mutex_);
34   std::string GetFirstErrorMessage() ABSL_LOCKS_EXCLUDED(mutex_);
35 
36  private:
37   absl::Mutex mutex_;
38   static constexpr int kBufferSize = 1024;
39   // There could be more than one error messages so we store all of them.
40   std::vector<std::string> error_messages_ ABSL_GUARDED_BY(mutex_);
41 };
42 
43 }  // namespace engine
44 }  // namespace client
45 }  // namespace fcp
46 
47 #endif  // FCP_CLIENT_ENGINE_CACHING_ERROR_REPORTER_H_
48