Apollo  6.0
Open source self driving car software
logger_util.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 
21 #ifndef CYBER_LOGGER_LOGGER_UTIL_H_
22 #define CYBER_LOGGER_LOGGER_UTIL_H_
23 
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/utsname.h>
27 #include <unistd.h>
28 
29 #include <cstdint>
30 #include <cstdlib>
31 #include <ctime>
32 #include <string>
33 #include <vector>
34 
36 
37 namespace apollo {
38 namespace cyber {
39 namespace logger {
40 
41 inline int64_t CycleClock_Now() {
42  struct timeval tv;
43  gettimeofday(&tv, nullptr);
44  return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
45 }
46 
47 inline int64_t UsecToCycles(int64_t usec) { return usec; }
48 
49 static inline void GetHostName(std::string* hostname) {
50  struct utsname buf;
51  if (0 != uname(&buf)) {
52  // ensure null termination on failure
53  *buf.nodename = '\0';
54  }
55  *hostname = buf.nodename;
56 }
57 
58 int32_t GetMainThreadPid();
59 
60 bool PidHasChanged();
61 
62 inline int32_t MaxLogSize() {
63  return (FLAGS_max_log_size > 0 ? FLAGS_max_log_size : 1);
64 }
65 
66 inline void FindModuleName(std::string* log_message, std::string* module_name) {
67  auto lpos = log_message->find(LEFT_BRACKET);
68  if (lpos != std::string::npos) {
69  auto rpos = log_message->find(RIGHT_BRACKET, lpos);
70  if (rpos != std::string::npos) {
71  module_name->assign(*log_message, lpos + 1, rpos - lpos - 1);
72  auto cut_length = rpos - lpos + 1;
73  log_message->erase(lpos, cut_length);
74  }
75  }
76  if (module_name->empty()) {
77  CHECK_NOTNULL(common::GlobalData::Instance());
78  *module_name = common::GlobalData::Instance()->ProcessGroup();
79  }
80 }
81 
82 } // namespace logger
83 } // namespace cyber
84 } // namespace apollo
85 
86 #endif // CYBER_LOGGER_LOGGER_UTIL_H_
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int32_t MaxLogSize()
Definition: logger_util.h:62
void FindModuleName(std::string *log_message, std::string *module_name)
Definition: logger_util.h:66
int64_t CycleClock_Now()
Definition: logger_util.h:41
#define RIGHT_BRACKET
Definition: log.h:33
#define LEFT_BRACKET
Definition: log.h:32
int32_t GetMainThreadPid()
int64_t UsecToCycles(int64_t usec)
Definition: logger_util.h:47