Apollo 10.0
自动驾驶开放平台
logger.cc
浏览该文件的文档.
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
17#include "cyber/logger/logger.h"
18
19#include <cstdlib>
20#include <string>
21#include <unordered_map>
22#include <utility>
23
24#include "glog/logging.h"
25
28
29namespace apollo {
30namespace cyber {
31namespace logger {
32
33static std::unordered_map<std::string, LogFileObject*> moduleLoggerMap;
34
35Logger::Logger(google::base::Logger* wrapped) : wrapped_(wrapped) {}
36
38 for (auto itr = moduleLoggerMap.begin(); itr != moduleLoggerMap.end();
39 ++itr) {
40 delete itr->second;
41 }
42 moduleLoggerMap.clear();
43}
44
45void Logger::Write(bool force_flush, time_t timestamp, const char* message,
46 int message_len) {
47 std::string log_message = std::string(message, message_len);
48 std::string module_name;
49 // set the same bracket as the bracket in log.h
50 FindModuleName(&log_message, &module_name);
51
52 LogFileObject* fileobject = nullptr;
53 {
54 std::lock_guard<std::mutex> lock(mutex_);
55 if (moduleLoggerMap.find(module_name) != moduleLoggerMap.end()) {
56 fileobject = moduleLoggerMap[module_name];
57 } else {
58 std::string file_name = module_name + ".log.INFO.";
59 if (!FLAGS_log_dir.empty()) {
60 file_name = FLAGS_log_dir + "/" + file_name;
61 }
62 fileobject = new LogFileObject(google::INFO, file_name.c_str());
63 fileobject->SetSymlinkBasename(module_name.c_str());
64 moduleLoggerMap[module_name] = fileobject;
65 }
66 }
67 if (fileobject) {
68 fileobject->Write(force_flush, timestamp, log_message.c_str(),
69 static_cast<int>(log_message.size()));
70 }
71}
72
73void Logger::Flush() { wrapped_->Flush(); }
74
75uint32_t Logger::LogSize() { return wrapped_->LogSize(); }
76
77} // namespace logger
78} // namespace cyber
79} // namespace apollo
void Write(bool force_flush, time_t timestamp, const char *message, int message_len) override
void SetSymlinkBasename(const char *symlink_basename)
uint32_t LogSize() override
Definition logger.cc:75
void Write(bool force_flush, time_t timestamp, const char *message, int message_len) override
Definition logger.cc:45
void Flush() override
Definition logger.cc:73
Logger(google::base::Logger *wrapped)
Definition logger.cc:35
void FindModuleName(std::string *log_message, std::string *module_name)
Definition logger_util.h:66
class register implement
Definition arena_queue.h:37