1 /*
2  * Copyright 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 #include <base/at_exit.h>
18 #include <base/files/file_descriptor_watcher_posix.h>
19 #include <base/files/file_util.h>
20 #include <base/logging.h>
21 #include <base/run_loop.h>
22 #include <base/strings/stringprintf.h>
23 #include <base/task/single_thread_task_executor.h>
24 #include <bluetooth/log.h>
25 #include <sys/syslog.h>
26 
27 #include "mmc/daemon/service.h"
28 
29 // syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING.
30 // We need to #undef at least these two before including base/logging.h.  The
31 // others are included to be consistent.
32 namespace {
33 const int kSyslogDebug = LOG_DEBUG;
34 const int kSyslogInfo = LOG_INFO;
35 const int kSyslogWarning = LOG_WARNING;
36 const int kSyslogError = LOG_ERR;
37 const int kSyslogCritical = LOG_CRIT;
38 
39 #undef LOG_INFO
40 #undef LOG_WARNING
41 #undef LOG_ERR
42 #undef LOG_CRIT
43 }  // namespace
44 
MessageHandler(int severity,const char * file,int line,size_t message_start,const std::string & message)45 static bool MessageHandler(int severity, const char* file, int line, size_t message_start,
46                            const std::string& message) {
47   const auto str =
48           base::StringPrintf("%s:%d - %s", file, line, message.substr(message_start).c_str());
49 
50   switch (severity) {
51     case logging::LOGGING_INFO:
52       severity = kSyslogInfo;
53       break;
54 
55     case logging::LOGGING_WARNING:
56       severity = kSyslogWarning;
57       break;
58 
59     case logging::LOGGING_ERROR:
60       severity = kSyslogError;
61       break;
62 
63     case logging::LOGGING_FATAL:
64       severity = kSyslogCritical;
65       break;
66 
67     default:
68       severity = kSyslogDebug;
69       break;
70   }
71 
72   syslog(severity, "%s", str.c_str());
73 
74   if (severity == kSyslogCritical) {
75     abort();
76   }
77 
78   return true;
79 }
80 
main(int argc,char * argv[])81 int main(int argc, char* argv[]) {
82   // Set up syslog to stderr.
83   logging::LoggingSettings settings;
84   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
85   logging::SetLogItems(false, false, false, false);
86   logging::InitLogging(settings);
87   logging::SetLogMessageHandler(MessageHandler);
88 
89   bluetooth::log::info("Start MMC daemon");
90 
91   // These are needed to send D-Bus signals and receive messages.
92   // Even though they are not used directly, they set up some global state
93   // needed by the D-Bus library.
94   base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
95   base::FileDescriptorWatcher watcher(task_executor.task_runner());
96   base::AtExitManager at_exit_manager;
97 
98   base::RunLoop run_loop;
99 
100   auto service = std::make_unique<mmc::Service>(run_loop.QuitClosure());
101   bluetooth::log::assert_that(service->Init(), "assert failed: service->Init()");
102 
103   run_loop.Run();
104 
105   return 0;
106 }
107