ChapterForge
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1//
2// logging.hpp
3// ChapterForge
4//
5// Created by Till Toenshoff on 12/9/25.
6// Copyright © 2025 Till Toenshoff. All rights reserved.
7//
8
9#pragma once
10
11#include <algorithm>
12#include <atomic>
13#include <cerrno>
14#include <iomanip>
15#include <iostream>
16#include <sstream>
17#include <string>
18#include <string_view>
19#include <system_error>
20#include <vector>
21#include <cstdint>
22
23namespace chapterforge {
24
27enum class LogVerbosity { Error = 0, Warn = 1, Info = 2, Debug = 3 };
28
33
34// Hex-preview helper used in debug logs to dump a short prefix of binary blobs (e.g. JPEG).
35inline constexpr size_t kHexPreviewBytes = 8;
36inline std::string hex_prefix(const std::vector<uint8_t>& data,
37 size_t max_len = kHexPreviewBytes) {
38 std::ostringstream oss;
39 oss << std::hex << std::setfill('0');
40 const size_t limit = std::min(max_len, data.size());
41 for (size_t i = 0; i < limit; ++i) {
42 oss << std::setw(2) << static_cast<unsigned int>(data[i]);
43 if (i + 1 != limit) {
44 oss << ' ';
45 }
46 }
47 return oss.str();
48}
49
50} // namespace chapterforge
51
52inline constexpr chapterforge::LogVerbosity ch_severity_for_tag(std::string_view tag) {
53 if (tag == "error") {
55 }
56 if (tag == "warn" || tag == "warning") {
58 }
59 if (tag == "info") {
61 }
62 // Everything else (io/parser/meta/etc.) treated as debug-level.
64}
65
66inline bool ch_should_log(const char* level) {
67 const auto current = chapterforge::get_log_verbosity();
68 const auto sev = ch_severity_for_tag(level ? level : "");
69 return static_cast<int>(sev) <= static_cast<int>(current);
70}
71
72inline void ch_log_impl(const char* level, const std::string& msg, const char* file, int line,
73 const char* func) {
74 std::string lvl(level ? level : "");
75 if (lvl == "error") {
76 std::cerr << "[ChapterForge][" << level << "][" << file << ":" << line << " " << func
77 << "] " << msg << std::endl;
78 } else {
79 std::cerr << "[ChapterForge][" << level << "] " << msg << std::endl;
80 }
81}
82
83#define CH_LOG(level, message) \
84 do { \
85 if (ch_should_log(level)) { \
86 std::ostringstream _ch_log_ss; \
87 _ch_log_ss << message; \
88 ch_log_impl(level, _ch_log_ss.str(), __FILE__, __LINE__, \
89 __func__); \
90 } \
91 } while (0)
LogVerbosity
Definition logging.hpp:27
void ch_log_impl(const char *level, const std::string &msg, const char *file, int line, const char *func)
Definition logging.hpp:72
bool ch_should_log(const char *level)
Definition logging.hpp:66
constexpr chapterforge::LogVerbosity ch_severity_for_tag(std::string_view tag)
Definition logging.hpp:52
Definition chapterforge.hpp:19
LogVerbosity get_log_verbosity()
Get current logging verbosity.
constexpr size_t kHexPreviewBytes
Definition logging.hpp:35
std::string hex_prefix(const std::vector< uint8_t > &data, size_t max_len=kHexPreviewBytes)
Definition logging.hpp:36
void set_log_verbosity(LogVerbosity level)
Set global logging verbosity.