-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.cpp
58 lines (50 loc) · 1.85 KB
/
util.cpp
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
/*
* Copyright (C) 2019 The Android Open Source Project
*
* 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 "util.h"
#include <android-base/format.h>
#include <android-base/parseint.h>
#include <server_configurable_flags/get_flags.h>
using android::base::ParseInt;
using server_configurable_flags::GetServerConfigurableFlag;
socklen_t sockaddrSize(const sockaddr* sa) {
if (sa == nullptr) return 0;
switch (sa->sa_family) {
case AF_INET:
return sizeof(sockaddr_in);
case AF_INET6:
return sizeof(sockaddr_in6);
default:
return 0;
}
}
socklen_t sockaddrSize(const sockaddr_storage& ss) {
return sockaddrSize(reinterpret_cast<const sockaddr*>(&ss));
}
int getExperimentFlagInt(const std::string& flagName, int defaultValue) {
int val = defaultValue;
ParseInt(GetServerConfigurableFlag("netd_native", flagName, ""), &val);
return val;
}
std::string timestampToString(const std::chrono::system_clock::time_point& ts) {
using std::chrono::duration_cast;
using std::chrono::milliseconds;
const auto time_sec = std::chrono::system_clock::to_time_t(ts);
char buf[32];
std::strftime(buf, sizeof(buf), "%H:%M:%S", std::localtime(&time_sec));
int ms = duration_cast<milliseconds>(ts.time_since_epoch()).count() % 1000;
return fmt::format("{}.{:03d}", buf, ms);
}