diff options
author | Patrick Venture <venture@google.com> | 2018-08-29 11:41:01 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2018-11-11 08:21:26 -0800 |
commit | 59a6b1f27e83066baa6f3711c88d6a3b9a5c4d49 (patch) | |
tree | 56c0b0abc5dd83b3d83ab598f47df663b8561e80 /phosphor-logging | |
parent | 30047bf9647215951ba5dfe21ceb3e58a1b405a4 (diff) | |
download | phosphor-logging-59a6b1f27e83066baa6f3711c88d6a3b9a5c4d49.tar.gz phosphor-logging-59a6b1f27e83066baa6f3711c88d6a3b9a5c4d49.zip |
add sdjournal interface to inject tests
The goal of the tests is not to test phosphor-logging, but rather allow
code to call through phosphor-logging/log<> during a test.
Change-Id: Id8c84ded473decc7f9f0be268116083093f86e54
Signed-off-by: Patrick Venture <venture@google.com>
Diffstat (limited to 'phosphor-logging')
-rw-r--r-- | phosphor-logging/log.hpp | 3 | ||||
-rw-r--r-- | phosphor-logging/sdjournal.hpp | 72 | ||||
-rw-r--r-- | phosphor-logging/test/sdjournal_mock.hpp | 27 |
3 files changed, 101 insertions, 1 deletions
diff --git a/phosphor-logging/log.hpp b/phosphor-logging/log.hpp index e20605b..673848c 100644 --- a/phosphor-logging/log.hpp +++ b/phosphor-logging/log.hpp @@ -18,6 +18,7 @@ #include <systemd/sd-journal.h> +#include <phosphor-logging/sdjournal.hpp> #include <sdbusplus/server/transaction.hpp> #include <tuple> #include <type_traits> @@ -99,7 +100,7 @@ template <typename T, size_t... I> void helper_log(T&& e, std::integer_sequence<size_t, I...>) { // https://www.freedesktop.org/software/systemd/man/sd_journal_print.html - sd_journal_send(std::get<I>(std::forward<T>(e))..., NULL); + sdjournal_ptr->journal_send(std::get<I>(std::forward<T>(e))..., NULL); } /** @fn details::log() diff --git a/phosphor-logging/sdjournal.hpp b/phosphor-logging/sdjournal.hpp new file mode 100644 index 0000000..6aa2394 --- /dev/null +++ b/phosphor-logging/sdjournal.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include <systemd/sd-journal.h> + +#include <cstdarg> + +namespace phosphor +{ +namespace logging +{ + +/** + * Implementation that calls into real sd_journal methods. + */ +class SdJournalHandler +{ + public: + SdJournalHandler() = default; + virtual ~SdJournalHandler() = default; + SdJournalHandler(const SdJournalHandler&) = default; + SdJournalHandler& operator=(const SdJournalHandler&) = default; + SdJournalHandler(SdJournalHandler&&) = default; + SdJournalHandler& operator=(SdJournalHandler&&) = default; + + /** + * Provide a fake method that's called by the real method so we can catch + * the journal_send call in testing. + * + * @param[in] fmt - the format string passed into journal_send. + * @return an int meant to be intepreted by the journal_send caller during + * testing. + */ + virtual int journal_send_call(const char* fmt) + { + return 0; + }; + + /** + * Send the information to sd_journal_send. + * + * @param[in] fmt - c string format. + * @param[in] ... - parameters. + * @return value from sd_journal_send + * + * sentinel default makes sure the last parameter is null. + */ + virtual int journal_send(const char* fmt, ...) + __attribute__((format(printf, 2, 0))) __attribute__((sentinel)) + { + va_list args; + va_start(args, fmt); + + int rc = ::sd_journal_send(fmt, args, NULL); + va_end(args); + + return rc; + } +}; + +extern SdJournalHandler* sdjournal_ptr; + +/** + * Swap out the sdjournal_ptr used by log<> such that a test + * won't need to hit the real sd_journal and fail. + * + * @param[in] with - pointer to your sdjournal_mock object. + * @return pointer to the previously stored sdjournal_ptr. + */ +SdJournalHandler* SwapJouralImpl(SdJournalHandler* with); + +} // namespace logging +} // namespace phosphor diff --git a/phosphor-logging/test/sdjournal_mock.hpp b/phosphor-logging/test/sdjournal_mock.hpp new file mode 100644 index 0000000..3290480 --- /dev/null +++ b/phosphor-logging/test/sdjournal_mock.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include <phosphor-logging/sdjournal.hpp> + +#include <gmock/gmock.h> + +namespace phosphor +{ +namespace logging +{ + +class SdJournalMock : public SdJournalImpl +{ + public: + virtual ~SdJournalMock() = default; + + /* Set your mock to catch this call. */ + MOCK_METHOD1(journal_send_call, int(const char*)); + + int journal_send(const char* fmt, ...) override + { + return journal_send_call(fmt); + } +} + +} // namespace logging +} // namespace phosphor |