Skynet Source Learning-how the Logger service works

Source: Internet
Author: User
Tags message queue


when Skynet is started, a logger context is created based on the log file of the configuration file, the process is to find the logger.so dynamic link file, and then call its logger_create function (parameter is the log file of the configuration), It then constructs the context for the service (it is important that the service's callback function _logger ()) and Message Queuing are registered, and finally executes the logger_init function, putting logger's message queue into the global queue. The key code is as follows:
<span style= "White-space:pre" ></span>ctx->handle = Skynet_handle_register (CTX);    Register to get a service handle struct Message_queue * Queue = Ctx->queue = Skynet_mq_create (ctx->handle);//init function maybe use ct X->handle, so it must init at Lastcontext_inc ();    This skynet node service number plus 1checkcalling_begin (ctx) int r = skynet_module_instance_init (mod, inst, CTX, param); Checkcalling_end (CTX) if (r = = 0) {///Xxx_init successfully returned, just ref-1 = 1struct Skynet_context * ret = skynet_context_release (CTX); if (ret) {ctx->init = true;  Initialization succeeds}//into the global message queue and can accept messages from other services Skynet_globalmq_push (queue), if (ret) {skynet_error (ret, "LAUNCH%s%s", name, param? par am: "");} return ret;} else {skynet_error (CTX, "FAILED launch%s", name); uint32_t handle = Ctx->handle;skynet_context_release (CTX); skynet_ Handle_retire (handle); struct drop_t d = {handle};skynet_mq_release (queue, drop_message, &d); return NULL;}



The skynet_error () in error handling in the code is the log output function,The error string is constructed as a skynet_mssage and placed into the logger message queue, where the message's source handle is saved. The key code is as follows:
struct Skynet_message smsg;if (context = = NULL) {smsg.source = 0;} else {//msg source is CTX's handle number Smsg.source = Skynet_co Ntext_handle (context);} Smsg.session = 0;smsg.data = data;//sz Low 24 bit saved is data size SMSG.SZ = Len | (Ptype_text << Handle_remote_shift);//Put this message in Logger's queue Skynet_context_push (logger, &smsg);




when will those messages be exported? Skynet start with a new worker thread, their job is to consume these messages, execute the corresponding callback function, for logger is to output the information to the corresponding file handle. The code is as follows:
Static void_dispatch_message (struct Skynet_context *ctx, struct skynet_message *msg) {assert (Ctx->init); Checkcalling_begin (CTX) pthread_setspecific (G_node.handle_key, (void *) (uintptr_t) (ctx->handle)); int type = msg- >sz >> handle_remote_shift;size_t sz = msg->sz & handle_mask;//Execution Node callback interface _cbif (!CTX->CB (CTX, ctx- >cb_ud, type, msg->session, Msg->source, Msg->data, sz)) {skynet_free (msg->data);} Checkcalling_end (CTX)}







Skynet Source Learning-how the Logger service works

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.