Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
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 : : #pragma once
16 : :
17 : : #include <memory>
18 : : #include <mutex>
19 : : #include <string>
20 : : #include <string_view>
21 : : #include <vector>
22 : :
23 : : #include "hpactor/types/types.hpp" // for result<T>
24 : :
25 : : namespace hpactor::log {
26 : :
27 : : struct LogConfig;
28 : : struct RotatingFileConfig;
29 : :
30 : : class ILogSink {
31 : : public:
32 : 121 : virtual ~ILogSink() = default;
33 : : virtual result<void> write(std::string_view line) noexcept = 0;
34 : : virtual result<void> flush() noexcept = 0;
35 : : };
36 : :
37 : : // In-memory sink for tests
38 : : class MemorySink : public ILogSink {
39 : : public:
40 : 572 : result<void> write(std::string_view line) noexcept override {
41 : 572 : std::lock_guard<std::mutex> lock(mutex_);
42 : 572 : lines_.emplace_back(line);
43 : 572 : return result<void>::make();
44 : 572 : }
45 : :
46 : 111 : result<void> flush() noexcept override {
47 : 111 : return result<void>::make();
48 : : }
49 : :
50 : : std::vector<std::string> lines() const {
51 : : std::lock_guard<std::mutex> lock(mutex_);
52 : : return lines_;
53 : : }
54 : :
55 : : void clear() {
56 : : std::lock_guard<std::mutex> lock(mutex_);
57 : : lines_.clear();
58 : : }
59 : :
60 : : private:
61 : : mutable std::mutex mutex_;
62 : : std::vector<std::string> lines_;
63 : : };
64 : :
65 : : // Factory functions (implemented in respective .cpp files)
66 : : std::unique_ptr<ILogSink> make_stderr_sink();
67 : : std::unique_ptr<ILogSink> make_file_sink(const std::string& path);
68 : : std::unique_ptr<ILogSink> make_rotating_file_sink(const RotatingFileConfig& cfg);
69 : :
70 : : } // namespace hpactor::log
|