ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfilelogger.cpp
1// #################################################################################################
2// alib::lox::loggers - ALox Logging Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
15#ifndef H_ALIB_ALOX
18#endif
19#include <fstream>
20// =========================================== Module ==========================================
21#if ALIB_C20_MODULES
22 module ALib.ALox.Impl;
23 import ALib.Lang;
24 import ALib.Strings;
25#else
26# include "ALib.Lang.H"
27# include "ALib.Strings.H"
28# include "ALib.ALox.H"
29# include "ALib.ALox.Impl.H"
30#endif
31// ====================================== Implementation =======================================
32using namespace alib;
33
35 const alib::NString& loggerName )
36: PlainTextLogger( loggerName, "TEXTFILE", false )
37{
38 FileName << fileName;
39
40 // test open file for test
41 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
42 auto* os= new std::ofstream( nFileName, std::ios::app );
43 if ( !os->is_open() )
44 {
46 ALIB_WARNING( "ALOX", "Could not open file: \"{}\". System error code: {}",
48 }
49 else
50 // test run OK
51 os->close();
52
53 delete os;
54}
55
57{
58 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
59 auto* os= new std::ofstream( nFileName, std::ios::app );
60 if ( !os->is_open() )
61 {
62 writer.SetStream( nullptr );
64 ALIB_WARNING( "ALOX", "Could not open file: \"{}\". System error code: {}",
66 delete os; os= nullptr;
67 return;
68 }
69 else
70 LastSystemError= SystemErrors::None;
71
72 writer.SetStream( os );
73}
74
76{
77 auto* stream= writer.GetStream();
78 if( stream == nullptr )
79 return;
80
81 auto* os= dynamic_cast<std::ofstream*>(stream);
82 os->close();
83 delete os;
84 writer.SetStream( nullptr );
85}
86
87
89{
90 // save state (to have it in logText)
92
93 // open/close the file
94 if ( phase == lang::Phase::Begin )
95 openFile();
96 else if( writer.GetStream() != nullptr )
97 closeFile();
98}
99
101{
102 auto* stream= writer.GetStream();
103 if ( stream != nullptr && phase == lang::Phase::End )
104 *stream << std::endl;
105
106 // open/close
108 {
109 if ( phase == lang::Phase::Begin )
110 openFile();
111 else
112 closeFile();
113 }
114
115 return LastSystemError == SystemErrors::None;
116}
117
119{
120 if (writer.GetStream() != nullptr && LastSystemError == SystemErrors::None)
121 return writer.WriteAndGetWideLength( buffer.Substring<NC>( start, length ) );
122 return 0;
123}
124
virtual ALIB_DLL void notifyMultiLineOp(lang::Phase phase) override
virtual ALIB_DLL bool notifyLogOp(lang::Phase phase) override
ALIB_DLL void openFile()
Opens the file.
bool currentlyInMultiLineOp
Flag to prevent file open/close operations when multi line text logging is performed.
ALIB_DLL void closeFile()
Closes the file.
virtual ALIB_DLL integer logSubstring(const String &buffer, integer start, integer length) override
StringWriter writer
Encapsulates the text file stream in a system dependent way.
ALIB_DLL TextFileLogger(const alib::String &fileName, const alib::NString &loggerName=nullptr)
alib::AString FileName
The path and fileName to the log file.
PlainTextLogger(const NString &name, const NString &typeName, bool pUsesStdStreams)
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:386
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1046
Phase
Denotes a phase, e.g.,of a transaction.
@ Begin
The start of a transaction.
@ End
The end of a transaction.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2390
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
system::SystemErrors SystemErrors
Type alias in namespace alib.