FluentLogger: fluent-logger-mbed A structured logger for Fluentd (mbed)
Dependents: FluentLogger_Hello SNIC-FluentLogger-example
FluentLogger
What is this ?
You can logging to Fluentd server.
This library included subset of MassagePack library.
Supported MessagePack formats(encode only)
format name | first byte (in hex) |
---|---|
positive fixint | 0x00 - 0x7f |
fixmap | 0x80 - 0x8f |
fixarray | 0x90 - 0x9f |
fixstr | 0xa0 - 0xbf |
nil | 0xc0 |
false | 0xc2 |
true | 0xc3 |
float 32 | 0xca |
float 64 | 0xcb |
uint 8 | 0xcc |
uint 16 | 0xcd |
uint 32 | 0xce |
uint 64 | 0xcf |
int 8 | 0xd0 |
int 16 | 0xd1 |
int 32 | 0xd2 |
int 64 | 0xd3 |
str 8 | 0xd9 |
negative fixint | 0xe0 - 0xff |
これは何?
Fluentd サーバにログを送信するためのライブラリです。
サブセット版のMassagePackライブラリも同梱しています。
サーバ側ダウン時の再接続機能は限定的に実装されています。 現時点での実装は送信時に切断を検知し、次回送信時に再接続する仕様です。
Diff: FluentLogger.cpp
- Revision:
- 0:b4815a079a4b
- Child:
- 1:6b1268731465
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FluentLogger.cpp Tue Nov 11 02:14:44 2014 +0000 @@ -0,0 +1,125 @@ +/* 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->set_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->is_connected()) { + if (open() < 0) { + return -1; + } + } + _mp->init(); + + // tag, timestamp, message + if (!_mp->set_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); +}