1 /*
2  * Copyright (C) 2023 The Android Open Source Project
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 
17 #pragma once
18 
19 #include <jni.h>
20 
21 #include <optional>
22 #include <string>
23 
24 #include "example_iterator_wrapper_impl.h"
25 #include "fcp/client/flags.h"
26 #include "fcp/client/log_manager.h"
27 
28 namespace fcp {
29 namespace client {
30 namespace engine {
31 namespace jni {
32 
33 // A wrapper around the LogManager Java class. Can be created on an
34 // arbitrary thread.
35 class LogManagerWrapperImpl : public LogManager {
36  public:
37   LogManagerWrapperImpl(const LogManagerWrapperImpl &) = delete;
38   void *operator new(std::size_t) = delete;
39   LogManagerWrapperImpl &operator=(const LogManagerWrapperImpl &) = delete;
40 
41   LogManagerWrapperImpl(JavaVM *jvm, jobject java_log_manager);
42 
43   ~LogManagerWrapperImpl() override;
44 
45   void LogDiag(ProdDiagCode diag_code) override;
46 
47   void LogDiag(DebugDiagCode diag_code) override;
48 
49   void LogToLongHistogram(HistogramCounters histogram_counter,
50                           int execution_index, int epoch_index,
51                           DataSourceType data_source_type,
52                           int64_t value) override;
53 
54   void SetModelIdentifier(const std::string &model_identifier) override;
55 
56  private:
57   JavaVM *const jvm_;
58   jobject jthis_;
59   jmethodID log_prod_diag_id_;
60   jmethodID log_debug_diag_id_;
61   jmethodID log_to_long_histogram_id_;
62   jmethodID log_to_long_histogram_with_model_identifier_id_;
63   std::optional<std::string> model_identifier_;
64 };
65 
66 }  // namespace jni
67 }  // namespace engine
68 }  // namespace client
69 }  // namespace fcp
70