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