This commit is contained in:
Bart Beumer 2025-07-27 10:16:59 +00:00
parent ea55d3e6fd
commit a745b47cf1
12 changed files with 98 additions and 72 deletions

37
.clang-format Normal file
View File

@ -0,0 +1,37 @@
---
AlignAfterOpenBracket: Align
AllowAllArgumentsOnNextLine: 'false'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBraces: Allman
BreakConstructorInitializers: BeforeComma
ColumnLimit: '120'
Cpp11BracedListStyle: 'true'
FixNamespaceComments: 'true'
IncludeBlocks: Merge
IndentCaseLabels: 'true'
IndentWidth: '4'
Language: Cpp
MaxEmptyLinesToKeep: '2'
NamespaceIndentation: Inner
PointerAlignment: Left
SortUsingDeclarations: 'true'
SpaceAfterTemplateKeyword: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInSquareBrackets: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: Never
...

View File

@ -5,10 +5,10 @@
namespace awesome_log namespace awesome_log
{ {
class console_writer : public writer_interface class console_writer : public writer_interface
{ {
public: public:
~console_writer() override; ~console_writer() override;
void log(std::string_view msg) override; void log(std::string_view msg) override;
}; };
} } // namespace awesome_log

View File

@ -5,8 +5,8 @@
namespace awesome_log namespace awesome_log
{ {
class writer_interface; class writer_interface;
writer_interface& get_writer(); writer_interface& get_writer();
void set_writer(const std::shared_ptr<writer_interface>& writer); void set_writer(const std::shared_ptr<writer_interface>& writer);
} } // namespace awesome_log

View File

@ -5,10 +5,10 @@
namespace awesome_log namespace awesome_log
{ {
class null_writer : public writer_interface class null_writer : public writer_interface
{ {
public: public:
~null_writer() override; ~null_writer() override;
void log(std::string_view msg) override; void log(std::string_view msg) override;
}; };
} } // namespace awesome_log

View File

@ -6,22 +6,22 @@
namespace awesome_log namespace awesome_log
{ {
class scoped_log class scoped_log
{ {
public: public:
scoped_log(std::string_view msg); scoped_log(std::string_view msg);
~scoped_log(); ~scoped_log();
scoped_log() = delete; scoped_log() = delete;
// No copying or moving allowed. // No copying or moving allowed.
scoped_log(const scoped_log&) = delete; scoped_log(const scoped_log&) = delete;
scoped_log(scoped_log&&) = delete; scoped_log(scoped_log&&) = delete;
scoped_log& operator=(const scoped_log&) = delete; scoped_log& operator=(const scoped_log&) = delete;
scoped_log& operator=(scoped_log&&) = delete; scoped_log& operator=(scoped_log&&) = delete;
private: private:
std::string m_msg; std::string m_msg;
int m_uncaught_exceptions; int m_uncaught_exceptions;
}; };
} } // namespace awesome_log

View File

@ -5,11 +5,11 @@
namespace awesome_log namespace awesome_log
{ {
class writer_interface class writer_interface
{ {
public: public:
virtual ~writer_interface() = default; virtual ~writer_interface() = default;
virtual void log(std::string_view msg) = 0; virtual void log(std::string_view msg) = 0;
}; };
} } // namespace awesome_log

View File

@ -5,12 +5,12 @@ using awesome_log::writer_interface;
namespace namespace
{ {
std::shared_ptr<writer_interface> g_instance; std::shared_ptr<writer_interface> g_instance;
} }
writer_interface& awesome_log::get_writer() writer_interface& awesome_log::get_writer()
{ {
if(!g_instance) if (!g_instance)
{ {
g_instance = std::make_shared<null_writer>(); g_instance = std::make_shared<null_writer>();
} }

View File

@ -1,8 +1,8 @@
#include <awesome_log/scoped_log.hpp>
#include <awesome_log/log.hpp> #include <awesome_log/log.hpp>
#include <awesome_log/scoped_log.hpp>
#include <awesome_log/writer_interface.hpp> #include <awesome_log/writer_interface.hpp>
#include <sstream>
#include <exception> #include <exception>
#include <sstream>
using namespace awesome_log; using namespace awesome_log;

View File

@ -6,7 +6,7 @@
class mock_writer : public awesome_log::writer_interface class mock_writer : public awesome_log::writer_interface
{ {
public: public:
~mock_writer() override = default; ~mock_writer() override = default;
MOCK_METHOD(void, log, (std::string_view), (override)); MOCK_METHOD(void, log, (std::string_view), (override));

View File

@ -1,27 +1,22 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "mock_writer.hpp" #include "mock_writer.hpp"
#include <awesome_log/log.hpp> #include <awesome_log/log.hpp>
#include <awesome_log/scoped_log.hpp> #include <awesome_log/scoped_log.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory> #include <memory>
class test_scoped_log : public ::testing::Test class test_scoped_log : public ::testing::Test
{ {
public: public:
void SetUp() override void SetUp() override
{ {
m_writer = std::make_shared<mock_writer>(); m_writer = std::make_shared<mock_writer>();
awesome_log::set_writer(m_writer); awesome_log::set_writer(m_writer);
} }
void TearDown() override void TearDown() override { awesome_log::set_writer(std::shared_ptr<awesome_log::writer_interface>()); }
{
awesome_log::set_writer(std::shared_ptr<awesome_log::writer_interface>());
}
protected: protected:
std::shared_ptr<mock_writer> m_writer; std::shared_ptr<mock_writer> m_writer;
}; };
@ -45,7 +40,7 @@ TEST_F(test_scoped_log, log_stack_unwind)
awesome_log::scoped_log a("log"); awesome_log::scoped_log a("log");
throw std::runtime_error(""); throw std::runtime_error("");
} }
catch(const std::exception& e) catch (const std::exception& e)
{ {
// Not doing anything // Not doing anything
} }

View File

@ -1,7 +1,7 @@
#include <sstream>
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <iostream>
#include <sstream>
struct position struct position
{ {
@ -9,23 +9,19 @@ struct position
int y; int y;
template<class Archive> template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/) void serialize(Archive& ar, const unsigned int /*version*/)
{ {
ar & x; ar & x;
ar & y; ar & y;
} }
bool operator==(const position& rhs) const bool operator==(const position& rhs) const { return x == rhs.x && y == rhs.y; }
{
return x == rhs.x
&& y == rhs.y;
}
}; };
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
position myposition{.x=123, .y=456}; position myposition{.x = 123, .y = 456};
std::stringstream ss; std::stringstream ss;
{ {
@ -44,5 +40,4 @@ int main(int argc, char* argv[])
{ {
std::cout << "positions are the same" << std::endl; std::cout << "positions are the same" << std::endl;
} }
} }

View File

@ -1,7 +1,6 @@
#include <awesome_log/console_writer.hpp>
#include <awesome_log/log.hpp> #include <awesome_log/log.hpp>
#include <awesome_log/scoped_log.hpp> #include <awesome_log/scoped_log.hpp>
#include <awesome_log/console_writer.hpp>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -22,7 +21,7 @@ int main(int argc, char* argv[])
throw std::runtime_error("Let's play!"); throw std::runtime_error("Let's play!");
// ... unreachable code goes here. // ... unreachable code goes here.
} }
catch(const std::exception& e) catch (const std::exception& e)
{ {
std::cout << "Caught an exception, as expected in this example" << std::endl; std::cout << "Caught an exception, as expected in this example" << std::endl;
} }