项目作者: systelab

项目描述 :
C++ trace system
高级语言: C++
项目地址: git://github.com/systelab/cpp-trace-api.git
创建时间: 2019-11-19T22:11:37Z
项目社区:https://github.com/systelab/cpp-trace-api

开源协议:MIT License

下载


Build Status
Build status
codecov
Codacy Badge

C++ Trace System

This library provides utilities to easily add traces to your application.

Supported features

  • Multiple channels
  • Dump to .log files
  • Dedicated threads
  • Built-in fields
  • Channel enabling/disabling
  • Severity filtering
  • Backup generation
  • Deletion of old backups (rolling basis)

Setup

Download using Conan

This library is designed to be installed by making use of Conan package manager. So, you just need to add the following requirement into your Conan recipe:

  1. def requirements(self):
  2. self.requires("TraceAPI/1.0.0@systelab/stable")

Version number of this code snipped is set just as an example. Replace it for the desired package to retrieve.

As this package is not available on the conan-center, you will also need to configure a remote repository before installing dependencies:

  1. conan remote add systelab-public https://systelab.jfrog.io/artifactory/api/conan/cpp-conan-production-local

See Conan documentation for further details on how to integrate this package with your build system.

Build from sources

See BUILD.md document for details.

Usage

Set up a file agent

For each channel of traces to set up, a systelab::trace::FileAgent needs to be instantiated.

The constructor parameter of this class requires a configuration object where the trace channel particularities are defined:

  • Channel name
  • Path of logs base folder
  • Rotation folders prefix
  • Maximum number of rotation folders
  1. #include "TraceAPI/Configuration.h"
  2. #include "TraceAPI/FileAgent.h"
  3. auto configuration = std::make_unique<systelab::trace::Configuration>();
  4. configuration->setChannelName("MyChannel");
  5. configuration->setBaseFolderPath("./Subfolder/MyTraces");
  6. configuration->setMaxRotationDays(3);
  7. auto fileAgent = std::make_unique<systelab::trace::FileAgent>(std::move(configuration));

The agent instance must be kept alive (not destroyed) during the whole application lifecycle. Thus, all traces added when there is no agent instance won’t be recorded on the file.

Add a trace

Traces can be added using the TRACE_CHANNEL macro providing:

  • Channel name as an argument
  • Trace content through the stream (<<) operator
  1. #include "TraceAPI/ChannelMacro.h"
  2. TRACE_CHANNEL("MyChannel") << "This is the trace number " << 1 << " to add";

It is highly recommended to define your own macros to easily trace to an specific channel:

  1. #define TRACE_MY_CHANNEL() \
  2. TRACE_CHANNEL("MyChannel")

Then, traces can be added as follows:

  1. TRACE_MY_CHANNEL() << "Trace added using custom macro.";

Built-in fields

The library is also prepared to record the severity and a tag value for each trace added.

As the usage of these fields is optional, the library provides specific macros designed for that purpose:

  1. #include "TraceAPI/ChannelMacro.h"
  2. TRACE_CHANNEL_SEVERITY("MyChannel", "INFO") << "This is a trace with 'INFO' severity";
  3. TRACE_CHANNEL_TAG("MyChannel", "MY_TAG") << "This is a trace with 'MY_TAG' tag";
  4. TRACE_CHANNEL_SEVERITY_TAG("MyChannel", "ERROR", "TAG2") << "An error trace with 'TAG2' tag";

Rotation

Trace files are automatically rotated at midnight. However, if you want to force a log file rotation, just call the rotate() method of the associated FileAgent entity.

  1. fileAgent->rotate();

That would move the current traces file into a Logs_YYYY_MM_DD subfolder, where YYYY, MM and DD respectively correspond with the year, month and day of the current date. In order to allow archiving of multiple trace files for the same day, a timestamp is appended to trace the filename.

Additionally, old backups are automatically deleted, so only the configured amount of rotation days folders is kept.

Prefix of rotation folders can be customized by means of the setRotationFoldersPrefix of the configuration object (by default, it is set to Logs):

  1. configuration->setRotationFoldersPrefix("MyPrefix");

Channel disabling

A channel can be temporarilly disabled by using the enable() method of the associated file agent:

  1. fileAgent->enable(false);

Similarly, the channel can be reenabled as follows:

  1. fileAgent->enable(true);

Severity filtering

This library allows defining which severity levels will be logged on the trace files. These severities are set using the setSeverityFilter() method of the configuration object:

  1. std::vector<std::string> severityFilter = {"ERROR", "WARNING"};
  2. configuration->setSeverityFilter(severityFilter);

By default (when no filter is configured), traces for any severity level are added to files.