1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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
16 #include "platform/platform.h"
17
18 #include <cstdarg>
19
20 #include "unistd.h"
21
22 namespace platform {
Abort()23 void OS::Abort()
24 {
25 std::abort();
26 }
27
GetUid()28 uint64_t OS::GetUid()
29 {
30 return static_cast<uint64_t>(getuid());
31 }
32
GetPid()33 uint64_t OS::GetPid()
34 {
35 return static_cast<uint64_t>(getppid());
36 }
37
GetTid()38 uint64_t OS::GetTid()
39 {
40 return static_cast<uint64_t>(gettid());
41 }
42
PrintString(OS::LogLevel level,const char * str)43 void OS::PrintString(OS::LogLevel level, const char* str)
44 {
45 (void)level;
46 printf("%s", str);
47 }
48
VPrint(const char * format,va_list args)49 void VPrint(const char* format, va_list args)
50 {
51 vprintf(format, args);
52 }
53
VPrintError(const char * format,va_list args)54 void VPrintError(const char* format, va_list args)
55 {
56 (void)vfprintf(stderr, format, args);
57 }
58
Print(OS::LogLevel level,const char * format,...)59 void OS::Print(OS::LogLevel level, const char* format, ...)
60 {
61 va_list args;
62 va_start(args, format);
63 if (static_cast<uint64_t>(level) > static_cast<uint64_t>(OS::LogLevel::LOG_WARN)) {
64 VPrintError(format, args);
65 } else {
66 VPrint(format, args);
67 }
68 va_end(args);
69 }
70
71 } // namespace platform
72