2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
#ifndef CONTEXT_H
|
|
|
|
#define CONTEXT_H
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
2014-10-19 14:48:19 +02:00
|
|
|
#include "base/string.hpp"
|
2022-10-28 17:19:50 +02:00
|
|
|
#include <functional>
|
2022-10-28 16:34:10 +02:00
|
|
|
#include <vector>
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2017-12-31 07:22:16 +01:00
|
|
|
class ContextTrace
|
2013-11-19 07:49:41 +01:00
|
|
|
{
|
|
|
|
public:
|
2018-01-04 04:25:35 +01:00
|
|
|
ContextTrace();
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
void Print(std::ostream& fp) const;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
size_t GetLength() const;
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
private:
|
2022-10-28 16:34:10 +02:00
|
|
|
std::vector<String> m_Frames;
|
2013-11-19 07:49:41 +01:00
|
|
|
};
|
|
|
|
|
2017-12-31 07:22:16 +01:00
|
|
|
std::ostream& operator<<(std::ostream& stream, const ContextTrace& trace);
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A context frame.
|
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class ContextFrame
|
2013-11-19 07:49:41 +01:00
|
|
|
{
|
|
|
|
public:
|
2022-10-28 17:19:50 +02:00
|
|
|
ContextFrame(std::function<void(std::ostream&)> message);
|
2018-01-04 04:25:35 +01:00
|
|
|
~ContextFrame();
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
private:
|
2022-10-28 17:19:50 +02:00
|
|
|
static std::vector<std::function<void(std::ostream&)>>& GetFrames();
|
2013-11-19 07:49:41 +01:00
|
|
|
|
|
|
|
friend class ContextTrace;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The currentContextFrame variable has to be volatile in order to prevent
|
|
|
|
* the compiler from optimizing it away. */
|
2022-10-28 17:19:50 +02:00
|
|
|
#define CONTEXT(message) volatile icinga::ContextFrame currentContextFrame ([&](std::ostream& _CONTEXT_stream) { \
|
|
|
|
_CONTEXT_stream << message; \
|
|
|
|
})
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONTEXT_H */
|