Skip to content

Commit 1696011

Browse files
Add FileLogSink class
1 parent f6bdb41 commit 1696011

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) 2025 Albin Johansson
2+
// This software is provided under the terms of the MIT License.
3+
4+
#pragma once
5+
6+
#include <fstream>
7+
8+
#include "tactile/core/container/path.hpp"
9+
#include "tactile/core/log/log_sink.hpp"
10+
11+
namespace tactile {
12+
13+
/// A log sink that forwards messages to a file.
14+
class FileLogSink final : public ILogSink
15+
{
16+
public:
17+
explicit FileLogSink(const Path& path);
18+
19+
void flush() override;
20+
21+
void log(const LogEntry& entry) override;
22+
23+
private:
24+
std::ofstream m_file {};
25+
};
26+
27+
} // namespace tactile
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2025 Albin Johansson
2+
// This software is provided under the terms of the MIT License.
3+
4+
#include "tactile/core/log/file_log_sink.hpp"
5+
6+
#include <ios>
7+
#include <ostream>
8+
#include <stdexcept>
9+
10+
namespace tactile {
11+
12+
FileLogSink::FileLogSink(const Path& path)
13+
: m_file {path, std::ios::out | std::ios::trunc}
14+
{
15+
if (!m_file.good()) {
16+
throw std::runtime_error {"[FileLogSink]: could not create file"};
17+
}
18+
}
19+
20+
void FileLogSink::flush()
21+
{
22+
m_file << std::flush;
23+
}
24+
25+
void FileLogSink::log(const LogEntry& entry)
26+
{
27+
m_file << entry.prefix << ' ' << entry.message << '\n';
28+
}
29+
30+
} // namespace tactile

tools/cmake/targets/tactile_core.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ target_sources(tactile_core
2020
"${PROJECT_SOURCE_DIR}/include/tactile/core/container/variant.hpp"
2121
"${PROJECT_SOURCE_DIR}/include/tactile/core/container/vector.hpp"
2222
"${PROJECT_SOURCE_DIR}/include/tactile/core/log/console_log_sink.hpp"
23+
"${PROJECT_SOURCE_DIR}/include/tactile/core/log/file_log_sink.hpp"
2324
"${PROJECT_SOURCE_DIR}/include/tactile/core/log/log_category.hpp"
2425
"${PROJECT_SOURCE_DIR}/include/tactile/core/log/log_entry.hpp"
2526
"${PROJECT_SOURCE_DIR}/include/tactile/core/log/log_sink.hpp"
@@ -36,6 +37,7 @@ target_sources(tactile_core
3637

3738
PRIVATE
3839
"${PROJECT_SOURCE_DIR}/src/tactile/core/log/console_log_sink.cpp"
40+
"${PROJECT_SOURCE_DIR}/src/tactile/core/log/file_log_sink.cpp"
3941
"${PROJECT_SOURCE_DIR}/src/tactile/core/log/logger.cpp"
4042
"${PROJECT_SOURCE_DIR}/src/tactile/core/meta/attribute.cpp"
4143
)

0 commit comments

Comments
 (0)