Giter VIP home page Giter VIP logo

c-11-xlib's Introduction

XLIB ##What is XLIB?


Xlib is a collection of c + + commonly used functions and basic components.
It is for the purpose of cross-platform and reuse.
Including the logging system, thread pool, timer, TCP communications, and so on.
You can easily use it in your program.

##Usage

###1. log ###2. thread ###3. timer ###4. socket

log

//set log level
LOG_SET(LOG_LEVEL::L_ALL); 
//you can set the specified log file to record
XLog::setWrite(true, XFileUtil::getCurrentPathWithPrefix().append("xliblog"));
/**usage example.
LOG_I------'I' means info. 
And by that analogy:
LOG_D------'D' means debug.
LOG_E------'E' means error.
...
*/
std::string s="this is a log test!";
LOG_I("%s",s.c_str());

thread pool

auto fun = []
{
std::this_thread::sleep_for(std::chrono::seconds(rand()%50));
std::thread::id tid = std::this_thread::get_id();
auto t = XString::formatTime(XTime::getTimeFromTimestamp_milliseconds(XTime::getTimestamp_milliseconds(),8),TIME_F::T_DEFAULT);
LOG_I("thread_id=%d,%s",tid,t.c_str());
};
/** XThreadPool's first constructor parameter means thread max number,
the second is task max number,
the last is Whether begin immediately.
*/
auto pool = std::shared_ptr<XThreadPool>(new XThreadPool(5,5,true));

pool->addTask(fun);
pool->addTask(fun);
pool->addTask(fun);
pool->addTask(fun);
pool->detach();

timer

/**at first,you defined a timer's attributes:
1.repeate number
2.the number of seconds 
3.the callback
*/
XTime::doPertime(-1, 0.5,[]
{
LOG_D("this is a timer,1 second 2 times");
} );

//then ,you add a loop to do it.
auto loop = []
{
while (true)
{
XTimer::getInstance()->mainLoop();
}
};

Socket

/** Below is the tcp server test code*/
xlib::net::XSocketTCP server;
server.startServer(4435);

//if you take client,you can do as follow

auto tcp = std::shared_ptr<net::XSocketTCP>(new net::XSocketTCP);
tcp->startClient(net::_server(2347,"120.27.94.221"),true);
std::string chutf8 = "这是一个字符串";
auto s = XUtf8::utf8ToUnicode(chutf8);
LOG_I(s.c_str());
tcp->Send(chutf8.c_str(), sizeof(chutf8));

##Notes

  1. Compile environment: need GCC 4.8 or above, more than clang3.1.
  2. TDD: the whole project, on the MAC and centos test has no problem. Perform the make, then run the generated script.And specific usage see main.cpp.

##Q&A

You have any question, welcome to discuss.
And I also hope that we can put forward valuable Suggestions and comments. You can contact me through the following :

*Email:[email protected] *QQ: 229827701

c-11-xlib's People

Contributors

xiaominghe2014 avatar

Watchers

 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.