Skip to content

Commit b16490e

Browse files
authored
Merge pull request #71 from SLM-Audio/syl/logging
Add logging, and document
2 parents f0c0b74 + 1b67123 commit b16490e

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

include/mostly_harmless/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(MOSTLYHARMLESS_HEADERS
2626
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_OnScopeExit.h
2727
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Proxy.h
2828
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_NoDenormals.h
29+
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Logging.h
2930
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabaseState.h
3031
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabasePropertyWatcher.h
3132
${PLATFORM_HEADERS}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Created by sylmo on 05/10/2025.
3+
//
4+
5+
#ifndef MOSTLYHARMLESS_LOGGING_H
6+
#define MOSTLYHARMLESS_LOGGING_H
7+
#include <filesystem>
8+
namespace mostly_harmless::utils {
9+
/***
10+
* \brief Helper class for logging to a file.
11+
*/
12+
class Logging final {
13+
public:
14+
/**
15+
* Constructor - if logFile doesn't already exist, creates it. Writes a newline specifying that logging has been started, to distinguish between multiple sessions with this file
16+
* @param logFile The destination file for your logging output to be written to
17+
*/
18+
explicit Logging(std::filesystem::path logFile);
19+
/**
20+
* Destructor - Writes a newline specifying that logging has been ended, to distinguish between multiple sessions with this file.
21+
*/
22+
~Logging() noexcept;
23+
24+
/**
25+
* Writes some text to your file, followed by a newline.
26+
* @param toDump The text to dump to the log file
27+
*/
28+
auto dump(std::string_view toDump) const noexcept -> void;
29+
private:
30+
std::filesystem::path m_logFile;
31+
};
32+
}
33+
#endif //MOSTLYHARMLESS_LOGGING_H

source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(MOSTLYHARMLESS_SOURCES
3232
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_NoDenormals.cpp
3333
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Hash.cpp
3434
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Macros.cpp
35+
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Logging.cpp
3536
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabaseState.cpp
3637
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabasePropertyWatcher.cpp
3738
${PLATFORM_SOURCES}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by sylmo on 05/10/2025.
3+
//
4+
#include <mostly_harmless/utils/mostlyharmless_Logging.h>
5+
#include <mostly_harmless/utils/mostlyharmless_OnScopeExit.h>
6+
7+
#include <utility>
8+
#include <fstream>
9+
10+
namespace mostly_harmless::utils {
11+
Logging::Logging(std::filesystem::path logFile) : m_logFile(std::move(logFile)) {
12+
13+
auto writeMode = std::ios::out;
14+
if(std::filesystem::exists(m_logFile)) {
15+
writeMode |= std::ios::app;
16+
}
17+
std::ofstream outputFile{ m_logFile, writeMode };
18+
outputFile << "------ START LOG ------ \n";
19+
outputFile.flush();
20+
}
21+
22+
Logging::~Logging() noexcept {
23+
dump("------ END LOG ------ \n");
24+
}
25+
26+
auto Logging::dump(std::string_view toDump) const noexcept -> void {
27+
try {
28+
std::ofstream outStream{ m_logFile, std::ios::app | std::ios::out };
29+
outStream << toDump << "\n";
30+
outStream.flush();
31+
} catch (...) {
32+
}
33+
}
34+
35+
36+
} // namespace mostly_harmless::utils

0 commit comments

Comments
 (0)