Slog Class Reference

simple logging class with a ostream like (<<) interface More...

#include <slogcxx.h>

List of all members.

Public Member Functions

 Slog (UNUSED const std::string &filename="", const std::string &indentStr=" ", UNUSED const bool append=true, const bool enableXml=true, const bool enableTime=true)
void setLevel (const int lvl)
int getLevel ()
int inc ()
int dec ()
void enableTime ()
void disableTime ()
bool getTimeStatus ()
void enableXml ()
void disableXml ()
bool getXmlStatus ()
bool entry (UNUSED const int lvl, UNUSED const std::string str)
bool where (UNUSED const std::string &file, UNUSED const int lineno, UNUSED const std::string &function)
void setMsgLevel (const int lvl)
int getMsgLevel ()
int incMsg ()
int decMsg ()
bool partial (UNUSED const int lvl, UNUSED const std::string str)
bool complete ()
void setStateIndent (const std::string &str)
std::string getStateIndent ()
std::string indent ()
std::string getStateNumberStr ()
std::string getCurScope ()
void pushState (std::string scope, int msgLvl=-1)
std::string popState ()
void writeState (UNUSED bool flat=true)
int getStateDepth ()
Slogoperator= (UNUSED const Slog &rhs)
 Slog (const std::string &filename="", const std::string &indentStr=" ", const bool append=true, const bool enableXml=true, const bool enableTime=true)
 simple console constructor using cerr
 ~Slog ()
 We must not have a mine shaft gap!
bool entry (const int lvl, const std::string str)
bool where (const std::string &file, const int lineno, const std::string &function)
 Add a tag of where the log is being generated from. Adds to the partial log message.
Slogoperator= (UNUSED const Slog &rhs)
 Prevent copying from happening. How could we handle a copy?
Verbosity
void setLevel (const int lvl)
 This controls the amount of output.
int getLevel ()
 What is the current verbosity level? Higher means more spewage.
int inc ()
 Ask for more pain (err... log messages).
int dec ()
 Stick your head in the sand (ostridge mode)... see fewer log messages.
Time control
void enableTime ()
 turn on time stamping in log entries
void disableTime ()
 turn off time stamping in log entries
bool getTimeStatus ()
 return true if time logging is on
XML control - only applies to log files.
Generally you will want to just leave XML logging on. You can get unbalanced begin and end log tags if the state is different at the log contruction/destruction.

void enableXml ()
 Switch to xml encoding of log messages.
void disableXml ()
 Switch back to text mode.
bool getXmlStatus ()
 Are we in xml output mode?
Controlling log messages in the stream style (<<)
void setMsgLevel (const int lvl)
 This sets the level of the entry.
int getMsgLevel ()
 Return the current log level number.
int incMsg ()
 Increase the log level for all messages following (less likely to be logged).
int decMsg ()
 Decrease the log level for all messages following (more likely to be logged).
Implementation for stream (<<) handling
These two are not really meant to be used in code... just within the slogcxx class

bool partial (const int lvl, const std::string str)
 add to the current log (for <<)
bool complete ()
State stack handling
void setStateIndent (const std::string &str)
 Change the indenting to a different string.
std::string getStateIndent ()
 What is the current indent string.
std::string indent ()
 Return the string with the proper indenting.
std::string getStateNumberStr ()
 Return a 2+ character scope depth.
std::string getCurScope ()
void pushState (std::string scope, int msgLvl=-1)
 Put the current scope onto the state stack.
std::string popState ()
 Back out one level of scope. FIX: what if no scope to pop?
void writeState (bool flat=true)
 Write out the state to the log.
int getStateDepth ()

Private Attributes

int logLevel
 what the programs logging level is. Turn this higher to get more messags
int msgLevel
 For partial messages, this is their default level.
std::string curStr
 building the current message
bool xmlEnabled
 Should the output be to xml?
bool timeEnabled
 If true, then log entries should include a time stamp.
std::string stateIndent
 How much to indent the output for each level.
std::vector< std::string > stateStack
std::vector< int > msgLvlStack
std::vector< std::string > stateStack
 All of the state names in a stack.
std::vector< int > msgLvlStack
 for push and pop state
std::ofstream logFile
 If open then also log to a file.


Detailed Description

simple logging class with a ostream like (<<) interface

Here is the simplest possible example of using slogcxx:

#include <slogcxx.h>

int
main(int argc, char *argv[]) {
  Slog log;
  log << "argc " << argc << endl;
  log << "argv[0] " << argv[0] << endl;
  return (EXIT_SUCCESS);
}

When compiled and run, this code logs only to stderr like this:

 0: started logging
 0: argc 1
 0: argv[0] ./simplest
 0: stopped logging

Here is a slightly more complicated example that logs to a file and uses a few more of the features.

int
main(int argc, char *argv[]) {
  Slog log("sample.log");
  log << "argc " << argc << endl;
  log << "argv[0] " << argv[0] << endl;
  log << "The WHERE object marks a location in the code " << WHERE << endl;
  {
    LogState ls1(&log,"scope name here");
    log << "LogState will pop a log scope when it is destroyed" << endl;
    {
      LogState ls2(&log,"two");
      log << "Here is another log scope" << endl;
    }
  }
  log << "Not all of a log message will show up" << incl <<"VANISHING"<<decl << endl;
  log << "No reason not to add your own XML <mytag>some info</mytag>" << endl;
  return (EXIT_SUCCESS);
}
Running this generates this output to stderr:
g++ foo.C slogcxx.o -I. && ./a.out
Opening log file: 'sample.log'
 0: started logging
 0: argc 1
 0: argv[0] ./a.out
 0: The WHERE object marks a location in the code <where file="foo.C" line="8" function="main" />
 1 scope name here: LogState will pop a log scope when it is destroyed
 2  two: Here is another log scope
 0: Not all of a log message will show up
 0: No reason not to add your own XML <mytag>some info</mytag>
 0: stopped logging
And xml is written to the log file. Note that there is currently a bug with writing the time. Scientific notation is not helpful.
<slogcxx>
<entry time="1.15092e+09">started logging</entry>
<entry time="1.15092e+09">argc 1</entry>
<entry time="1.15092e+09">argv[0] ./a.out</entry>
<entry time="1.15092e+09">The WHERE object marks a location in the code <where file="foo.C" line="8" function="main" /></entry>
<scope name="scope name here">
 <entry time="1.15092e+09" scope="scope name here">LogState will pop a log scope when it is destroyed</entry>
 <scope name="two">
  <entry time="1.15092e+09" scope="two">Here is another log scope</entry>
 </scope> <!-- two -->
</scope> <!-- scope name here -->
<entry time="1.15092e+09">Not all of a log message will show up</entry>
<entry time="1.15092e+09">No reason not to add your own XML <mytag>some info</mytag></entry>
<entry time="1.15092e+09">stopped logging</entry>
</slogcxx>

Todo:
get fractions of a second into the time entry

get people other than Kurt to write a bit of documentation.


Constructor & Destructor Documentation

Slog::Slog const std::string &  filename = "",
const std::string &  indentStr = " ",
const bool  append = true,
const bool  enableXml = true,
const bool  enableTime = true
 

simple console constructor using cerr

Parameters:
filename Also log to a file
append Set to false to wipe out previous loggin with the same file name
enableXml set false to write plain text to log file. Console logging not affected.
enableTime set to false to stop writing time to the log file entries
indentStr Whatever string to indent by for each scope (e.g. " ", "\t" or "...")


Member Function Documentation

bool Slog::complete  ) 
 

Finish up a log entry after partials

Returns:
False if there was no stored message to write to the log

bool Slog::entry const int  lvl,
const std::string  str
 

do one complete log. return true if logged. This is the more traditionalC like interface return false if there was some trouble

bool Slog::partial const int  lvl,
const std::string  str
 

add to the current log (for <<)

FIX: maybe this should be some friend type thing, but me no like friends

Returns:
true if it added anything to the log message

bool Slog::where const std::string &  file,
const int  lineno,
const std::string &  function
 

Add a tag of where the log is being generated from. Adds to the partial log message.

Use the WHERE macro rather than this call directly. Will write out an xml tag if in xml mode.

Parameters:
file filename string for what file the call is currently in
lineno The current line number within the file
function The current function that execution is occuring in.

void Slog::writeState bool  flat = true  ) 
 

Write out the state to the log.

Parameters:
flat If flat is false, then it tries to pretty pring the scopes on more than one line


The documentation for this class was generated from the following files:
Generated on Tue Aug 15 10:09:38 2006 by  doxygen 1.4.6