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 : : #include <chrono>
16 : : #include <hpactor/log/log_ring_buffer.hpp>
17 : : #include <hpactor/log/logger.hpp>
18 : :
19 : : namespace hpactor::log {
20 : :
21 : 0 : void Logger::emit(LogEvent event) noexcept {
22 : 0 : if (!buffer_)
23 : 0 : return;
24 : :
25 : 0 : auto now = std::chrono::system_clock::now();
26 : 0 : event.timestamp_ns = static_cast<uint64_t>(
27 : 0 : std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch())
28 : 0 : .count());
29 : :
30 : 0 : buffer_->try_push(event);
31 : :
32 : : // Nudge the drain thread for events at or above flush_on_level
33 : 0 : if (static_cast<uint8_t>(event.level) <= static_cast<uint8_t>(flush_on_level_)) {
34 : 0 : nudge();
35 : : }
36 : : }
37 : :
38 : 10556 : void Logger::emit(LogLevel level, LogCategory category, ActorId actor_id,
39 : : uint32_t event_id, const char* message, const LogField* fields,
40 : : uint8_t field_count, const char* file, uint32_t line) noexcept {
41 : 10556 : if (!buffer_)
42 : 0 : return;
43 : :
44 : 10556 : LogEvent evt{};
45 : 10556 : evt.level = level;
46 : 10556 : evt.category = category;
47 : 10556 : evt.actor_id = actor_id;
48 : 10556 : evt.event_id = event_id;
49 : 10556 : evt.message = message;
50 : 10556 : evt.file = file;
51 : 10556 : evt.line = line;
52 : 10556 : evt.worker_id = UINT32_MAX;
53 : 10556 : evt.field_count = (field_count > kMaxLogFields) ? kMaxLogFields : field_count;
54 : 10556 : if (field_count > kMaxLogFields) {
55 : 0 : fields_dropped_ += field_count - kMaxLogFields;
56 : : }
57 : 11083 : for (uint8_t i = 0; i < evt.field_count; ++i) {
58 : 527 : evt.fields[i] = fields[i];
59 : : }
60 : :
61 : 10556 : auto now = std::chrono::system_clock::now();
62 : 10556 : evt.timestamp_ns = static_cast<uint64_t>(
63 : 10556 : std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch())
64 : 10556 : .count());
65 : :
66 : 10556 : buffer_->try_push(evt);
67 : :
68 : 10556 : if (static_cast<uint8_t>(level) <= static_cast<uint8_t>(flush_on_level_)) {
69 : 0 : nudge();
70 : : }
71 : : }
72 : :
73 : : namespace {
74 : : Logger g_noop_logger; // buffer_ is null, all operations are no-ops
75 : : }
76 : :
77 : 12405 : Logger& global_logger() noexcept {
78 : 12405 : return g_noop_logger;
79 : : }
80 : :
81 : : } // namespace hpactor::log
|