ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
memorylogger.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_alox of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_LOX_MEMORY_LOGGER
9#define HPP_ALIB_LOX_MEMORY_LOGGER 1
10#pragma once
12
13namespace alib { namespace lox { namespace loggers {
14
15//==================================================================================================
16/// A logger that logs all messages to an in-memory buffer of type AString. The name of the \e Logger
17/// defaults to "MEMORY".
18//==================================================================================================
20{
21 // #############################################################################################
22 // public fields
23 // #############################################################################################
24 public:
25 /// The logging Buffer. This can be accessed publicly and hence used as preferred.
26 /// Especially, the whole log can easily be cleared using
27 /// \alib{strings;TAString::Reset;AString::Reset}.
28 /// In multithreaded environments, \c Lox interface's mutex should be acquired
29 /// before accessing this buffer. The initial size of the buffer is 8kb.
31
32 /// If this field is set to \c true (which is the default), the effective length of the
33 /// messages when converted to wide character strings are taken into account.
34 ///
35 /// Switching this off increases the overall logging performance (especially when logging
36 /// into memory) significantly.
38
39
40 // #############################################################################################
41 // Constructor/destructor
42 // #############################################################################################
43 public:
44 //==========================================================================================
45 /// Creates a MemoryLogger with the given name.
46 /// @param name (Optional) The name of the \e Logger. Defaults to "MEMORY".
47 /// @param pruneESCSequences (Optional) Sets the member \ref PruneESCSequences.
48 /// Defaults to \c true.
49 /// @param useWStringLengthForTabAdjustments (Optional) Sets the member
50 /// \ref UseWStringLengthForTabAdjustments.
51 /// Defaults to \c true.
52 //==========================================================================================
53 explicit MemoryLogger( const NString& name = nullptr,
54 bool pruneESCSequences = true,
55 bool useWStringLengthForTabAdjustments= true )
56 : PlainTextLogger( name, "MEMORY", false )
57 {
58 MemoryLog.SetBuffer( 8092 );
59 PruneESCSequences = pruneESCSequences;
60 UseWStringLengthForTabAdjustments= useWStringLengthForTabAdjustments;
61 }
62
63
64 //==========================================================================================
65 /// Destructs a MemoryLogger
66 //==========================================================================================
67 virtual ~MemoryLogger() override {}
68
69 // #############################################################################################
70 // Abstract method implementations
71 // #############################################################################################
72 protected:
73 //==========================================================================================
74 /// Start a new log line. Appends a new-line character sequence to previously logged lines.
75 ///
76 /// @param phase Indicates the beginning or end of a log line.
77 /// @return Always returns true.
78 //==========================================================================================
79 virtual bool notifyLogOp(lang::Phase phase) override
80 {
81 // append new line if buffer has already lines stored
82 if ( phase == lang::Phase::Begin && MemoryLog.IsNotEmpty() )
84 return true;
85 }
86
87 //==========================================================================================
88 /// Write the given region of the given AString to the destination buffer.
89 ///
90 /// @param buffer The string to write a portion of.
91 /// @param start The start of the portion in \p{buffer} to write out.
92 /// @param length The length of the portion in \p{buffer} to write out.
93 /// @return The number of characters written, -1 on error.
94 //==========================================================================================
95 virtual integer logSubstring( const String& buffer,
96 integer start, integer length ) override
97 {
98 MemoryLog._<NC>( buffer, start, length );
100 ? buffer.Substring<NC>( start, length ).WStringLength()
101 : length;
102 }
103
104 //==========================================================================================
105 /// Empty implementation, not needed for this class
106 //==========================================================================================
107 virtual void notifyMultiLineOp( lang::Phase ) override {}
108
109}; // class MemoryLogger
110
111}} // namespace alib[::lox::loggers]
112
113/// Type alias in namespace \b alib.
115
116} // namespace [alib]
117
118#endif // HPP_ALIB_LOX_MEMORY_LOGGER
119
virtual void notifyMultiLineOp(lang::Phase) override
Empty implementation, not needed for this class.
virtual bool notifyLogOp(lang::Phase phase) override
virtual ~MemoryLogger() override
Destructs a MemoryLogger.
MemoryLogger(const NString &name=nullptr, bool pruneESCSequences=true, bool useWStringLengthForTabAdjustments=true)
virtual integer logSubstring(const String &buffer, integer start, integer length) override
PlainTextLogger(const NString &name, const NString &typeName, bool pUsesStdStreams)
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
ALIB_API void SetBuffer(integer newCapacity)
constexpr bool IsNotEmpty() const
Definition string.hpp:389
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:406
Phase
Denotes a phase, e.g.,of a transaction.
@ Begin
The start of a transaction.
Definition alib.cpp:69
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273