FluentLogger: fluent-logger-mbed A structured logger for Fluentd (mbed)

Dependents:   FluentLogger_Hello SNIC-FluentLogger-example

FluentLogger

Fluentd Logo

What is this ?

You can logging to Fluentd server.
This library included subset of MassagePack library.

Supported MessagePack formats(encode only)

format namefirst byte (in hex)
positive fixint0x00 - 0x7f
fixmap0x80 - 0x8f
fixarray0x90 - 0x9f
fixstr0xa0 - 0xbf
nil0xc0
false0xc2
true0xc3
float 320xca
float 640xcb
uint 80xcc
uint 160xcd
uint 320xce
uint 640xcf
int 80xd0
int 160xd1
int 320xd2
int 640xd3
str 80xd9
negative fixint0xe0 - 0xff

これは何?

Fluentd サーバにログを送信するためのライブラリです。
サブセット版のMassagePackライブラリも同梱しています。

サーバ側ダウン時の再接続機能は限定的に実装されています。 現時点での実装は送信時に切断を検知し、次回送信時に再接続する仕様です。

FluentLogger.cpp

Committer:
YuuichiAkagawa
Date:
2014-12-15
Revision:
1:6b1268731465
Parent:
0:b4815a079a4b

File content as of revision 1:6b1268731465:

/* fluent-logger-mbed 
 * Copyright (c) 2014 Yuuichi Akagawa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "FluentLogger.h"
#ifdef USE_NTP
#include <time.h>
#endif

FluentLogger::FluentLogger(const char *host, const int port, uint32_t bufsize) :
_sock(), _host(host), _port(port), _timeout(1000)
{
    _mp = new uMP(bufsize);
    _sock = NULL;
}

int FluentLogger::open()
{
    if ( _sock != NULL ) {
        return 0;
    }
    _sock = new TCPSocketConnection();
    int ret = _sock->connect(_host, _port);
    if (ret < 0)
    {
        _sock->close();
        return -1;
    }
    return 0;
}

int FluentLogger::close()
{
    if (_sock->is_connected()) {
        _sock->close();
    }
    delete _sock;
    _sock = NULL;
    return 0;
}

int FluentLogger::log(const char *tag, const char *msg)
{
    if (_sock == NULL || !_sock->is_connected()) {
        if (open() < 0) {
            return -1;
        }
    }
    _mp->init();

    // tag, timestamp, message
    if (!_mp->start_array(3)) {
        return -1;
    }
    if (!_mp->set_str(tag, strlen(tag))) {
        return -1;
    }
#ifdef USE_NTP  
    if (!_mp->set_u32(time(NULL))) {
        return -1;
    }
#else
    if (!_mp->set_u32(0)) {
        return -1;
    }
#endif
    if (!_mp->set_str(msg, strlen(msg))) {
        return -1;
    }
    return(send());
}

int FluentLogger::log(const char *tag, uMP &mpmsg)
{
    if (_sock == NULL || !_sock->is_connected()) {
        if (open() < 0) {
            return -1;
        }
    }
    _mp->init();

    // tag, timestamp, message
    if (!_mp->start_array(3)) {
        return -1;
    }
    if (!_mp->set_str(tag, strlen(tag))) {
        return -1;
    }
#ifdef USE_NTP  
    if (!_mp->set_u32(time(NULL))) {
        return -1;
    }
#else
    if (!_mp->set_u32(0)) {
        return -1;
    }
#endif
    if (!_mp->set_raw((const char*)mpmsg.get_buffer(), mpmsg.get_size())) {
        return -1;
    }
    return(send());
}

int FluentLogger::send()
{
    _sock->set_blocking(false, _timeout);
    int ret = _sock->send_all((char*)_mp->get_buffer(), (int)_mp->get_size());
    if ( ret < 0 ) {//fail
        close();
        ret = -2;
    }
    return(ret);
}