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