Skip to content

SysLoggerC++ is a simple and easy to use C++ thread-safe syslog wrapper.

License

Notifications You must be signed in to change notification settings

JakubFornadel/SysLoggerCpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SysLoggerC++

SysLoggerC++ is a simple and easy to use C++ thread-safe syslog wrapper.

Keywords: syslog, C++17, thread-safe, concurrent, library, wrapper C++

Note: For C++14 just replace std::optional with std::experimental::optional in code.

About SysLoggerC++:

SysLoggerC++ writes messages to the /var/log/syslog. These messages can be also filtered and redirected to the custom file. For this purpose special rsyslog and logrotate configuration files must be created and copied into the appropriate directories.

Multiple parameters can be specified:

  • application name that syslog messages belong to
  • min. log level
  • custom prefix that is displayed before actual messages
  • syslog options and facilities

Build instructions

Use git to clone the repository.

git clone https://github.com/JakubFornadel/SysLoggerCpp.git
cd SysLoggerCpp

A CMake configuration file is provided for multiplatform support.

mkdir build
cd build

cmake -DSYSLOGGERCPP_BUILD_EXAMPLES=ON ../
cmake --build .

# run example
./SyslogExample

# go back to the project root
cd ..

Custom syslog file

To redirect messages from example, copy config files as below:

sudo cp examples/etc/rsyslog.d/99-custom-syslog.conf /etc/rsyslog.d/
sudo cp examples/etc/logrotate.d/custom-syslog /etc/logrotate.d/

sudo service rsyslog restart
sudo service logrotate restart

Prepare directory for custom syslog file and its archives:

sudo mkdir -p /var/log/custom-syslog/
sudo chown -R syslog:adm /var/log/custom-syslog/

Now if you run ./build/SyslogExample, its syslog messages are redirected to the

cat /var/log/custom-syslog/custom-syslog-example.log

Usage

#include <SysLogger.hpp>
#include <string>

class Person {
public:
  Person(const std::string& name, int age) :
    name_(name),
    age_(age)
  {};
  ~Person() = default;

  friend std::ostream& operator <<(std::ostream& out, const Person& person) {
    out << "Name: " << person.name_ << ", age: " << person.age_;
    return out;
  }
private:
  std::string name_;
  int         age_;
};


int main(void) {
  // test data
  const std::string name = "Alice";
  const Person bob("Bob", 21);

  // Creates logger, specifies application name as "custom-syslog-example". Application name can be used for
  // filtering application specific messages from /var/log/syslog and redirecting it to the custom file
  SysLogger logger("custom-syslog-example");

  // logger accepts any TYPE of parameter to be printed, but it has to overload
  // std::ostream& operator <<(std::ostream& stream, const TYPE& obj)
  logger.info("Start logging...");
  logger.info("Bes friend of", name, "is", bob);

  logger.debug("debug message");
  logger.notice("notice message");
  logger.warning("warning message");
  logger.error("error message");
  logger.critical("critical message");
  logger.alert("alert message");
  logger.emergency("emergency message");

  logger.info("Finish logging...");
}

About

SysLoggerC++ is a simple and easy to use C++ thread-safe syslog wrapper.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published