• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use connection file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdarg.h>
16 #include <fstream>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 
21 #include "aemu/base/files/PathUtils.h"
22 #include "aemu/base/logging/CLog.h"
23 #include "aemu/base/logging/LogFormatter.h"
24 #include "aemu/base/logging/LogSeverity.h"
25 #include "android/base/system/System.h"
26 #include "android/utils/path.h"
27 
28 static constexpr std::string_view BLUETOOTH_LOG{"bluetooth"};
29 using android::base::System;
30 using android::base::VerboseLogFormatter;
31 
32 namespace android::bluetooth {
getLogstream(std::string_view id)33 std::shared_ptr<std::ostream> getLogstream(std::string_view id) {
34     auto basedir = android::base::pj(
35             {System::get()->getTempDir(), BLUETOOTH_LOG.data(),
36              std::to_string(System::get()->getCurrentProcessId())});
37 
38     if (path_mkdir_if_needed(basedir.c_str(), 0700) != 0) {
39         dfatal("Unable to create bluetooth logging directory: %s",
40                basedir.c_str());
41     }
42     std::string filename = android::base::pj(basedir, id.data());
43     for (int i = 0; System::get()->pathExists(filename); i++) {
44         filename = android::base::pj(basedir,
45                                      std::to_string(i) + "_" + std::string(id));
46     }
47     dinfo("Creating bluetooth log: %s", filename.c_str());
48 
49     return std::make_shared<std::ofstream>(filename, std::ios::binary);
50 }
51 }  // namespace android::bluetooth
52 
__blue_write_to_file(LogSeverity prio,const char * file,int line,const char * fmt,...)53 extern "C" void __blue_write_to_file(LogSeverity prio,
54                                      const char* file,
55                                      int line,
56                                      const char* fmt,
57                                      ...) {
58     static VerboseLogFormatter formatter;
59     static std::shared_ptr<std::ostream> logStream =
60             android::bluetooth::getLogstream("rootcanal.log");
61     va_list args;
62     va_start(args, fmt);
63     std::string msg = formatter.format({file, line, prio}, fmt, args);
64     va_end(args);
65 
66     if (!msg.empty()) {
67         if (msg.back() == '\n') {
68             *logStream << msg;
69         } else {
70             *logStream << msg << std::endl;
71         }
72     }
73 }
74