about
11/30/2022
Logger Repo
Logger code

Logger  Repository

Simultaneously log messages to multiple std::ostreams

Quick Status Code functions correctly No known defects Demonstration code yes Documentation yes Test cases no Static library no Build requires C++17 option Planned design changes add postTimed method

1.0 Concept:

Logger is a facility for inserting time-date stamped string messages into one or more std::iostreams concurrently. It is intended to work effectively with the single-user test harness described in Testing Repository documentation

2.0 Design:

Fig 1. Test Logger Classes
There are two logger classes in this design:
  1. TestLogger<Level>:
    Logs string messages to one or more std::ostream instances, perhaps std::cout and a file stream. Provides the ability to time-stamp any message, usually done only for the first message. Level determines whether a post is sent to its streams. Each logger has a level:
    • Level::debug
    • Level::demo
    • Level::results
    • Level::all
    That is compared to a global invariant Level logLevel to determine if post is sent. Applications set legLevel to determine what is posted during a program execution. logLevel = Level::all posts everything.
  2. QTestLogger<Level>:
    Adds queued writing to the streams. A logging application simply drops a log message into the write queue and returns immediately. The intent is to minimize performance issues with logging, as that is done on a child thread while the main thread continues with its test processing.
TestLogger<Level> supports adding multiple std::ostream references for concurrent logging to both the console and a log file, or anything else that supports a std::ostream interface. Logger post calls accept any type that has a dynamic_cast<std::string> for logging. It's expected that a test application may want to define a message class to hold application specific information for logging. This logger facility provides an ITestLogger<Level> interface and singleton object factory. That is important because a lot of the code in these repositories use, or will use, logging. Since these clients bind to the interface they won't be affected by changes that we decide to make to the logger (see below). QTestLogger<Level> inherits from both TestLogger<Level> and IQTestLogger<Level>. Therefore those two classes inherit the common root ITestLogger<Level> using public virtual inheritance to avoid having two copies of the root in QTestLogger<Level>. The purpose of using this structure is to allow clients of either logger to access all their facilities through an interface pointer or reference (examples of both can be found in the demonstration code).

Operation:

  1. Set logLevel = Level::all // [Level::debug, Level::demo, Level::results, Level::all]
  2. Create a logger with factory: auto pDemoLogger = createLogger<Level::demo>();
  3. Add streams: pDemoLogger->addStream(&std::cout);
    std::ofstream strm2;
    if(openFile("test.log",strm2)
       pDemoLogger->addStream(&strm2)
  4. Write log messages: pDemoLogger->postDated("logging demo");
    pDemoLogger->post("first msg").post("second msg");
  5. End logging: pDemoLogger->clear(); That closes file. You don't need to call delete on pDemoLogger. That is a std::unique_ptr<ITestLogger> and will release the heap-based logger when it goes out of scope.
The QTestLogger operations are the same except that QTestLogger provides a wait method, needed when you also use std::cout or other streams for output, so all outputs appear in the order they are written. Without calling wait output written after a post may be emitted before the post, which is written by a child thread. wait simply waits until the logger write queue is empty.

Build:

All of the logging functionalities are completely defined in their header files, so test developers only need to include those. They don't have to add implementation files to their test projects for logging. Logger code was built with Visual Studio Community Edition - 2019 and tested on Windows 10.

Status:

Installed. Plan to add postTimed method for QTestLogger. I haven't used this logger design on a large project, so there may be some design tweaks when I start doing that.
  Next Prev Pages Sections About Keys