• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2016 Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include <base/command_line.h>
19 #include <base/logging.h>
20 #include <base/strings/stringprintf.h>
21 #include <osi/include/log.h>
22 
23 #include "main_int.h"
24 
25 #ifdef TARGET_FLOSS
26 #include <syslog.h>
MessageHandler(int severity,const char * file,int line,size_t message_start,const std::string & message)27 static bool MessageHandler(int severity, const char* file, int line,
28                            size_t message_start, const std::string& message) {
29   ASSERT(message_start <= message.size());
30 
31   const auto str = base::StringPrintf("%s:%d - %s", file, line,
32                                       message.substr(message_start).c_str());
33 
34   switch (severity) {
35     case logging::LOGGING_INFO:
36       severity = LOG_INFO;
37       break;
38 
39     case logging::LOGGING_WARNING:
40       severity = LOG_WARNING;
41       break;
42 
43     case logging::LOGGING_ERROR:
44       severity = LOG_ERR;
45       break;
46 
47     case logging::LOGGING_FATAL:
48       severity = LOG_CRIT;
49       break;
50 
51     default:
52       severity = LOG_DEBUG;
53       break;
54   }
55 
56   syslog(severity, str.c_str());
57 
58   if (severity == LOG_CRIT) abort();
59 
60   return true;
61 }
62 #endif
63 
init_cpp_logging(config_t * config)64 void init_cpp_logging(config_t* config) {
65   // Command line and log level might be also configured in service/main.cpp
66   // when running the bluetoothtbd daemon. If it's already configured, skip
67   // configuring.
68   if (base::CommandLine::InitializedForCurrentProcess()) return;
69 
70   const std::string* loggingV =
71       config_get_string(*config, CONFIG_DEFAULT_SECTION, "LoggingV", NULL);
72   const std::string* loggingVModule = config_get_string(
73       *config, CONFIG_DEFAULT_SECTION, "LoggingVModule", NULL);
74 
75   int argc = 1;
76   const char* argv[] = {"bt_stack", NULL, NULL};
77 
78   if (loggingV != NULL) {
79     argv[argc] = loggingV->c_str();
80     argc++;
81   }
82 
83   if (loggingVModule != NULL) {
84     argv[argc] = loggingVModule->c_str();
85     argc++;
86   }
87 
88   // Init command line object with logging switches
89   base::CommandLine::Init(argc, argv);
90 
91   logging::LoggingSettings log_settings;
92 
93   if (!logging::InitLogging(log_settings)) {
94     LOG_ERROR("Failed to set up logging");
95   }
96 
97   // Android already logs thread_id, proc_id, timestamp, so disable those.
98   logging::SetLogItems(false, false, false, false);
99 
100 #ifdef TARGET_FLOSS
101   log_settings.logging_dest =
102       logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
103 
104   logging::SetLogMessageHandler(MessageHandler);
105 #endif
106 }
107