Giter VIP home page Giter VIP logo

priority-logger-for-cpp's Introduction

priority-logger-for-cpp

This is a simple C++ logging library to log according to specified priorities.

architecture:

Logger: This write the logs with a priority number to the StackManager, it blocks if the total number of logs in memory are too large(> limit). One can append to the same log using multiple lines in the code.(see example) StackManager: This is the data storage class with in-memory stacks to store the logs. The logs in the stacks are meant to be drained by the LogReaders to avoid high memory consumption. The logger throttles if the total number of logs reaches certain threshold. The number of priorities can be specified. LogReader: This reads logs from the StackManager according the specified priorities. It blocks if there's nothing to read, as soon as there's something to read, it wakes up. LogStruct: A struct that encapsulates the actual log string, priority number, class name, time(epoch), thread id.

Examples:

using SM = StackManager<LogStruct>;

//threads A1, A2, B1 to log things
void threadA1(SM *sm) {
    Logger logger(sm, "class name A");
    logger.log(3, "first");
    logger.log(1, "abc");
    logger.log(2, "def");
    logger.log(1, "ghi");
    logger.log(3, "xyz");
}
void threadA2(SM *sm) {
    Logger logger(sm, "class name A");
    logger.log(1, "A2abc");
    logger.log(2, "A2def");
    logger.log(1, "A2ghi");
    logger.log(3, "A2xyz");
    logger.log(1, "A2klr");
}
void threadB1(SM *sm) {
    Logger logger(sm, "class name B");
    logger.setPriority(1);
    logger.log("B1xyz");
    logger.setPriority(3);
    logger.logAppend("B1abc");
    logger.logAppend("B1def");
    logger.logPublish();
    logger.setPriority(1);
    //this will automatically publish as well
    logger.logAppend("B1last");
}

//threads A3, B2, B3 to read the logs
void threadA3(SM *sm) {
    LogReader logReader(sm);
    for(;;) {
	//Note that get() is a blocking call
	auto logStruct = logReader.get();
	SafePrint::pretty_print(logStruct);
    }
}
void threadB2(SM *sm) {
    LogReader logReader(sm);
    for(;;) {
	auto logStruct = logReader.get();
	SafePrint::pretty_print(logStruct);
    }
}
void threadB3(SM *sm) {
    LogReader logReader(sm);
    //It prints the first one
    LogStruct log = logReader.get();
    SafePrint::pretty_print(log);
}


int main()
{
    cout << "Testing"<<endl;
    //create a QueueManager instance with 3 queues
    size_t q_num = 3;
    size_t limit = 100;
    auto sm = new StackManager<LogStruct>(q_num, limit);

    //spawn threads A1, A2, A3, B1, B2, B3
    thread A1(threadA1, sm);
    thread A2(threadA2, sm);
    thread B1(threadB1, sm);
    //sleep so all the producers will be done.
    std::this_thread::sleep_for(1s);

    thread A3(threadA3, sm);
    thread B2(threadB2, sm);
    thread B3(threadB3, sm);
    //sleep so all the consumers will be done.
    std::this_thread::sleep_for(1s);

    A1.join();
    A2.join();
    B1.join();
    A3.detach();
    B2.detach();
    B3.detach();
}

priority-logger-for-cpp's People

Contributors

ijklr avatar

Watchers

 avatar  avatar

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.