A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
27#ifndef A11_ACTIONS_LOG_H_
28#define A11_ACTIONS_LOG_H_
29
30#include <functional>
31#include <optional>
32#include <string>
33#include <string_view>
34
35#include <absl/base/log_severity.h>
36#include <absl/status/statusor.h>
37#include <absl/time/time.h>
38
39#include "a11/data/types.h"
40
41namespace a11::actions {
42
49enum class LogLevel {
50 kDebug,
51 kInfo,
53 kError,
55};
56
59
61[[nodiscard]] std::string_view LogLevelName(LogLevel level);
69absl::StatusOr<LogLevel> ParseLogLevel(std::string_view name);
71[[nodiscard]] absl::LogSeverity LogLevelToSeverity(LogLevel level);
72
80inline constexpr std::string_view kLogLevelAttribute = "level";
87inline constexpr std::string_view kLogInternalAttribute = "internal";
93inline constexpr std::string_view kLogChannelAttribute = "channel";
95inline constexpr std::string_view kLogFileAttribute = "file";
97inline constexpr std::string_view kLogLinenoAttribute = "lineno";
99inline constexpr std::string_view kLogChildActionAttribute = "a11-child-action";
101inline constexpr std::string_view kLogChildCallIdAttribute =
102 "a11-child-call-id";
103
105inline constexpr std::string_view kLogInternalTrue = "true";
107inline constexpr std::string_view kLogInternalFalse = "false";
108
117 std::string_view level{};
124 std::string_view mimetype{};
126 std::string_view channel{};
128 std::string_view file{};
130 std::optional<int> lineno{};
132 bool internal = false;
134 const data::ByteMap* absl_nullable metadata = nullptr;
135};
136
143struct LogRecord {
144 std::string_view action_name{};
145 std::string_view action_id{};
147 std::string_view channel{};
148 std::string_view file{};
149 std::optional<int> lineno{};
150 bool internal = false;
151 absl::Time timestamp = absl::InfinitePast();
152 std::string_view mimetype{};
155 std::string_view data{};
156};
157
164using ActionLogSink = std::function<void(const LogRecord&)>;
165
174[[nodiscard]] ActionLogSink GetActionLogSink();
176void ReportLog(const LogRecord& record);
177
184[[nodiscard]] bool IsTextualLogMimetype(std::string_view mimetype);
185
190[[nodiscard]] std::string LogText(const LogRecord& record);
191
197[[nodiscard]] LogRecord LogRecordFromChunk(const data::Chunk& chunk,
198 std::string_view action_name = {},
199 std::string_view action_id = {});
200
201} // namespace a11::actions
202
203#endif // A11_ACTIONS_LOG_H_
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
Definition action.cc:63
constexpr std::string_view kLogChildActionAttribute
Metadata attribute naming the nested action that wrote a log.
Definition log.h:99
constexpr std::string_view kLogInternalAttribute
Metadata attribute marking a log not meant for an end user.
Definition log.h:87
std::string_view LogLevelName(LogLevel level)
Canonical lowercase name of level, as written to a chunk.
Definition log.cc:78
constexpr std::string_view kLogInternalFalse
The value kLogInternalAttribute takes when it is not.
Definition log.h:107
constexpr std::string_view kLogLevelAttribute
Metadata attribute naming the log's level.
Definition log.h:80
std::function< void(const LogRecord &)> ActionLogSink
What the process does with a log it consumes itself.
Definition log.h:164
constexpr std::string_view kLogChannelAttribute
Metadata attribute naming the log's logical channel.
Definition log.h:93
std::string LogText(const LogRecord &record)
record as one line: its payload where that is text, a description of it where it is not.
Definition log.cc:172
void ReportLog(const LogRecord &record)
Reports record to the installed sink.
Definition log.cc:149
bool IsTextualLogMimetype(std::string_view mimetype)
Whether a log payload of mimetype reads as text.
Definition log.cc:162
constexpr LogLevel kDefaultLogLevel
The default level of a log written without one.
Definition log.h:58
absl::LogSeverity LogLevelToSeverity(LogLevel level)
The Abseil severity level is reported at.
Definition log.cc:119
constexpr std::string_view kLogFileAttribute
Metadata attribute naming the source file the log came from.
Definition log.h:95
constexpr std::string_view kLogChildCallIdAttribute
Metadata attribute naming the nested action call that wrote a log.
Definition log.h:101
absl::StatusOr< LogLevel > ParseLogLevel(std::string_view name)
Parses a level name.
Definition log.cc:94
constexpr std::string_view kLogInternalTrue
The value kLogInternalAttribute takes when the log is internal.
Definition log.h:105
void SetActionLogSink(ActionLogSink sink)
Installs sink as the process's action log sink.
Definition log.cc:134
ActionLogSink GetActionLogSink()
Returns the installed sink; never null.
Definition log.cc:139
LogRecord LogRecordFromChunk(const data::Chunk &chunk, std::string_view action_name, std::string_view action_id)
Reads a LogRecord back out of a log chunk.
Definition log.cc:180
constexpr std::string_view kLogLinenoAttribute
Metadata attribute naming the source line the log came from.
Definition log.h:97
LogLevel
Severity of a log chunk.
Definition log.h:49
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:58
std::string mimetype
Definition sqlite_chunk_store.cc:191
Everything about a log other than the object being logged.
Definition log.h:115
std::string_view channel
Logical channel; see kLogChannelAttribute.
Definition log.h:126
const data::ByteMap *absl_nullable metadata
Extra attributes, merged before the fields above. Not owned.
Definition log.h:134
std::string_view level
Level name; empty is kDefaultLogLevel. See ParseLogLevel.
Definition log.h:117
bool internal
Whether this log is A11's own bookkeeping; see kLogInternalAttribute.
Definition log.h:132
std::string_view mimetype
Media type hint for the serializer.
Definition log.h:124
std::string_view file
Source file the log came from; see kLogFileAttribute.
Definition log.h:128
std::optional< int > lineno
Source line the log came from; see kLogLinenoAttribute.
Definition log.h:130
One log as a sink sees it.
Definition log.h:143
std::string_view channel
Definition log.h:147
std::string_view data
The chunk's payload as written.
Definition log.h:155
std::string_view action_name
Definition log.h:144
absl::Time timestamp
Definition log.h:151
std::string_view file
Definition log.h:148
std::string_view mimetype
Definition log.h:152
LogLevel level
Definition log.h:146
bool internal
Definition log.h:150
std::string_view action_id
Definition log.h:145
std::optional< int > lineno
Definition log.h:149
A unit of data: bytes plus optional descriptive metadata.
Definition types.h:185
A11's core wire value types: chunks, node fragments and messages.