• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 #pragma once
18 
19 #include <assert.h>
20 #include <log/log.h>
21 #include <stdio.h>
22 #include <sys/syscall.h> /* Definition of SYS_* constants */
23 #include <unistd.h>
24 
25 #include <chrono>
26 #include <ctime>
27 #include <string>
28 
29 #include "build_timestamp.h"  // generated
30 
31 void enable_logging();
32 void log_logging();
33 long long GetTimestampMs();
34 
35 // Internal to headless below
36 
37 extern int console_fd;
38 extern std::chrono::system_clock::time_point _prev;
39 
40 // Highlight the MAIN thread with text replacement
41 constexpr char _main[7 + 1] = "  MAIN ";
42 
43 #define STR(obj) (obj).ToString().c_str()
44 
45 #define ASSERT_LOG(condition, fmt, args...)                                 \
46   do {                                                                      \
47     if (!(condition)) {                                                     \
48       LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, ##args); \
49     }                                                                       \
50   } while (false)
51 
52 #define LOG_CONSOLE(fmt, args...)                                              \
53   do {                                                                         \
54     ASSERT_LOG(console_fd != -1, "Console output fd has not been set");        \
55     /* Also log to Android logging via INFO level */                           \
56     ALOGI("%s:%d %s: " fmt, __FILE__, __LINE__, __func__, ##args);             \
57     auto _now = std::chrono::system_clock::now();                              \
58     auto _delta =                                                              \
59         std::chrono::duration_cast<std::chrono::microseconds>(_now - _prev);   \
60     _prev = _now;                                                              \
61     auto _now_us =                                                             \
62         std::chrono::time_point_cast<std::chrono::microseconds>(_now);         \
63     auto _now_t = std::chrono::system_clock::to_time_t(_now);                  \
64     /* YYYY-MM-DD_HH:MM:SS.ssssss is 26 byte long, plus 1 for null terminator  \
65      */                                                                        \
66     char _buf[26 + 1];                                                         \
67     auto l = std::strftime(_buf, sizeof(_buf), "%Y-%m-%d %H:%M:%S",            \
68                            std::localtime(&_now_t));                           \
69     snprintf(_buf + l, sizeof(_buf) - l, ".%06u",                              \
70              static_cast<unsigned int>(_now_us.time_since_epoch().count() %    \
71                                        1000000));                              \
72     /* pid max is 2^22 = 4194304 in 64-bit system, and 32768 by default, hence \
73      * 7 digits are needed most */                                             \
74     char _buf_thread[7 + 1];                                                   \
75     snprintf(_buf_thread, sizeof(_buf_thread), "%7ld", syscall(SYS_gettid));   \
76     dprintf(console_fd, "%s - [ %9.06f ] %7d %s %s : " fmt "\n", _buf,         \
77             _delta.count() / 1000000.0, static_cast<int>(getpid()),            \
78             (syscall(SYS_gettid) == static_cast<int>(getpid())) ? _main        \
79                                                                 : _buf_thread, \
80             LOG_TAG, ##args);                                                  \
81   } while (false)
82 
83 constexpr char kCompiledDateFormat[] = "%b %d %Y";
84 constexpr char kBuildDateFormat[] = "%Y-%m-%d";
85 
build_id()86 inline std::string build_id() {
87   return std::string(bluetooth::test::headless::kBuildTime);
88 }
89