1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <chrono>
16 #include <cstdarg>
17 #include <cstdio>
18 #include <ctime>
19 #include <fstream>
20
21 namespace netsim {
22
BtsLog(const char * fmt,...)23 void BtsLog(const char *fmt, ...) {
24 char buffer[255];
25 va_list arglist;
26
27 va_start(arglist, fmt);
28 vsnprintf(buffer, sizeof(buffer), fmt, arglist);
29 va_end(arglist);
30
31 auto now = std::chrono::system_clock::now();
32 auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
33 auto now_t = std::chrono::system_clock::to_time_t(now);
34 //"mm-dd_HH:MM:SS.sss\0" is 19 byte long
35 char prefix[19];
36 auto l = std::strftime(prefix, sizeof(prefix), "%m-%d %H:%M:%S",
37 std::localtime(&now_t));
38 snprintf(prefix + l, sizeof(prefix) - l, ".%03u",
39 static_cast<unsigned int>(now_ms.time_since_epoch().count() % 1000));
40 fprintf(stderr, "netsim D %s %s\n", prefix, buffer);
41 }
42
43 } // namespace netsim
44