FluentLogger: fluent-logger-mbed A structured logger for Fluentd (mbed)
Dependents: FluentLogger_Hello SNIC-FluentLogger-example
FluentLogger.cpp
00001 /* fluent-logger-mbed 00002 * Copyright (c) 2014 Yuuichi Akagawa 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "FluentLogger.h" 00018 #ifdef USE_NTP 00019 #include <time.h> 00020 #endif 00021 00022 FluentLogger::FluentLogger(const char *host, const int port, uint32_t bufsize) : 00023 _sock(), _host(host), _port(port), _timeout(1000) 00024 { 00025 _mp = new uMP(bufsize); 00026 _sock = NULL; 00027 } 00028 00029 int FluentLogger::open() 00030 { 00031 if ( _sock != NULL ) { 00032 return 0; 00033 } 00034 _sock = new TCPSocketConnection(); 00035 int ret = _sock->connect(_host, _port); 00036 if (ret < 0) 00037 { 00038 _sock->close(); 00039 return -1; 00040 } 00041 return 0; 00042 } 00043 00044 int FluentLogger::close() 00045 { 00046 if (_sock->is_connected()) { 00047 _sock->close(); 00048 } 00049 delete _sock; 00050 _sock = NULL; 00051 return 0; 00052 } 00053 00054 int FluentLogger::log(const char *tag, const char *msg) 00055 { 00056 if (_sock == NULL || !_sock->is_connected()) { 00057 if (open() < 0) { 00058 return -1; 00059 } 00060 } 00061 _mp->init(); 00062 00063 // tag, timestamp, message 00064 if (!_mp->start_array(3)) { 00065 return -1; 00066 } 00067 if (!_mp->set_str(tag, strlen(tag))) { 00068 return -1; 00069 } 00070 #ifdef USE_NTP 00071 if (!_mp->set_u32(time(NULL))) { 00072 return -1; 00073 } 00074 #else 00075 if (!_mp->set_u32(0)) { 00076 return -1; 00077 } 00078 #endif 00079 if (!_mp->set_str(msg, strlen(msg))) { 00080 return -1; 00081 } 00082 return(send()); 00083 } 00084 00085 int FluentLogger::log(const char *tag, uMP &mpmsg) 00086 { 00087 if (_sock == NULL || !_sock->is_connected()) { 00088 if (open() < 0) { 00089 return -1; 00090 } 00091 } 00092 _mp->init(); 00093 00094 // tag, timestamp, message 00095 if (!_mp->start_array(3)) { 00096 return -1; 00097 } 00098 if (!_mp->set_str(tag, strlen(tag))) { 00099 return -1; 00100 } 00101 #ifdef USE_NTP 00102 if (!_mp->set_u32(time(NULL))) { 00103 return -1; 00104 } 00105 #else 00106 if (!_mp->set_u32(0)) { 00107 return -1; 00108 } 00109 #endif 00110 if (!_mp->set_raw((const char*)mpmsg.get_buffer(), mpmsg.get_size())) { 00111 return -1; 00112 } 00113 return(send()); 00114 } 00115 00116 int FluentLogger::send() 00117 { 00118 _sock->set_blocking(false, _timeout); 00119 int ret = _sock->send_all((char*)_mp->get_buffer(), (int)_mp->get_size()); 00120 if ( ret < 0 ) {//fail 00121 close(); 00122 ret = -2; 00123 } 00124 return(ret); 00125 }
Generated on Sat Jul 16 2022 18:53:06 by 1.7.2