1 /*
2 * Copyright (c) 2024-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 #ifndef SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H
16 #define SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H
17 #include <stdio.h>
18 #include <iostream>
19 #include <time.h>
20 #include <chrono>
21 #include <ctime>
22 #include <iomanip>
23 #include <sstream>
24
25 #include "signature_tools_errno.h"
26
27 namespace OHOS {
28 namespace SignatureTools {
29
30 static const char POINT = '.';
31 static const char PLACEHOLDER = '0';
32 static const int PLACEHOLDERLEN = 3;
33 static const int SCALE = 1000;
34
35 #define SIGNATURE_LOG(level, fmt, ...) \
36 printf("[%s] [%s] [%s] [%d] " fmt "\n", level, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) \
37
38 #ifdef SIGNATURE_LOG_DEBUG
39 #define SIGNATURE_TOOLS_LOGI(fmt, ...) SIGNATURE_LOG("Info", fmt, ##__VA_ARGS__)
40 #define SIGNATURE_TOOLS_LOGD(fmt, ...) SIGNATURE_LOG("Debug", fmt, ##__VA_ARGS__)
41 #define SIGNATURE_TOOLS_LOGW(fmt, ...) SIGNATURE_LOG("Warn", fmt, ##__VA_ARGS__)
42 #else
43 #define SIGNATURE_TOOLS_LOGI(fmt, ...)
44 #define SIGNATURE_TOOLS_LOGD(fmt, ...)
45 #define SIGNATURE_TOOLS_LOGW(fmt, ...)
46 #endif
47
48 #define SIGNATURE_TOOLS_LOGF(fmt, ...) SIGNATURE_LOG("Fatal", fmt, ##__VA_ARGS__)
49 #define SIGNATURE_TOOLS_LOGE(fmt, ...) SIGNATURE_LOG("Error", fmt, ##__VA_ARGS__)
50
GetSystemTime()51 inline std::string GetSystemTime()
52 {
53 std::string timeBuffer;
54 std::stringstream ss;
55 auto now = std::chrono::system_clock::now();
56 std::time_t nowTime = std::chrono::system_clock::to_time_t(now);
57 std::tm* localTime = std::localtime(&nowTime);
58 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % SCALE;
59 ss << std::put_time(localTime, "%m-%d %H:%M:%S");
60 ss << POINT << std::setfill(PLACEHOLDER) << std::setw(PLACEHOLDERLEN) << ms.count();
61 timeBuffer = ss.str();
62 return timeBuffer;
63 }
64
65 /*
66 * Function: Print the error code and error message to the terminal.
67 * Parametric Description: command, code, details
68 * command: Error code variable name as a string.
69 * code: Error code.
70 * details: Error description information.
71 **/
PrintErrorNumberMsg(const std::string& command, const int code, const std::string& details)72 inline void PrintErrorNumberMsg(const std::string& command, const int code, const std::string& details)
73 {
74 std::cerr << GetSystemTime() << " ERROR - " << command << ", code: "
75 << code << ". Details: " << details << std::endl;
76 }
77
78 /*
79 * Function: Print a prompt to the terminal.
80 * Parametric Description: message
81 * message: Prompt Description Information.
82 **/
PrintMsg(const std::string& message)83 inline void PrintMsg(const std::string& message)
84 {
85 std::cout << GetSystemTime() << " INFO - " << message << std::endl;
86 }
87 } // namespace SignatureTools
88 } // namespace OHOS
89 #endif // SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H