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 <core/namespace.h>
28
29 #include "log/logger.h"
30
31 namespace LoggerUtils {
GetFilename(std::string_view path)32 std::string_view GetFilename(std::string_view path)
33 {
34 if (auto const pos = path.find_last_of("\\/"); pos != std::string_view::npos) {
35 return path.substr(pos + 1);
36 }
37 return path;
38 }
39
PrintTimeStamp(std::ostream & outputStream)40 void PrintTimeStamp(std::ostream& outputStream)
41 {
42 const auto now = std::chrono::system_clock::now();
43 const auto time = std::chrono::system_clock::to_time_t(now);
44 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) -
45 std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
46
47 outputStream << std::put_time(std::localtime(&time), "%D %H:%M:%S.");
48 outputStream << std::right << std::setfill('0') << std::setw(3) // 3: alignment
49 << ms.count() << std::setfill(' ');
50 }
51 } // namespace LoggerUtils
52
53
54 CORE_BEGIN_NAMESPACE()
55 using BASE_NS::string_view;
56
57
58 class FileOutput final : public ILogger::IOutput {
59 public:
FileOutput(const string_view filePath)60 explicit FileOutput(const string_view filePath) : IOutput(), outputStream_(filePath.data(), std::ios::app) {}
61
62 ~FileOutput() override = default;
63
Write(ILogger::LogLevel logLevel,const string_view filename,int linenumber,const string_view message)64 void Write(
65 ILogger::LogLevel logLevel, const string_view filename, int linenumber, const string_view message) override
66 {
67 if (outputStream_.is_open()) {
68 auto& outputStream = outputStream_;
69
70 LoggerUtils::PrintTimeStamp(outputStream);
71 const auto levelString = Logger::GetLogLevelName(logLevel, true);
72 outputStream << ' ' << std::string_view(levelString.data(), levelString.size());
73
74 if (!filename.empty()) {
75 outputStream << " (" << std::string_view(filename.data(), filename.size()) << ':' << linenumber
76 << "): ";
77 } else {
78 outputStream << ": ";
79 }
80
81 outputStream << std::string_view(message.data(), message.size()) << std::endl;
82 }
83 }
84
85 protected:
Destroy()86 void Destroy() override
87 {
88 delete this;
89 }
90
91 private:
92 std::ofstream outputStream_;
93 };
94
CreateLoggerFileOutput(const string_view filename)95 ILogger::IOutput::Ptr CreateLoggerFileOutput(const string_view filename)
96 {
97 return ILogger::IOutput::Ptr { new FileOutput(filename) };
98 }
99 CORE_END_NAMESPACE()
100