forked from ApolloAuto/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.cc
134 lines (117 loc) · 3.71 KB
/
init.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* 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 "cyber/init.h"
#include <libgen.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <csignal>
#include <string>
#include "cyber/binary.h"
#include "cyber/common/environment.h"
#include "cyber/common/file.h"
#include "cyber/common/global_data.h"
#include "cyber/data/data_dispatcher.h"
#include "cyber/event/perf_event_cache.h"
#include "cyber/logger/async_logger.h"
#include "cyber/scheduler/scheduler.h"
#include "cyber/service_discovery/topology_manager.h"
#include "cyber/task/task.h"
#include "cyber/timer/timer_manager.h"
#include "cyber/transport/transport.h"
namespace apollo {
namespace cyber {
using apollo::cyber::common::EnsureDirectory;
using apollo::cyber::common::GetAbsolutePath;
using apollo::cyber::common::GetProtoFromFile;
using apollo::cyber::common::WorkRoot;
using apollo::cyber::croutine::CRoutine;
using apollo::cyber::event::PerfEventCache;
using apollo::cyber::scheduler::Scheduler;
using apollo::cyber::service_discovery::TopologyManager;
namespace {
bool g_atexit_registered = false;
std::mutex g_mutex;
logger::AsyncLogger* async_logger = nullptr;
}
namespace {
void InitLogger(const char* binary_name) {
const char* slash = strrchr(binary_name, '/');
if (slash) {
::apollo::cyber::Binary::SetName(slash + 1);
} else {
::apollo::cyber::Binary::SetName(binary_name);
}
CHECK_NOTNULL(common::GlobalData::Instance());
// Init glog
google::InitGoogleLogging(binary_name);
google::SetLogDestination(google::ERROR, "");
google::SetLogDestination(google::WARNING, "");
google::SetLogDestination(google::FATAL, "");
// Init async logger
async_logger = new ::apollo::cyber::logger::AsyncLogger(
google::base::GetLogger(FLAGS_minloglevel), 2 * 1024 * 1024);
google::base::SetLogger(FLAGS_minloglevel, async_logger);
async_logger->Start();
}
void StopLogger() {
if (async_logger != nullptr) {
async_logger->Stop();
}
}
} // namespace
void OnShutdown(int sig) {
(void)sig;
if (GetState() != STATE_SHUTDOWN) {
SetState(STATE_SHUTTING_DOWN);
}
}
void ExitHandle() { Clear(); }
bool Init(const char* binary_name) {
std::lock_guard<std::mutex> lg(g_mutex);
if (GetState() != STATE_UNINITIALIZED) {
return false;
}
InitLogger(binary_name);
std::signal(SIGINT, OnShutdown);
// Register exit handlers
if (!g_atexit_registered) {
if (std::atexit(ExitHandle) != 0) {
AERROR << "Register exit handle failed";
return false;
}
AINFO << "Register exit handle succ.";
g_atexit_registered = true;
}
SetState(STATE_INITIALIZED);
return true;
}
void Clear() {
std::lock_guard<std::mutex> lg(g_mutex);
if (GetState() == STATE_SHUTDOWN || GetState() == STATE_UNINITIALIZED) {
return;
}
TaskManager::CleanUp();
TimerManager::CleanUp();
scheduler::CleanUp();
service_discovery::TopologyManager::CleanUp();
transport::Transport::CleanUp();
StopLogger();
SetState(STATE_SHUTDOWN);
}
} // namespace cyber
} // namespace apollo