Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "sys_utils.h"
11 : #include <map>
12 : #include <ctime>
13 : #include <chrono>
14 : #include <sstream>
15 : #include <iomanip>
16 : #include "adx_log.h"
17 :
18 : namespace Adx {
19 : namespace {
20 : constexpr uint64_t SEC_TO_USEC = 1000000UL;
21 : constexpr uint64_t SEC_TO_MSEC = 1000UL;
22 : constexpr uint32_t MILLI_S_FORMAT_LEN = 3;
23 : } // namespace
24 :
25 0 : std::string SysUtils::GetCurrentWorkDir()
26 : {
27 0 : char currentPath[MMPA_MAX_PATH] = {0};
28 0 : return mmGetCwd(currentPath, MMPA_MAX_PATH) == EN_OK ? currentPath : "";
29 : }
30 :
31 273 : std::string SysUtils::HandleEnv(const char* env)
32 : {
33 273 : if (env == nullptr) {
34 284 : return "";
35 : }
36 131 : size_t lenOfRet = strlen(env);
37 131 : size_t envLen = lenOfRet;
38 131 : if (lenOfRet < MMPA_MEM_MAX_LEN - 1) {
39 131 : envLen += 1;
40 : }
41 :
42 131 : if ((envLen != MMPA_ZERO) && (MMPA_MAX_PATH - 1 < envLen)) {
43 6 : return "";
44 : }
45 128 : char envValue[MMPA_MAX_PATH] = {0};
46 178 : IDE_CTRL_VALUE_FAILED(memcpy_s(envValue, MMPA_MAX_PATH - 1, env, envLen) == EN_OK, return "",
47 : "Failed to copy environment data %s.", env);
48 :
49 206 : return envValue;
50 : }
51 :
52 52 : uint64_t SysUtils::GetTimestamp()
53 : {
54 52 : uint64_t time = 0U;
55 : mmTimeval tv;
56 52 : if (mmGetTimeOfDay(&tv, nullptr) == 0) {
57 52 : time = (static_cast<uint64_t>(tv.tv_sec) * SEC_TO_USEC) + static_cast<uint64_t>(tv.tv_usec);
58 : }
59 52 : return time;
60 : }
61 :
62 346 : std::string SysUtils::GetCurrentTime()
63 : {
64 346 : const std::time_t now = std::time(nullptr);
65 346 : const std::tm *const ptm = std::localtime(&now);
66 346 : if (ptm == nullptr) {
67 0 : return "";
68 : }
69 :
70 346 : constexpr int32_t timeBufferLen = 32;
71 346 : char buffer[timeBufferLen + 1] = {0};
72 346 : (void)std::strftime(&buffer[0], static_cast<size_t>(timeBufferLen), "%Y%m%d%H%M%S", ptm);
73 692 : return std::string(buffer);
74 : }
75 :
76 70 : std::string SysUtils::GetCurrentTimeWithMillisecond()
77 : {
78 70 : auto now = std::chrono::system_clock::now();
79 70 : std::time_t nowClock = std::chrono::system_clock::to_time_t(now);
80 70 : auto duration = now.time_since_epoch();
81 70 : auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % SEC_TO_MSEC;
82 :
83 70 : std::tm localTime{};
84 70 : if (localtime_r(&nowClock, &localTime) == nullptr) {
85 3 : return DEFAULT_MILLISECOND_TIME;
86 : }
87 :
88 67 : std::ostringstream oss;
89 67 : oss << std::put_time(&localTime, "%Y%m%d%H%M%S");
90 67 : oss << std::setfill('0') << std::setw(MILLI_S_FORMAT_LEN) << millis;
91 :
92 67 : return oss.str();
93 67 : }
94 :
95 0 : std::string SysUtils::GetPid()
96 : {
97 0 : const auto tid = mmGetPid();
98 0 : if (tid != EN_ERROR) {
99 0 : return std::to_string(tid);
100 : }
101 0 : return "";
102 : }
103 : } // namespace Adx
|