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

f8_thread delegated async logging class More...

#include <logger.hpp>

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

Classes

struct  LogElement
 

Public Types

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 >
 

Public Member Functions

 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...
 
virtual bool rotate (bool force=false)
 
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 ()
 

Static Public Member Functions

static const Tickvalget_time_started ()
 

Static Public Attributes

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

using ThreadCodes = std::map< thread_id_t, char >
 
using RevThreadCodes = std::map< char, thread_id_t >
 

Protected Attributes

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
 

Static Protected Attributes

static const Tickval _started { true }
 The time the entire logging system was start. More...
 

Private Attributes

f8_thread< Logger_thread
 
std::list< std::string > _buffer
 

Detailed Description

f8_thread delegated async logging class

Definition at line 153 of file logger.hpp.

Member Typedef Documentation

Definition at line 175 of file logger.hpp.

Definition at line 174 of file logger.hpp.

using FIX8::Logger::LogPositions = std::vector<int>

Definition at line 176 of file logger.hpp.

using FIX8::Logger::RevThreadCodes = std::map<char, thread_id_t>
protected

Definition at line 225 of file logger.hpp.

using FIX8::Logger::ThreadCodes = std::map<thread_id_t, char>
protected

Definition at line 222 of file logger.hpp.

Member Enumeration Documentation

Enumerator
Debug 
Info 
Warn 
Error 
Fatal 

Definition at line 159 of file logger.hpp.

Constructor & Destructor Documentation

FIX8::Logger::Logger ( const LogFlags  flags,
const Levels  levels = Levels(All),
const std::string  delim = " ",
const LogPositions  positions = LogPositions() 
)
inline

Ctor.

Parameters
flagsebitset flags
levelsebitset levels
delimfield delimiter
positionsfield positions

Definition at line 239 of file logger.hpp.

References mstart, num_flags, and FIX8::f8_thread< T >::start().

240  : _thread(std::ref(*this)), _flags(flags), _levels(levels), _delim(delim), _positions(positions)
241  {
242  if (_positions.empty()) // setup default order
243  {
244  for (auto ii(mstart); ii != num_flags; ii = Flags(ii + 1))
245  if (_flags & ii)
246  _positions.push_back(ii);
247  }
248 
249  _thread.start();
250  }
f8_thread< Logger > _thread
Definition: logger.hpp:155
LogPositions _positions
Definition: logger.hpp:231
Levels _levels
Definition: logger.hpp:182
std::string _delim
Definition: logger.hpp:183
LogFlags _flags
Definition: logger.hpp:181
virtual FIX8::Logger::~Logger ( )
inlinevirtual

Dtor.

Definition at line 253 of file logger.hpp.

References _ofs, and stop().

254  {
255  stop();
256  delete _ofs;
257  }
void stop()
Stop the logging thread.
Definition: logger.hpp:310
std::ostream * _ofs
Definition: logger.hpp:184

Member Function Documentation

f8_thread_cancellation_token& FIX8::Logger::cancellation_token ( )
inline

Get the thread cancellation token

Returns
reference to the cancellation token

Definition at line 352 of file logger.hpp.

References _stopping.

352 { return _stopping; }
f8_thread_cancellation_token _stopping
Definition: logger.hpp:186
bool FIX8::Logger::enqueue ( const std::string &  what,
Level  lev = Logger::Info,
const char *  fl = nullptr,
const unsigned  val = 0 
)
inline

Log a string with log level. Ignore level and always enqueue.

Parameters
whatthe string to log
levlog level (enum)
flpointer to fileline
valoptional value for the logger to use
Returns
true on success

Definition at line 294 of file logger.hpp.

Referenced by FIX8::SingleLogger< fn >::enqueue(), FIX8::Session::enqueue(), send(), and stop().

295  {
296  const LogElement le(f8_thread<Logger>::getid(), what, lev, fl, val);
297  return _msg_queue.try_push (le) == 0;
298  }
f8_concurrent_queue< LogElement > _msg_queue
Definition: logger.hpp:219
void Logger::flush ( )
virtual

Flush the buffer.

Definition at line 196 of file logger.cpp.

Referenced by FIX8::SingleLogger< fn >::flush_log().

197 {
198  f8_scoped_lock guard(_mutex);
199  for (const auto& pp : _buffer)
200  get_stream() << pp << endl;
201  _buffer.clear();
202  _lines = 0;
203 }
f8_mutex _mutex
Definition: logger.hpp:179
std::list< std::string > _buffer
Definition: logger.hpp:156
virtual std::ostream & get_stream() const
Definition: logger.hpp:286
size_t _lines
Definition: logger.hpp:185
virtual std::ostream& FIX8::Logger::get_stream ( ) const
inlinevirtual

Get the underlying stream object.

Returns
the stream

Definition at line 286 of file logger.hpp.

Referenced by FIX8::XmlFileLogger::postamble(), FIX8::XmlFileLogger::preamble(), and FIX8::XmlFileLogger::process_logline().

286 { return _ofs ? *_ofs : std::cout; }
std::ostream * _ofs
Definition: logger.hpp:184
char Logger::get_thread_code ( thread_id_t  tid)

Get the thread code for this thread or allocate a new code if not found.

Parameters
tidthe thread id of the thread to get a code for

Definition at line 232 of file logger.cpp.

Referenced by FIX8::XmlFileLogger::process_logline().

233 {
234  f8_scoped_lock guard(_mutex);
235 
236  ThreadCodes::const_iterator itr(_thread_codes.find(tid));
237  if (itr != _thread_codes.end())
238  return itr->second;
239 
240  for (char acode('A'); acode < 127; ++acode) // A-~ will allow for 86 threads
241  {
242  RevThreadCodes::const_iterator itr(_rev_thread_codes.find(acode));
243  if (itr == _rev_thread_codes.end())
244  {
245  _thread_codes.insert({tid, acode});
246  _rev_thread_codes.insert({acode, tid});
247  return acode;
248  }
249  }
250 
251  return '?';
252 }
f8_mutex _mutex
Definition: logger.hpp:179
RevThreadCodes _rev_thread_codes
Definition: logger.hpp:226
ThreadCodes _thread_codes
Definition: logger.hpp:223
static const Tickval& FIX8::Logger::get_time_started ( )
inlinestatic

Get te time the logging syste started (actual system startup)

Returns
Tickval of start time

Definition at line 333 of file logger.hpp.

References _started.

333 { return _started; }
static const Tickval _started
The time the entire logging system was start.
Definition: logger.hpp:229
bool FIX8::Logger::has_flag ( const Flags  flg) const
inline

Check if the given log flag is set

Parameters
flgflag bit to check
Returns
true if set

Definition at line 338 of file logger.hpp.

References FIX8::ebitset< T, B >::has().

Referenced by FIX8::Session::process(), and FIX8::Session::send_process().

338 { return _flags.has(flg); }
integral_type has(const T sbit) const
Definition: f8utils.hpp:876
LogFlags _flags
Definition: logger.hpp:181
bool FIX8::Logger::is_loggable ( Level  level) const
inline

Check if the given log level is set for this logger

Parameters
levellevel to test
Returns
true if available

Definition at line 262 of file logger.hpp.

References level.

Referenced by FIX8::SingleLogger< fn >::is_loggable(), FIX8::Session::is_loggable(), and send().

262 { return _levels & level; }
Levels _levels
Definition: logger.hpp:182
int Logger::operator() ( )

The logging thread entry point.

Returns
0 on success

Definition at line 60 of file logger.cpp.

References FIX8::Logger::LogElement::_str, and FIX8::hypersleep< h_microseconds >().

61 {
62  unsigned received(0);
63 
64  while (!_stopping)
65  {
66  LogElement *msg_ptr(0);
67 
68 #if (FIX8_MPMC_SYSTEM == FIX8_MPMC_FF)
69  if (!_msg_queue.try_pop(msg_ptr))
70  {
72  continue;
73  }
74 #else
75  LogElement msg;
76  if (_stopping) // make sure we dequeue any pending msgs before exiting
77  {
78  if (!_msg_queue.try_pop(msg))
79  break;
80  }
81  else
82  _msg_queue.pop (msg); // will block
83  msg_ptr = &msg;
84 #endif
85 
86  ++received;
87 
88  if (msg_ptr)
89  {
90  if (msg_ptr->_str.empty()) // means exit
91  {
92 #if (FIX8_MPMC_SYSTEM == FIX8_MPMC_FF)
93  break;
94 #else
95  continue;
96 #endif
97  }
98 
99  process_logline(msg_ptr);
100  }
101 #if (FIX8_MPMC_SYSTEM == FIX8_MPMC_FF)
102  _msg_queue.release(msg_ptr);
103 #endif
104  }
105 
106  return 0;
107 }
int hypersleep< h_microseconds >(unsigned amt)
Definition: hypersleep.hpp:127
virtual F8API void process_logline(LogElement *le)
Definition: logger.cpp:110
f8_concurrent_queue< LogElement > _msg_queue
Definition: logger.hpp:219
f8_thread_cancellation_token _stopping
Definition: logger.hpp:186
void Logger::process_logline ( LogElement le)
virtual

Process this logelement

Parameters
leLogElement

Reimplemented in FIX8::XmlFileLogger.

Definition at line 110 of file logger.cpp.

References FIX8::Logger::LogElement::_fileline, FIX8::Logger::LogElement::_level, FIX8::Logger::LogElement::_str, FIX8::Logger::LogElement::_tid, FIX8::Logger::LogElement::_val, FIX8::Logger::LogElement::_when, FIX8::GetTimeAsStringMini(), FIX8::Tickval::msecs(), FIX8::f8_scoped_lock_impl< T >::release(), and FIX8::Tickval::secs().

111 {
112  ostringstream ostr;
113  f8_scoped_spin_lock posguard(_log_spl); // protect positions, allow app to change
114  for (auto pp : _positions)
115  {
116  ostringstream fostr;
117  if (pp < start_controls) switch (pp)
118  {
119  case mstart:
120  {
121  const Tickval tvs(msg_ptr->_when - _started);
122  fostr << setw(11) << right << setfill('0') << (tvs.secs() * 1000 + tvs.msecs());
123  }
124  break;
125  case sstart:
126  fostr << setw(8) << right << setfill('0') << (msg_ptr->_when - _started).secs();
127  break;
128  case sequence:
129  fostr << setw(7) << right << setfill('0');
130  if (_flags & direction)
131  fostr << (msg_ptr->_val ? ++_sequence : ++_osequence);
132  else
133  fostr << ++_sequence;
134  break;
135  case thread:
136  fostr << get_thread_code(msg_ptr->_tid);
137  break;
138  case location:
139  if (msg_ptr->_fileline)
140  fostr << msg_ptr->_fileline;
141  break;
142  case direction:
143  fostr << (msg_ptr->_val ? " in" : "out");
144  break;
145  case timestamp:
146  fostr << msg_ptr->_when;
147  break;
148  case minitimestamp:
149  fostr << GetTimeAsStringMini(&msg_ptr->_when);
150  break;
151  case level:
152  fostr << _level_names[msg_ptr->_level];
153  break;
154  default:
155  break;
156  }
157 
158  if (!fostr.str().empty())
159  {
160  if (_delim.size() > 1)
161  ostr << _delim[0] << fostr.str() << _delim[1];
162  else
163  ostr << fostr.str() << _delim;
164  }
165  }
166  posguard.release();
167 
168  if (_flags & buffer)
169  {
170  string result(ostr.str());
171  if (_delim.size() > 1)
172  {
173  result += _delim[0];
174  result += msg_ptr->_str;
175  result += _delim[1];
176  }
177  else
178  result += msg_ptr->_str;
179  _buffer.push_back(result);
180  }
181  else
182  {
183  f8_scoped_lock guard(_mutex);
184  if (_delim.size() > 1)
185  get_stream() << ostr.str() << _delim[0] << msg_ptr->_str << _delim[1];
186  else
187  get_stream() << ostr.str() << msg_ptr->_str;
188  if (_flags & nolf)
189  get_stream().flush();
190  else
191  get_stream() << endl;
192  }
193 }
f8_mutex _mutex
Definition: logger.hpp:179
unsigned _osequence
Definition: logger.hpp:220
std::list< std::string > _buffer
Definition: logger.hpp:156
F8API const std::string & GetTimeAsStringMini(std::string &result, const Tickval *tv)
static const Tickval _started
The time the entire logging system was start.
Definition: logger.hpp:229
F8API char get_thread_code(thread_id_t tid)
Definition: logger.cpp:232
static const std::vector< std::string > _level_names
string representation of levels
Definition: logger.hpp:329
unsigned _sequence
Definition: logger.hpp:220
virtual std::ostream & get_stream() const
Definition: logger.hpp:286
LogPositions _positions
Definition: logger.hpp:231
f8_spin_lock _log_spl
Definition: logger.hpp:180
std::string _delim
Definition: logger.hpp:183
LogFlags _flags
Definition: logger.hpp:181
void Logger::purge_thread_codes ( )

Remove dead threads from the thread code cache.

Definition at line 206 of file logger.cpp.

Referenced by FIX8::Session::start().

207 {
208 #if (FIX8_THREAD_SYSTEM == FIX8_THREAD_PTHREAD)
209  f8_scoped_lock guard(_mutex);
210 
211  for (ThreadCodes::iterator itr(_thread_codes.begin()); itr != _thread_codes.end();)
212  {
213 #if defined _MSC_VER || defined __APPLE__
214  // If ESRCH then thread is definitely dead. Otherwise, we're unsure.
215  if (pthread_kill(itr->first, 0) == ESRCH)
216 #else
217  // a little trick to see if a thread is still alive
218  clockid_t clock_id;
219  if (pthread_getcpuclockid(itr->first, &clock_id) == ESRCH)
220 #endif
221  {
222  _rev_thread_codes.erase(itr->second);
223  _thread_codes.erase(itr++); // post inc, takes copy before incr;
224  }
225  else
226  ++itr;
227  }
228 #endif
229 }
f8_mutex _mutex
Definition: logger.hpp:179
RevThreadCodes _rev_thread_codes
Definition: logger.hpp:226
ThreadCodes _thread_codes
Definition: logger.hpp:223
virtual bool FIX8::Logger::rotate ( bool  force = false)
inlinevirtual

Perform logfile rotation. Only relevant for file-type loggers.

Parameters
forcethe rotation (even if the file is set to append)
Returns
true on success

Reimplemented in FIX8::FileLogger.

Definition at line 315 of file logger.hpp.

315 { return true; }
bool FIX8::Logger::send ( const std::string &  what,
Level  lev = Logger::Info,
const char *  fl = nullptr,
const unsigned  val = 0 
)
inline

Log a string with log level.

Parameters
whatthe string to log
levlog level (enum)
flpointer to fileline
valoptional value for the logger to use
Returns
true on success

Definition at line 306 of file logger.hpp.

References enqueue(), and is_loggable().

Referenced by FIX8::SingleLogger< fn >::log(), FIX8::Session::log(), and FIX8::Session::plog().

307  { return is_loggable(lev) ? enqueue(what, lev, fl, val) : true; }
bool enqueue(const std::string &what, Level lev=Logger::Info, const char *fl=nullptr, const unsigned val=0)
Definition: logger.hpp:294
bool is_loggable(Level level) const
Definition: logger.hpp:262
void FIX8::Logger::set_delimiter ( const std::string &  delim)
inline

Set the field delimiter

Parameters
delimdelimiter value to set

Definition at line 274 of file logger.hpp.

Referenced by FIX8::SingleLogger< fn >::set_delimiter().

274 { _delim = delim; }
std::string _delim
Definition: logger.hpp:183
void FIX8::Logger::set_flags ( LogFlags  flags)
inline

Set the LogFlags

Parameters
flagsflags to set

Definition at line 270 of file logger.hpp.

Referenced by FIX8::SingleLogger< fn >::set_flags().

270 { _flags = flags; }
LogFlags _flags
Definition: logger.hpp:181
void FIX8::Logger::set_levels ( Levels  levels)
inline

Set the Log Levels

Parameters
levelslevels to set

Definition at line 266 of file logger.hpp.

Referenced by FIX8::SingleLogger< fn >::set_levels().

266 { _levels = levels; }
Levels _levels
Definition: logger.hpp:182
void FIX8::Logger::set_positions ( const std::vector< int > &  positions)
inline

Set the log attribute positions

Parameters
positionspositions to set

Definition at line 278 of file logger.hpp.

Referenced by FIX8::SingleLogger< fn >::set_positions().

279  {
281  _positions = positions;
282  }
f8_scoped_lock_impl< f8_spin_lock > f8_scoped_spin_lock
Definition: thread.hpp:452
LogPositions _positions
Definition: logger.hpp:231
f8_spin_lock _log_spl
Definition: logger.hpp:180
void FIX8::Logger::stop ( )
inline

Stop the logging thread.

Definition at line 310 of file logger.hpp.

References enqueue(), FIX8::_f8_threadcore::join(), and FIX8::f8_thread_cancellation_token::request_stop().

Referenced by FIX8::SingleLogger< fn >::stop(), ~Logger(), and FIX8::Session::~Session().

310 { _stopping.request_stop(); enqueue(std::string()); _thread.join(); }
f8_thread< Logger > _thread
Definition: logger.hpp:155
bool enqueue(const std::string &what, Level lev=Logger::Info, const char *fl=nullptr, const unsigned val=0)
Definition: logger.hpp:294
f8_thread_cancellation_token _stopping
Definition: logger.hpp:186
void request_stop()
Tell the thread to stop.
Definition: thread.hpp:219

Member Data Documentation

const vector< string > FIX8::Logger::_bit_names
static
Initial value:
{
"mstart", "sstart", "sequence", "thread", "timestamp", "minitimestamp", "direction", "level", "location",
"append", "buffer", "compress", "pipe", "broadcast", "nolf", "inbound", "outbound", "xml"
}

string representation of logflags

Definition at line 326 of file logger.hpp.

Referenced by FIX8::Configuration::create_logger().

std::list<std::string> FIX8::Logger::_buffer
private

Definition at line 156 of file logger.hpp.

std::string FIX8::Logger::_delim
protected

Definition at line 183 of file logger.hpp.

const vector< string > FIX8::Logger::_level_names { "Debug", "Info ", "Warn ", "Error", "Fatal" }
static

string representation of levels

Definition at line 329 of file logger.hpp.

Referenced by FIX8::Configuration::create_logger(), and FIX8::XmlFileLogger::process_logline().

Levels FIX8::Logger::_levels
protected

Definition at line 182 of file logger.hpp.

size_t FIX8::Logger::_lines = 0
protected

Definition at line 185 of file logger.hpp.

f8_spin_lock FIX8::Logger::_log_spl
protected

Definition at line 180 of file logger.hpp.

f8_concurrent_queue<LogElement> FIX8::Logger::_msg_queue
protected

Definition at line 219 of file logger.hpp.

f8_mutex FIX8::Logger::_mutex
protected

Definition at line 179 of file logger.hpp.

Referenced by FIX8::XmlFileLogger::process_logline(), and FIX8::FileLogger::rotate().

std::ostream* FIX8::Logger::_ofs = nullptr
protected
unsigned FIX8::Logger::_osequence = 0
protected

Definition at line 220 of file logger.hpp.

Referenced by FIX8::XmlFileLogger::process_logline().

LogPositions FIX8::Logger::_positions
protected

Definition at line 231 of file logger.hpp.

RevThreadCodes FIX8::Logger::_rev_thread_codes
protected

Definition at line 226 of file logger.hpp.

unsigned FIX8::Logger::_sequence = 0
protected

Definition at line 220 of file logger.hpp.

Referenced by FIX8::XmlFileLogger::process_logline().

const Tickval FIX8::Logger::_started { true }
staticprotected

The time the entire logging system was start.

Definition at line 229 of file logger.hpp.

Referenced by get_time_started(), and FIX8::XmlFileLogger::process_logline().

f8_thread_cancellation_token FIX8::Logger::_stopping
protected

Definition at line 186 of file logger.hpp.

Referenced by cancellation_token().

f8_thread<Logger> FIX8::Logger::_thread
private

Definition at line 155 of file logger.hpp.

ThreadCodes FIX8::Logger::_thread_codes
protected

Definition at line 223 of file logger.hpp.

const int FIX8::Logger::All = bitsum(Debug,Info,Warn,Error,Fatal)
static
const int FIX8::Logger::Errors = bitsum(Warn,Error,Fatal)
static

Definition at line 161 of file logger.hpp.

const int FIX8::Logger::max_rotation = 1024
static

Definition at line 173 of file logger.hpp.

Referenced by FIX8::FilePersister::initialise(), and FIX8::FileLogger::rotate().

const int FIX8::Logger::None = 0
static

Definition at line 165 of file logger.hpp.

Referenced by FIX8::Configuration::create_logger().

const int FIX8::Logger::rotation_default = 5
static

Definition at line 173 of file logger.hpp.

const int FIX8::Logger::StdFlags = bitsum(sequence,thread,timestamp,level)
static

Definition at line 169 of file logger.hpp.

Referenced by FIX8::Configuration::create_logger().


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