Log supporting for binder ndk

Move log code Generating code aidl_to_cpp -> aidl_to_common
Add dependency of libjsoncpp(_ndk)
 -ndk: libjsoncpp_ndk
 -ndk_platform: libjsoncpp

Bug: 126501406
Test: m
Test: ./runtests.sh

Change-Id: Ife69f146ce313564082d070e5e047ec8e1111093
Merged-In: Ife69f146ce313564082d070e5e047ec8e1111093
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index cafd794..f7a2b45 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -233,9 +233,11 @@
   return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
 }
 
-static void GenerateClientMethodDefinition(
-    CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
-    const AidlMethod& method, const std::optional<std::string> return_value_cached_to) {
+static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types,
+                                           const AidlInterface& defined_type,
+                                           const AidlMethod& method,
+                                           const std::optional<std::string> return_value_cached_to,
+                                           const Options& options) {
   const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
 
   out << NdkMethodDecl(types, method, clazz) << " {\n";
@@ -256,6 +258,11 @@
   out << "::ndk::ScopedAParcel _aidl_out;\n";
   out << "\n";
 
+  if (options.GenLog()) {
+    out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::CLIENT), method,
+                                    false /* isServer */, true /* isNdk */);
+  }
+
   out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
   StatusCheckGoto(out);
 
@@ -320,14 +327,19 @@
 
   out << "_aidl_error:\n";
   out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
+  if (options.GenLog()) {
+    out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::CLIENT), defined_type,
+                                   method, "_aidl_status", "_aidl_return", false /* isServer */,
+                                   true /* isNdk */);
+  }
   out << "return _aidl_status;\n";
   out.Dedent();
   out << "}\n";
 }
 
 static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
-                                         const AidlInterface& /*defined_type*/,
-                                         const AidlMethod& method) {
+                                         const AidlInterface& defined_type,
+                                         const AidlMethod& method, const Options& options) {
   out << "case " << MethodId(method) << ": {\n";
   out.Indent();
   for (const auto& arg : method.GetArguments()) {
@@ -351,7 +363,10 @@
       out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n";
     }
   }
-
+  if (options.GenLog()) {
+    out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::SERVER), method,
+                                    true /* isServer */, true /* isNdk */);
+  }
   out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
       << NdkArgList(types, method, FormatArgForCall) << ");\n";
 
@@ -378,14 +393,18 @@
       StatusCheckBreak(out);
     }
   }
-
+  if (options.GenLog()) {
+    out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::SERVER), defined_type,
+                                   method, "_aidl_status", "_aidl_return", true /* isServer */,
+                                   true /* isNdk */);
+  }
   out << "break;\n";
   out.Dedent();
   out << "}\n";
 }
 
 void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
-                         const AidlInterface& defined_type, const Options& /*options*/) {
+                         const AidlInterface& defined_type, const Options& options) {
   const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
   const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
 
@@ -405,7 +424,7 @@
     out << "switch (_aidl_code) {\n";
     out.Indent();
     for (const auto& method : defined_type.GetMethods()) {
-      GenerateServerCaseDefinition(out, types, defined_type, *method);
+      GenerateServerCaseDefinition(out, types, defined_type, *method, options);
     }
     out.Dedent();
     out << "}\n";
@@ -426,6 +445,9 @@
 
   out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
   out << clazz << "::~" << clazz << "() {}\n";
+  if (options.GenLog()) {
+    out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n";
+  }
   out << "\n";
   for (const auto& method : defined_type.GetMethods()) {
     // Only getInterfaceVersion can use cache.
@@ -433,7 +455,8 @@
                            options.Version() > 0;
     const auto return_value_cached_to =
         cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
-    GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to);
+    GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to,
+                                   options);
   }
 }
 void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
@@ -444,7 +467,9 @@
   out << "// Source for " << clazz << "\n";
   out << clazz << "::" << clazz << "() {}\n";
   out << clazz << "::~" << clazz << "() {}\n";
-
+  if (options.GenLog()) {
+    out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n";
+  }
   out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
   out.Indent();
   out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n";
@@ -585,6 +610,12 @@
       << "\"\n";
   out << "\n";
   out << "#include <android/binder_ibinder.h>\n";
+  if (options.GenLog()) {
+    out << "#include <json/value.h>\n";
+    out << "#include <functional>\n";
+    out << "#include <chrono>\n";
+    out << "#include <sstream>\n";
+  }
   out << "\n";
   EnterNdkNamespace(out, defined_type);
   out << "class " << clazz << " : public ::ndk::BpCInterface<"
@@ -601,7 +632,9 @@
   if (options.Version() > 0) {
     out << "int32_t " << kCacheVariable << " = -1;\n";
   }
-
+  if (options.GenLog()) {
+    out << "static std::function<void(const Json::Value&)> logFunc;\n";
+  }
   out.Dedent();
   out << "};\n";
   LeaveNdkNamespace(out, defined_type);
@@ -635,7 +668,9 @@
       AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented.";
     }
   }
-
+  if (options.GenLog()) {
+    out << "static std::function<void(const Json::Value&)> logFunc;\n";
+  }
   out.Dedent();
   out << "protected:\n";
   out.Indent();
@@ -653,6 +688,12 @@
 
   out << "#pragma once\n\n";
   out << "#include <android/binder_interface_utils.h>\n";
+  if (options.GenLog()) {
+    out << "#include <json/value.h>\n";
+    out << "#include <functional>\n";
+    out << "#include <chrono>\n";
+    out << "#include <sstream>\n";
+  }
   out << "\n";
 
   GenerateHeaderIncludes(out, types, defined_type);