mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-25 21:45:52 +00:00
Add generic Journal class for logging
This commit is contained in:
@@ -154,6 +154,7 @@
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\beast_Debug.h" />
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\beast_Error.h" />
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\FatalError.h" />
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\Journal.h" />
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\ProtectedCall.h" />
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\beast_FPUFlags.h" />
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\beast_LeakChecked.h" />
|
||||
@@ -579,6 +580,12 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_core\diagnostic\Journal.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_core\diagnostic\ProtectedCall.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
|
||||
@@ -1025,6 +1025,9 @@
|
||||
<ClInclude Include="..\..\modules\beast_core\memory\StaticObject.h">
|
||||
<Filter>beast_core\memory</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_core\diagnostic\Journal.h">
|
||||
<Filter>beast_core\diagnostic</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\modules\beast_core\containers\beast_AbstractFifo.cpp">
|
||||
@@ -1546,6 +1549,9 @@
|
||||
<ClCompile Include="..\..\modules\beast_core\diagnostic\Assert.cpp">
|
||||
<Filter>beast_core\diagnostic</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_core\diagnostic\Journal.cpp">
|
||||
<Filter>beast_core\diagnostic</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="..\..\TODO.txt" />
|
||||
|
||||
@@ -156,6 +156,7 @@ namespace beast
|
||||
#include "diagnostic/beast_SemanticVersion.cpp"
|
||||
#include "diagnostic/beast_UnitTest.cpp"
|
||||
#include "diagnostic/beast_UnitTestUtilities.cpp"
|
||||
#include "diagnostic/Journal.cpp"
|
||||
|
||||
#include "files/beast_DirectoryIterator.cpp"
|
||||
#include "files/beast_File.cpp"
|
||||
|
||||
@@ -281,6 +281,7 @@ extern BEAST_API void BEAST_CALLTYPE logAssertion (char const* file, int line) n
|
||||
#include "text/beast_String.h"
|
||||
#include "time/AtExitHook.h"
|
||||
#include "diagnostic/beast_LeakChecked.h"
|
||||
#include "diagnostic/Journal.h"
|
||||
#include "time/beast_RelativeTime.h"
|
||||
#include "time/beast_Time.h"
|
||||
#include "memory/beast_HeapBlock.h"
|
||||
|
||||
38
modules/beast_core/diagnostic/Journal.cpp
Normal file
38
modules/beast_core/diagnostic/Journal.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
Journal::Sink* Journal::getNullSink ()
|
||||
{
|
||||
// A Sink that does nothing.
|
||||
class NullSink : public Sink
|
||||
{
|
||||
public:
|
||||
void write (Severity, std::string const&)
|
||||
{
|
||||
}
|
||||
|
||||
bool active (Severity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
return SharedSingleton <NullSink>::get (
|
||||
SingletonLifetime::neverDestroyed);
|
||||
}
|
||||
188
modules/beast_core/diagnostic/Journal.h
Normal file
188
modules/beast_core/diagnostic/Journal.h
Normal file
@@ -0,0 +1,188 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_CORE_JOURNAL_H_INCLUDED
|
||||
#define BEAST_CORE_JOURNAL_H_INCLUDED
|
||||
|
||||
/** A generic endpoint for log messages. */
|
||||
class Journal
|
||||
{
|
||||
public:
|
||||
/** Severity level of the message. */
|
||||
enum Severity
|
||||
{
|
||||
kTrace,
|
||||
kDebug,
|
||||
kInfo,
|
||||
kWarning,
|
||||
kError,
|
||||
kFatal
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Abstraction for the underlying message destination. */
|
||||
class Sink
|
||||
{
|
||||
public:
|
||||
virtual ~Sink () { }
|
||||
|
||||
/** Write text to the sink at the specified severity. */
|
||||
virtual void write (Severity severity, std::string const& text) = 0;
|
||||
|
||||
/** Returns `true` if text at the passed severity produces output. */
|
||||
virtual bool active (Severity)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** Returns a Sink which does nothing. */
|
||||
static Sink* getNullSink ();
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Scoped container for building journal messages. */
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
explicit Stream (Sink* sink, Severity severity)
|
||||
: m_sink (sink)
|
||||
, m_severity (severity)
|
||||
{
|
||||
}
|
||||
|
||||
Stream (Stream const& other)
|
||||
: m_sink (other.m_sink)
|
||||
, m_severity (other.m_severity)
|
||||
{
|
||||
}
|
||||
|
||||
Stream& operator= (Stream const& other)
|
||||
{
|
||||
m_sink = other.m_sink;
|
||||
m_severity = other.m_severity;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Stream ()
|
||||
{
|
||||
if (m_sink ->active (m_severity))
|
||||
m_sink->write (m_severity, m_stream.str ());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<< (T const& t) const
|
||||
{
|
||||
return m_stream << t;
|
||||
}
|
||||
|
||||
std::ostringstream& stream () const
|
||||
{
|
||||
return m_stream;
|
||||
}
|
||||
|
||||
private:
|
||||
Sink* m_sink;
|
||||
Severity m_severity;
|
||||
std::ostringstream mutable m_stream;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
Journal (Sink* sink = getNullSink ())
|
||||
: m_sink (sink)
|
||||
{
|
||||
}
|
||||
|
||||
bool reportActive (Severity severity) const
|
||||
{
|
||||
return m_sink->active (severity);
|
||||
}
|
||||
|
||||
bool traceActive () const
|
||||
{
|
||||
return reportActive (kTrace);
|
||||
}
|
||||
|
||||
bool debugActive () const
|
||||
{
|
||||
return reportActive (kDebug);
|
||||
}
|
||||
|
||||
bool infoActive () const
|
||||
{
|
||||
return reportActive (kInfo);
|
||||
}
|
||||
|
||||
bool warningActive () const
|
||||
{
|
||||
return reportActive (kWarning);
|
||||
}
|
||||
|
||||
bool errorActive () const
|
||||
{
|
||||
return reportActive (kError);
|
||||
}
|
||||
|
||||
bool fatalActive () const
|
||||
{
|
||||
return reportActive (kFatal);
|
||||
}
|
||||
|
||||
Stream report (Severity severity) const
|
||||
{
|
||||
return Stream (m_sink, severity);
|
||||
}
|
||||
|
||||
Stream trace () const
|
||||
{
|
||||
return report (kTrace);
|
||||
}
|
||||
|
||||
Stream debug () const
|
||||
{
|
||||
return report (kDebug);
|
||||
}
|
||||
|
||||
Stream info () const
|
||||
{
|
||||
return report (kInfo);
|
||||
}
|
||||
|
||||
Stream warning () const
|
||||
{
|
||||
return report (kWarning);
|
||||
}
|
||||
|
||||
Stream error () const
|
||||
{
|
||||
return report (kError);
|
||||
}
|
||||
|
||||
Stream fatal () const
|
||||
{
|
||||
return Stream (m_sink, kFatal);
|
||||
}
|
||||
|
||||
private:
|
||||
Sink* m_sink;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -75,7 +75,7 @@ class SharedSingleton
|
||||
public:
|
||||
typedef SharedPtr <SharedSingleton <Object> > Ptr;
|
||||
|
||||
static Ptr getInstance (SingletonLifetime::Lifetime lifetime
|
||||
static Ptr get (SingletonLifetime::Lifetime lifetime
|
||||
= SingletonLifetime::persistAfterCreation)
|
||||
{
|
||||
StaticData& staticData (getStaticData ());
|
||||
@@ -95,12 +95,20 @@ public:
|
||||
return instance;
|
||||
}
|
||||
|
||||
// DEPRECATED LEGACY FUNCTION NAME
|
||||
static Ptr getInstance (SingletonLifetime::Lifetime lifetime
|
||||
= SingletonLifetime::persistAfterCreation)
|
||||
{
|
||||
return get (lifetime);
|
||||
}
|
||||
|
||||
private:
|
||||
explicit SharedSingleton (SingletonLifetime::Lifetime lifetime)
|
||||
: m_lifetime (lifetime)
|
||||
, m_exitHook (this)
|
||||
{
|
||||
if (m_lifetime == SingletonLifetime::persistAfterCreation)
|
||||
if (m_lifetime == SingletonLifetime::persistAfterCreation ||
|
||||
m_lifetime == SingletonLifetime::neverDestroyed)
|
||||
this->incReferenceCount ();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user