Coder Social home page Coder Social logo

sysloggercpp's Introduction

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...");
}

sysloggercpp's People

Contributors

jakubfornadel avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

sysloggercpp's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.