summaryrefslogtreecommitdiff
path: root/system/stack/mmc/main.cc
blob: 3f24e7851c8c372e149e723a0ccfecf13f05466b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <base/at_exit.h>
#include <base/files/file_descriptor_watcher_posix.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/run_loop.h>
#include <base/task/single_thread_task_executor.h>
#include <bluetooth/log.h>
#include <sys/syslog.h>

#include "mmc/daemon/service.h"

// syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING.
// We need to #undef at least these two before including base/logging.h.  The
// others are included to be consistent.
namespace {
const int kSyslogDebug = LOG_DEBUG;
const int kSyslogInfo = LOG_INFO;
const int kSyslogWarning = LOG_WARNING;
const int kSyslogError = LOG_ERR;
const int kSyslogCritical = LOG_CRIT;

#undef LOG_INFO
#undef LOG_WARNING
#undef LOG_ERR
#undef LOG_CRIT
}  // namespace

static bool MessageHandler(int severity, const char* file, int line, size_t message_start,
                           const std::string& message) {
  const auto str = std::format("{}:{} - {}", file, line, message.substr(message_start));

  switch (severity) {
    case logging::LOGGING_INFO:
      severity = kSyslogInfo;
      break;

    case logging::LOGGING_WARNING:
      severity = kSyslogWarning;
      break;

    case logging::LOGGING_ERROR:
      severity = kSyslogError;
      break;

    case logging::LOGGING_FATAL:
      severity = kSyslogCritical;
      break;

    default:
      severity = kSyslogDebug;
      break;
  }

  syslog(severity, "%s", str.c_str());

  if (severity == kSyslogCritical) {
    abort();
  }

  return true;
}

int main(int argc, char* argv[]) {
  // Set up syslog to stderr.
  logging::LoggingSettings settings;
  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
  logging::SetLogItems(false, false, false, false);
  logging::InitLogging(settings);
  logging::SetLogMessageHandler(MessageHandler);

  bluetooth::log::info("Start MMC daemon");

  // These are needed to send D-Bus signals and receive messages.
  // Even though they are not used directly, they set up some global state
  // needed by the D-Bus library.
  base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
  base::FileDescriptorWatcher watcher(task_executor.task_runner());
  base::AtExitManager at_exit_manager;

  base::RunLoop run_loop;

  auto service = std::make_unique<mmc::Service>(run_loop.QuitClosure());
  bluetooth::log::assert_that(service->Init(), "assert failed: service->Init()");

  run_loop.Run();

  return 0;
}