MFLogger

MFLogger
C++ library for file logging

Based around the standard function strftime for timestamps. To download see the bottom of this page. There are two classes you can use. A file logger and a string object.

Log        // log to a file object
LogString  // to not log to a file but build a log line with time prefix

To use: include the library header

#include <mflogger/log.h>

create an mflogger::Log object passing a filename in the constructor.

mflogger::Log log("filename.log");

or an mflogger::LogString object.

mflogger::LogString log2;

to log to the file call the log function.

log.log("a log line");

or to just get the string use the LogString objects logstring function

std::string line(log2.logstring("a log line") );

By default the time is logged in ISO8601 format: YYYY-MM-DDTHH:MM:SS
e.g 1983-04-17T14:23:05
So if the above example was logged at the example time the log line/string would read like this:-
1983-04-17T14:23:05 a log line

You can set how a log line is formatted by calling the strftime_prefix

log.strftime_prefix("[%I:%M myprefix] ");
log2.strftime_prefix("[%I:%M myprefix] ");

Then the line above would be read like this if it was 2:23 in the log file/string
[02:23 myprefix] a log line

INTERFACE
Log interface

public:
  Log(const std::string& Logfilename, const bool& append = true);
  ~Log();
  bool open(const std::string& Logfilename, const bool& append = true);
  bool isgood() const;
  std::string log(const std::string& message, bool timeasstring = true);
  Log& operator<<(const char* in);
  Log& operator<<(const std::string& in);
  Log& operator<<(char in);
  Log& operator<<(int in);
  Log& operator<<(short in);
  Log& operator<<(long in);
  Log& operator<<(float in);
  Log& operator<<(double in);
  Log& operator<<(long double in);
  Log& operator<<(unsigned int in);
  Log& operator<<(unsigned short in);
  Log& operator<<(unsigned long in);

  void strftime_prefix(const std::string& FORMAT);

LogString interface

public:
  LogString();
  ~LogString();
  std::string logstring(const std::string& message, bool timeasstring = true);
  void strftime_prefix(const std::string& FORMAT);

EXAMPLE CODE

#include <iostream>
#include <mflogger/log.h>

int main(int argc, char* argv[], char *envp[]) {
  const std::string logfilename("log.file");
  // could of used:- using namespace logger;
  mflogger::Log log(logfilename);
  if(log.isgood()) { // log file created/opened ok
    log.log("line one logging with time as an ISO8601 string");
    // set a new prefix string
    log.strftime_prefix("[{%D %T} changed the prefix] ");
    log.log("line two logging with personal prefix");
    log << "Streaming values with c++ insertion operator (<<)" 
        << 8 << 34.6f << 'L' << 1200000000340000.45;
  }
  else {
    std::cout << "Oops, could not create/open log file - " << logfilename << "\n";
  }

  mflogger::LogString log2;
  std::cout << log2.logstring("test LogString interface") << std::endl;
  return 0;
}

If this was a file called logexample.cpp to compile it run the command:
g++ logexample.cpp -o log `pkg-config –libs –cflags mflogger-<version number>`

Run log and open “log.file” to see the results of the program. This program is included as an example with the library.

to install download and unpack the files and do the make dance
tar -xvzf mflogger-<version number>.tar.gz
make
sudo make install

There is a man page
man mflogger

SEE ALSO
strftime(3)
for format characters you can use for the strftime_prefix function.

Attachment Size
mflogger-1.0.1.tar.gz 105.07 KB