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 "log/logger_output.h"
17
18 #include <chrono>
19 #include <cstdarg>
20 #include <ctime>
21 #include <fstream>
22 #include <hilog/log.h>
23 #include <iomanip>
24 #include <iostream>
25 #include <sstream>
26 #include <string_view>
27 #include <unistd.h>
28
29 #include <core/namespace.h>
30
31 #include "log/logger.h"
32
33 CORE_BEGIN_NAMESPACE()
34 using BASE_NS::string_view;
35
36 namespace {
37 // Gets the filename part from the path.
GetFilename(std::string_view path)38 std::string_view GetFilename(std::string_view path)
39 {
40 if (auto const pos = path.find_last_of("\\/"); pos != std::string_view::npos) {
41 return path.substr(pos + 1);
42 }
43 return path;
44 }
45
PrintTimeStamp(std::ostream & outputStream)46 void PrintTimeStamp(std::ostream& outputStream)
47 {
48 const auto now = std::chrono::system_clock::now();
49 const auto time = std::chrono::system_clock::to_time_t(now);
50 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) -
51 std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
52
53 outputStream << std::put_time(std::localtime(&time), "%D %H:%M:%S.");
54 outputStream << std::right << std::setfill('0') << std::setw(3) << ms.count() << std::setfill(' '); // 3: alignment
55 }
56 } // namespace
57
58 class LogcatOutput final : public Logger::IOutput {
59 public:
Write(ILogger::LogLevel logLevel,const string_view filename,int linenumber,const string_view message)60 void Write(
61 ILogger::LogLevel logLevel, const string_view filename, int linenumber, const string_view message) override
62 {
63 int logPriority;
64 switch (logLevel) {
65 case ILogger::LogLevel::LOG_VERBOSE:
66 logPriority = LOG_LEVEL_MIN;
67 break;
68
69 case ILogger::LogLevel::LOG_DEBUG:
70 logPriority = LOG_DEBUG;
71 break;
72
73 case ILogger::LogLevel::LOG_INFO:
74 logPriority = LOG_INFO;
75 break;
76
77 case ILogger::LogLevel::LOG_WARNING:
78 logPriority = LOG_WARN;
79 break;
80
81 case ILogger::LogLevel::LOG_ERROR:
82 logPriority = LOG_ERROR;
83 break;
84
85 case ILogger::LogLevel::LOG_FATAL:
86 logPriority = LOG_FATAL;
87 break;
88
89 default:
90 logPriority = LOG_LEVEL_MIN;
91 break;
92 }
93 unsigned int domain = 0xD003B00;
94 if (!filename.empty()) {
95 std::stringstream outputStream;
96 auto const filenameView = GetFilename({ filename.data(), filename.size() });
97 outputStream << '(' << filenameView << ':' << linenumber << "): ";
98 outputStream << std::string_view(message.data(), message.size());
99 HiLogPrint(LOG_CORE, LOG_ERROR, domain, logTag_.data(), "%{public}s", outputStream.str().c_str());
100 } else {
101 HiLogPrint(LOG_CORE, LOG_ERROR, domain, logTag_.data(), "%{public}s", message.data());
102 }
103 }
104
105 protected:
Destroy()106 void Destroy() override
107 {
108 delete this;
109 }
110
111 private:
112 static constexpr std::string_view logTag_ = "ohos_lume";
113 };
114
CreateLoggerConsoleOutput()115 ILogger::IOutput::Ptr CreateLoggerConsoleOutput()
116 {
117 return ILogger::IOutput::Ptr { new LogcatOutput };
118 }
119
CreateLoggerDebugOutput()120 ILogger::IOutput::Ptr CreateLoggerDebugOutput()
121 {
122 return {};
123 }
124 CORE_END_NAMESPACE()
125