fix8  version 1.4.0
Open Source C++ FIX Framework
FIX8::FileLogger Class Reference

A file logger. More...

#include <logger.hpp>

Inheritance diagram for FIX8::FileLogger:
FIX8::Logger FIX8::XmlFileLogger

Public Member Functions

F8API FileLogger (const std::string &pathname, const LogFlags flags, const Levels levels, const std::string delim=" ", const LogPositions positions=LogPositions(), const unsigned rotnum=rotation_default)
 
virtual ~FileLogger ()
 Dtor. More...
 
virtual F8API bool rotate (bool force=false)
 
- Public Member Functions inherited from FIX8::Logger
 Logger (const LogFlags flags, const Levels levels=Levels(All), const std::string delim=" ", const LogPositions positions=LogPositions())
 
virtual ~Logger ()
 Dtor. More...
 
bool is_loggable (Level level) const
 
void set_levels (Levels levels)
 
void set_flags (LogFlags flags)
 
void set_delimiter (const std::string &delim)
 
void set_positions (const std::vector< int > &positions)
 
virtual std::ostream & get_stream () const
 
bool enqueue (const std::string &what, Level lev=Logger::Info, const char *fl=nullptr, const unsigned val=0)
 
bool send (const std::string &what, Level lev=Logger::Info, const char *fl=nullptr, const unsigned val=0)
 
void stop ()
 Stop the logging thread. More...
 
F8API int operator() ()
 
virtual F8API void process_logline (LogElement *le)
 
bool has_flag (const Flags flg) const
 
F8API char get_thread_code (thread_id_t tid)
 
F8API void purge_thread_codes ()
 Remove dead threads from the thread code cache. More...
 
virtual F8API void flush ()
 Flush the buffer. More...
 
f8_thread_cancellation_tokencancellation_token ()
 

Protected Attributes

std::string _pathname
 
unsigned _rotnum
 
- Protected Attributes inherited from FIX8::Logger
f8_mutex _mutex
 
f8_spin_lock _log_spl
 
LogFlags _flags
 
Levels _levels
 
std::string _delim
 
std::ostream * _ofs = nullptr
 
size_t _lines = 0
 
f8_thread_cancellation_token _stopping
 
f8_concurrent_queue< LogElement_msg_queue
 
unsigned _sequence = 0
 
unsigned _osequence = 0
 
ThreadCodes _thread_codes
 
RevThreadCodes _rev_thread_codes
 
LogPositions _positions
 

Additional Inherited Members

- Public Types inherited from FIX8::Logger
enum  Level {
  Debug, Info, Warn, Error,
  Fatal
}
 
enum  Flags {
  mstart, sstart, sequence, thread,
  timestamp, minitimestamp, direction, level,
  location, start_controls, append =start_controls, buffer,
  compress, pipe, broadcast, nolf,
  inbound, outbound, xml, num_flags
}
 
using LogFlags = ebitset< Flags >
 
using Levels = ebitset< Level >
 
using LogPositions = std::vector< int >
 
- Static Public Member Functions inherited from FIX8::Logger
static const Tickvalget_time_started ()
 
- Static Public Attributes inherited from FIX8::Logger
static const int Errors = bitsum(Warn,Error,Fatal)
 
static const int All = bitsum(Debug,Info,Warn,Error,Fatal)
 
static const int None = 0
 
static const int StdFlags = bitsum(sequence,thread,timestamp,level)
 
static const int rotation_default = 5
 
static const int max_rotation = 1024
 
static const std::vector< std::string > _bit_names
 string representation of logflags More...
 
static const std::vector< std::string > _level_names { "Debug", "Info ", "Warn ", "Error", "Fatal" }
 string representation of levels More...
 
- Protected Types inherited from FIX8::Logger
using ThreadCodes = std::map< thread_id_t, char >
 
using RevThreadCodes = std::map< char, thread_id_t >
 
- Static Protected Attributes inherited from FIX8::Logger
static const Tickval _started { true }
 The time the entire logging system was start. More...
 

Detailed Description

A file logger.

Definition at line 357 of file logger.hpp.

Constructor & Destructor Documentation

FileLogger::FileLogger ( const std::string &  pathname,
const LogFlags  flags,
const Levels  levels,
const std::string  delim = " ",
const LogPositions  positions = LogPositions(),
const unsigned  rotnum = rotation_default 
)

Ctor.

Parameters
pathnamepathname to log to
flagsebitset flags
levelsebitset levels
delimfield delimiter
positionsfield positions
rotnumnumber of logfile rotations to retain (default=5)

Definition at line 255 of file logger.cpp.

References _pathname, and rotate().

257  : Logger(flags, levels, delim, positions), _rotnum(rotnum)
258 {
259  if (!fname.empty())
260  {
261  _pathname = fname;
262  rotate();
263  }
264 }
Logger(const LogFlags flags, const Levels levels=Levels(All), const std::string delim=" ", const LogPositions positions=LogPositions())
Definition: logger.hpp:239
virtual F8API bool rotate(bool force=false)
Definition: logger.cpp:267
unsigned _rotnum
Definition: logger.hpp:361
std::string _pathname
Definition: logger.hpp:360
virtual FIX8::FileLogger::~FileLogger ( )
inlinevirtual

Dtor.

Definition at line 375 of file logger.hpp.

375 {}

Member Function Documentation

bool FileLogger::rotate ( bool  force = false)
virtual

Perform logfile rotation

Parameters
forceforce the rotation (even if the file is set ti append)
Returns
true on success

Reimplemented from FIX8::Logger.

Definition at line 267 of file logger.cpp.

References FIX8::Logger::_flags, FIX8::Logger::_mutex, FIX8::Logger::_ofs, _pathname, _rotnum, FIX8::Logger::append, FIX8::Logger::compress, FIX8::create_path(), FIX8::exist(), filepart(), FIX8::ebitset< T, B >::has(), FIX8::Logger::max_rotation, and FIX8::split_path().

Referenced by FileLogger().

268 {
269  f8_scoped_lock guard(_mutex);
270 
271  delete _ofs;
272 
273  string thislFile(_pathname), filepart, dirpart;
274  split_path(thislFile, filepart, dirpart);
275  if (!dirpart.empty() && !exist(dirpart))
276  create_path(dirpart);
277 
278 #ifdef HAVE_COMPRESSION
279  if (_flags & compress)
280  thislFile += ".gz";
281 #endif
282  if (_rotnum > 0 && (!_flags.has(append) || force))
283  {
284  vector<string> rlst;
285  rlst.push_back(thislFile);
286 
287  for (unsigned ii(0); ii < _rotnum && ii < max_rotation; ++ii)
288  {
289  ostringstream ostr;
290  ostr << _pathname << '.' << (ii + 1);
291  if (_flags & compress)
292  ostr << ".gz";
293  rlst.push_back(ostr.str());
294  }
295 
296  for (unsigned ii(_rotnum); ii; --ii)
297  rename (rlst[ii - 1].c_str(), rlst[ii].c_str());
298  }
299 
300  const ios_base::openmode mode (_flags & append ? ios_base::out | ios_base::app : ios_base::out);
301 #ifdef HAVE_COMPRESSION
302  if (_flags & compress)
303  _ofs = new ogzstream(thislFile.c_str(), mode);
304  else
305 #endif
306  _ofs = new ofstream(thislFile.c_str(), mode);
307 
308  if (!_ofs || !*_ofs)
309  throw LogfileException(thislFile);
310 
311  return true;
312 }
f8_mutex _mutex
Definition: logger.hpp:179
const string & filepart(const string &source, string &where)
Definition: f8cutils.cpp:108
std::ostream * _ofs
Definition: logger.hpp:184
F8API void create_path(const std::string &path)
static const int max_rotation
Definition: logger.hpp:173
void split_path(const std::string &source, std::string &filepart, std::string &dirpart)
Definition: f8utils.hpp:1082
integral_type has(const T sbit) const
Definition: f8utils.hpp:876
unsigned _rotnum
Definition: logger.hpp:361
LogFlags _flags
Definition: logger.hpp:181
bool exist(const std::string &fname)
Definition: f8utils.hpp:1068
std::string _pathname
Definition: logger.hpp:360
Could not open a logfile.

Member Data Documentation

std::string FIX8::FileLogger::_pathname
protected

Definition at line 360 of file logger.hpp.

Referenced by FileLogger(), and rotate().

unsigned FIX8::FileLogger::_rotnum
protected

Definition at line 361 of file logger.hpp.

Referenced by rotate().


The documentation for this class was generated from the following files: