Commit a743a1d1 authored by 尹佳钦's avatar 尹佳钦

111

parents 2d71ac7f e6ffee28
LIBA_TARGET := libiot_kcloud.a LIBA_TARGET := libiot_kcloud.a
$(call Append_Conditional, LIB_SRCS_EXCLUDE, kcloud_main.c) $(call Append_Conditional, LIB_SRCS_EXCLUDE, kcloud_main.c)
$(call Append_Conditional, SRCS_kcloud, kcloud_main.c) $(call Append_Conditional, SRCS_kcloud, kcloud_main.c)
$(call Append_Conditional, TARGET, kcloud) $(call Append_Conditional, TARGET, kcloud)
CFLAGS += -I$(TOP_DIR)/common/api CFLAGS += -I$(TOP_DIR)/common/api
CFLAGS += -I$(TOP_DIR)/common/json CFLAGS += -I$(TOP_DIR)/common/json
CFLAGS += -I$(TOP_DIR)/common/nanomsg/include CFLAGS += -I$(TOP_DIR)/common/nanomsg/include
CFLAGS += -I$(TOP_DIR)/common/ev/include CFLAGS += -I$(TOP_DIR)/common/ev/include
LDFLAGS += -lkk_tsl LDFLAGS += -lkk_tsl
LDFLAGS += -lapi_com LDFLAGS += -lapi_com
ifeq ($(CONFIG_VENDOR),ubuntu) ifeq ($(CONFIG_VENDOR),ubuntu)
LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg_ubuntu LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg_ubuntu
LDFLAGS += -L$(TOP_DIR)/common/ev -lev_ubuntu LDFLAGS += -L$(TOP_DIR)/common/ev -lev_ubuntu
else else
LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg LDFLAGS += -L$(TOP_DIR)/common/nanomsg -lnanomsg
LDFLAGS += -L$(TOP_DIR)/common/ev -lev LDFLAGS += -L$(TOP_DIR)/common/ev -lev
endif endif
LDFLAGS += -liot_cjson -liot_mqtt -ldl -lm -lanl -lkk_hal -lzlog LDFLAGS += -liot_cjson -liot_mqtt -ldl -lm -lanl -lkk_hal
#ifndef MQTT_CONF_H_ #ifndef MQTT_CONF_H_
#define MQTT_CONF_H_ #define MQTT_CONF_H_
#define ADDRESS "tcp://172.25.240.31:1983" #define ADDRESS "tcp://172.25.240.31:1983"
#define CLIENTID "kk_%s" #define CLIENTID "kk_%s"
#define TOPIC "/sys/a1OYuSBt23u/aIqEbWno8yDdsjCX15iq/thing/service/property/set" #define TOPIC "/sys/a1OYuSBt23u/aIqEbWno8yDdsjCX15iq/thing/service/property/set"
#define PAYLOAD "Hello cwc World!" #define PAYLOAD "Hello cwc World!"
#define QOS 2 #define QOS 0
#define TIMEOUT 10000L #define TIMEOUT 10000L
#define USRNAME "hangzhou_gateway " #define USRNAME "hangzhou_gateway "
#define PASSWORD "2ca1442865ff4cb99870f60f2c646190" #define PASSWORD "2ca1442865ff4cb99870f60f2c646190"
#define AUTO_CONN 1 #define AUTO_CONN 1
#define CONNECT_TIMEOUT 3 #define CONNECT_TIMEOUT 3
typedef enum {
RETURN_FAIL = -1,
#endif RETURN_SUCCESS = 0,
} kk_kcloud_error_code_t;
#endif
This diff is collapsed.
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2012, 2020 IBM Corp. * Copyright (c) 2012, 2020 IBM Corp.
* *
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0 * are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution. * and Eclipse Distribution License v1.0 which accompany this distribution.
* *
* The Eclipse Public License is available at * The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/ * https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at * and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php. * http://www.eclipse.org/org/documents/edl-v10.php.
* *
* Contributors: * Contributors:
* Ian Craggs - initial contribution * Ian Craggs - initial contribution
*******************************************************************************/ *******************************************************************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include "MQTTAsync.h" #include "MQTTAsync.h"
#include "mqtt_api.h" #include "mqtt_api.h"
#include "com_api.h" #include "com_api.h"
#include "kk_product.h" #include "kk_product.h"
//#include "kcloud_log.h"
static int mqtt_start(void) static int mqtt_start(void)
{ {
int count = 0; int count = 0;
MQTTAsync mqttClient; MQTTAsync mqttClient;
int rc = 0; int rc = 0;
mqttClient = KK_MQTT_Connect(); mqttClient = KK_MQTT_Connect();
if(mqttClient == NULL) if(mqttClient == NULL)
{ {
WARNING_PRINT("KK_MQTT_Connect FAIL!!!\n"); WARNING_PRINT("KK_MQTT_Connect FAIL!!!\n");
} }
else else
{ {
INFO_PRINT("Waiting for publication of %s\n" INFO_PRINT("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n", "on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID); PAYLOAD, TOPIC, CLIENTID);
} }
while(1) while(1)
{ {
usleep(100000L); usleep(100000L);
count++; count++;
if(count>50) if(count>50)
{ {
count = 0; count = 0;
//INFO_PRINT("i am send now\n"); //INFO_PRINT("i am send now\n");
//KK_MQTT_SendMsg(TOPIC,"hello my world",2); //KK_MQTT_SendMsg(TOPIC,"hello my world",2);
} }
} }
INFO_PRINT("MQTTAsync_destroy\n"); INFO_PRINT("MQTTAsync_destroy\n");
MQTTAsync_destroy(&mqttClient); MQTTAsync_destroy(&mqttClient);
return rc; return rc;
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int rc = 0; int rc = 0;
//KK_Data_Hdl_Init(); open("kcloud",LOG_PID,LOG_USER);
kk_zlog_init("kcloud"); /*set the callback to get the device date to cloud*/
kk_ipc_init(IPC_APP2MID,KK_Data_FromDev,NULL,NULL);
/*set the callback to get the device date to cloud*/ rc = mqtt_start();
HAL_SetProduct_Type(PRODUCT_TPYE);
HAL_SetProduct_Code(PRODUCT_CODE); return rc;
kk_ipc_init(IPC_APP2MID,KK_Data_FromDev,NULL,NULL); }
rc = mqtt_start();
return rc;
}
This diff is collapsed.
This diff is collapsed.
//=========kk============= //=========kk=============
#include "com_api.h" #include "com_api.h"
#include "kk_log.h"
#define APP2MID "ipc:///tmp/app2mid.ipc" #define APP2MID "ipc:///tmp/app2mid.ipc"
#define PLAT2MID "ipc:///tmp/plat2mid.ipc" #define PLAT2MID "ipc:///tmp/plat2mid.ipc"
...@@ -10,21 +11,6 @@ ...@@ -10,21 +11,6 @@
#define FILTERSTR "|" #define FILTERSTR "|"
#ifndef _ZLOG_
#undef INFO_PRINT
#undef WARNING_PRINT
#undef ERROR_PRINT
#define INFO_PRINT printf
#define WARNING_PRINT printf
#define ERROR_PRINT printf
#else
#include "kk_log.h"
#endif
typedef struct { typedef struct {
int n;//nanomsg socket int n;//nanomsg socket
int s;//nanomsg recieve fd int s;//nanomsg recieve fd
......
/* /*
* Copyright (C) 2020-2020 ikonke * Copyright (C) 2020-2020 ikonke
*/ */
#ifndef _KK_COM_API_H_ #ifndef _KK_COM_API_H_
#define _KK_COM_API_H_ #define _KK_COM_API_H_
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "ev.h" #include "ev.h"
#include "nn.h" #include "nn.h"
#include "pair.h" #include "pair.h"
#include "pubsub.h" #include "pubsub.h"
#include "pipeline.h" #include "pipeline.h"
//=====kk====================== //=====kk======================
typedef enum { typedef enum {
IPC_APP2MID = 0, IPC_APP2MID = 0,
IPC_MID2APP, IPC_MID2APP,
IPC_MID2PLAT, IPC_MID2PLAT,
IPC_PLAT2MID, IPC_PLAT2MID,
IPC_UNDEF IPC_UNDEF
} ipc_type; } ipc_type;
#define MSG_TYPE_STR "msgtype" #define MSG_TYPE_STR "msgtype"
#define MSG_PRODUCT_TYPE_STR "productType" #define MSG_PRODUCT_TYPE_STR "productType"
#define MSG_PRODUCT_CODE_STR "productCode" #define MSG_PRODUCT_CODE_STR "productCode"
#define MSG_DEVICE_CODE_STR "deviceCode" #define MSG_DEVICE_CODE_STR "deviceCode"
#define MSG_PAYLOAD_STR "payload" #define MSG_PAYLOAD_STR "payload"
#define MSG_INFO_STR "info" #define MSG_INFO_STR "info"
#define MSG_INDENTIFIER_STR "identifier" #define MSG_INDENTIFIER_STR "identifier"
#define MSG_PARAMS_STR "params" #define MSG_PARAMS_STR "params"
#define MSG_IOTClOUDSTATE_STR "IOTCloudState" #define MSG_IOTClOUDSTATE_STR "IOTCloudState"
#define MSG_TOPO_CHANGE_TYPE_STR "changeType"
typedef void ipc_cb(void* data, int len, char* chalMark); #define MSG_TOPO_CHANGE_DEVICES_STR "devices"
int kk_ipc_init(ipc_type type, ipc_cb cb, char* chalMark, char* ip);
int kk_ipc_deinit(); typedef void ipc_cb(void* data, int len, char* chalMark);
int kk_ipc_send(ipc_type type, void* data, int len); int kk_ipc_init(ipc_type type, ipc_cb cb, char* chalMark, char* ip);
int kk_ipc_send_ex(ipc_type type, void* data, int len, char* chalMark); int kk_ipc_deinit(ipc_type type);
int kk_ipc_send(ipc_type type, void* data, int len);
#if defined(__cplusplus) int kk_ipc_send_ex(ipc_type type, void* data, int len, char* chalMark);
}
#endif #if defined(__cplusplus)
#endif }
#endif
#endif
...@@ -320,7 +320,7 @@ static void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) ...@@ -320,7 +320,7 @@ static void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
} }
int res = 0; int res = 0;
kk_tcp_ctrl_t* tcp_ctrl = (kk_tcp_ctrl_t*)watcher->data; kk_tcp_ctrl_t* tcp_ctrl = (kk_tcp_ctrl_t*)watcher->data;
int32_t bytes = read(watcher->fd, buffer, sizeof(buffer)); int32_t bytes = recv(watcher->fd, buffer, sizeof(buffer),0);
if (-1 == bytes) { if (-1 == bytes) {
//tcp Error //tcp Error
if (EINTR != errno && EAGAIN != errno) { if (EINTR != errno && EAGAIN != errno) {
...@@ -338,7 +338,7 @@ static void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) ...@@ -338,7 +338,7 @@ static void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
free(watcher); free(watcher);
} else { } else {
//intf("READ:\r\n %s\r\n", buffer); //intf("READ:\r\n %s\r\n", buffer);
printf("read_cb deviceCode ip sock [%s][%s][%d]",tcp_ctrl->deviceCode,tcp_ctrl->ip, tcp_ctrl->sock); //printf("read_cb deviceCode ip sock [%s][%s][%d] \n",tcp_ctrl->deviceCode,tcp_ctrl->ip, tcp_ctrl->sock);
if (g_cb != NULL){ if (g_cb != NULL){
g_cb(buffer,bytes,tcp_ctrl->deviceCode); g_cb(buffer,bytes,tcp_ctrl->deviceCode);
} }
...@@ -659,6 +659,7 @@ static void loop_tcp_client_thread(void *arg){ ...@@ -659,6 +659,7 @@ static void loop_tcp_client_thread(void *arg){
{ {
//接受网络数据 //接受网络数据
_MutexLock(g_client_ctrl.mutex); _MutexLock(g_client_ctrl.mutex);
memset(buf,0x0,sizeof(buf));
ret = read(g_client_ctrl.sd, buf, sizeof(buf)); ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex); _MutexUnlock(g_client_ctrl.mutex);
...@@ -670,7 +671,7 @@ static void loop_tcp_client_thread(void *arg){ ...@@ -670,7 +671,7 @@ static void loop_tcp_client_thread(void *arg){
break; break;
} }
}else if(ret > 0){ }else if(ret > 0){
printf("buf = %s\n",buf); //printf("11buf = %s\n",buf);
if (g_client_ctrl.cb != NULL){ if (g_client_ctrl.cb != NULL){
g_client_ctrl.cb(buf,ret,""); g_client_ctrl.cb(buf,ret,"");
} }
...@@ -701,6 +702,7 @@ int kk_reset_retry_num(){ ...@@ -701,6 +702,7 @@ int kk_reset_retry_num(){
int kk_tcp_client_send(char* data, int len){ int kk_tcp_client_send(char* data, int len){
int ret = 0; int ret = 0;
int cnt = 0; int cnt = 0;
if ( data != NULL){ if ( data != NULL){
while(g_client_ctrl.sd == -1 && cnt < 5){ while(g_client_ctrl.sd == -1 && cnt < 5){
printf("[%s] tcp don't connect, sleep 1s !!!! \n",__FUNCTION__); printf("[%s] tcp don't connect, sleep 1s !!!! \n",__FUNCTION__);
......
...@@ -115,6 +115,14 @@ uint64_t HAL_UptimeMs(void) ...@@ -115,6 +115,14 @@ uint64_t HAL_UptimeMs(void)
return time_ms; return time_ms;
} }
uint64_t HAL_GetTime(void)
{
time_t t;
t = time(NULL);
return t;
}
uint64_t HAL_GetTimeMs(void) uint64_t HAL_GetTimeMs(void)
{ {
uint64_t time_ms; uint64_t time_ms;
......
#ifndef __KK_PRODUCT_H__ #ifndef __KK_PRODUCT_H__
#define __KK_PRODUCT_H__ #define __KK_PRODUCT_H__
#include <stdio.h> #include <stdio.h>
#ifndef _IN_ #ifndef _IN_
#define _IN_ #define _IN_
#endif #endif
#ifndef _OU_ #ifndef _OU_
#define _OU_ #define _OU_
#endif #endif
#define PRODUCT_TYPE_LEN (32+1) #define PRODUCT_TYPE_LEN (32+1)
#define PRODUCT_CODE_LEN (32+1) #define PRODUCT_CODE_LEN (32+1)
#define DEVICE_CODE_LEN (32+1) #define DEVICE_CODE_LEN (32+1)
#define MAC_ADDR_LEN_MAX (10) #define MAC_ADDR_LEN_MAX (10)
#define PID_STRLEN_MAX (64) #define PID_STRLEN_MAX (64)
#define MID_STRLEN_MAX (64) #define MID_STRLEN_MAX (64)
#define IOTX_URI_MAX_LEN (135) /* IoTx CoAP/HTTP uri & MQTT topic maximal length */ #define IOTX_URI_MAX_LEN (135) /* IoTx CoAP/HTTP uri & MQTT topic maximal length */
#define PID_STR_MAXLEN (64) #define PID_STR_MAXLEN (64)
#define MID_STR_MAXLEN (64) #define MID_STR_MAXLEN (64)
#define PRODUCT_SECRET_MAXLEN (64 + 1) #define PRODUCT_SECRET_MAXLEN (64 + 1)
#define FIRMWARE_VERSION_MAXLEN (32 + 1) #define FIRMWARE_VERSION_MAXLEN (32 + 1)
#define VERSION_MAXLEN (32 + 1) #define VERSION_MAXLEN (32 + 1)
#define HAL_CID_LEN (64 + 1) #define HAL_CID_LEN (64 + 1)
#define NETWORK_ADDR_LEN (16) /* IPÍøÂçµØÖ·µÄ³¤¶È */ #define NETWORK_ADDR_LEN (16+1) /* IPÍøÂçµØÖ·µÄ³¤¶È */
#define MAC_ADDR_LEN (17+1) #define MAC_ADDR_LEN (17+1)
#define SN_ADDR_LEN (32+1) #define SN_ADDR_LEN (32+1)
#define PRODUCT_TPYE "kk" #define PRODUCT_TPYE "kk"
#define PRODUCT_CODE "15" #define PRODUCT_CODE "15"
int HAL_SetProduct_Type(_IN_ char *product_type); int HAL_SetProduct_Type(_IN_ char *product_type);
int HAL_SetProduct_Code(_IN_ char *product_code); int HAL_SetProduct_Code(_IN_ char *product_code);
int HAL_SetDevice_Code(_IN_ char *device_code); int HAL_SetDevice_Code(_IN_ char *device_code);
int HAL_GetProduct_Type(_OU_ char *product_type); int HAL_GetProduct_Type(_OU_ char *product_type);
int HAL_GetProduct_Code(_OU_ char *product_code); int HAL_GetProduct_Code(_OU_ char *product_code);
int HAL_GetDevice_Code(_OU_ char *device_code); int HAL_GetDevice_Code(_OU_ char *device_code);
#endif #endif
...@@ -2885,7 +2885,7 @@ static int MQTTAsync_cleanSession(Clients* client) ...@@ -2885,7 +2885,7 @@ static int MQTTAsync_cleanSession(Clients* client)
static int MQTTAsync_deliverMessage(MQTTAsyncs* m, char* topicName, size_t topicLen, MQTTAsync_message* mm) static int MQTTAsync_deliverMessage(MQTTAsyncs* m, char* topicName, size_t topicLen, MQTTAsync_message* mm)
{ {
int rc; int rc;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
Log(TRACE_MIN, -1, "Calling messageArrived for client %s, queue depth %d", Log(TRACE_MIN, -1, "Calling messageArrived for client %s, queue depth %d",
m->c->clientID, m->c->messageQueue->count); m->c->clientID, m->c->messageQueue->count);
rc = (*(m->ma))(m->maContext, topicName, (int)topicLen, mm); rc = (*(m->ma))(m->maContext, topicName, (int)topicLen, mm);
...@@ -2902,7 +2902,6 @@ void Protocol_processPublication(Publish* publish, Clients* client, int allocate ...@@ -2902,7 +2902,6 @@ void Protocol_processPublication(Publish* publish, Clients* client, int allocate
MQTTAsync_message* mm = NULL; MQTTAsync_message* mm = NULL;
MQTTAsync_message initialized = MQTTAsync_message_initializer; MQTTAsync_message initialized = MQTTAsync_message_initializer;
int rc = 0; int rc = 0;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
FUNC_ENTRY; FUNC_ENTRY;
if ((mm = malloc(sizeof(MQTTAsync_message))) == NULL) if ((mm = malloc(sizeof(MQTTAsync_message))) == NULL)
......
...@@ -304,7 +304,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock) ...@@ -304,7 +304,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock)
Clients* client = NULL; Clients* client = NULL;
char* clientid = NULL; char* clientid = NULL;
int rc = TCPSOCKET_COMPLETE; int rc = TCPSOCKET_COMPLETE;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
FUNC_ENTRY; FUNC_ENTRY;
client = (Clients*)(ListFindItem(bstate->clients, &sock, clientSocketCompare)->content); client = (Clients*)(ListFindItem(bstate->clients, &sock, clientSocketCompare)->content);
...@@ -313,7 +312,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock) ...@@ -313,7 +312,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock)
publish->header.bits.retain, publish->payloadlen, min(20, publish->payloadlen), publish->payload); publish->header.bits.retain, publish->payloadlen, min(20, publish->payloadlen), publish->payload);
if (publish->header.bits.qos == 0){ if (publish->header.bits.qos == 0){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
Protocol_processPublication(publish, client, 1); Protocol_processPublication(publish, client, 1);
} }
else if (!Socket_noPendingWrites(sock)) else if (!Socket_noPendingWrites(sock))
...@@ -324,7 +322,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock) ...@@ -324,7 +322,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock)
rc = MQTTPacket_send_puback(publish->MQTTVersion, publish->msgId, &client->net, client->clientID); rc = MQTTPacket_send_puback(publish->MQTTVersion, publish->msgId, &client->net, client->clientID);
/* if we get a socket error from sending the puback, should we ignore the publication? */ /* if we get a socket error from sending the puback, should we ignore the publication? */
Protocol_processPublication(publish, client, 1); Protocol_processPublication(publish, client, 1);
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
} }
else if (publish->header.bits.qos == 2) else if (publish->header.bits.qos == 2)
{ {
...@@ -374,7 +371,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock) ...@@ -374,7 +371,6 @@ int MQTTProtocol_handlePublishes(void* pack, int sock)
publish1.payloadlen = m->publish->payloadlen; publish1.payloadlen = m->publish->payloadlen;
publish1.MQTTVersion = m->MQTTVersion; publish1.MQTTVersion = m->MQTTVersion;
publish1.properties = m->properties; publish1.properties = m->properties;
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
Protocol_processPublication(&publish1, client, 1); Protocol_processPublication(&publish1, client, 1);
ListRemove(&(state.publications), m->publish); ListRemove(&(state.publications), m->publish);
......
This diff is collapsed.
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_buf_h
#define __zlog_buf_h
/* buf, is a dynamic expand buffer for one single log,
* as one single log will interlace if use multiple write() to file.
* and buf is always keep in a thread, to make each thread has its
* own buffer to avoid lock.
*/
#include <stdarg.h>
#include <stdint.h>
typedef struct zlog_buf_s {
char *start;
char *tail;
char *end;
char *end_plus_1;
size_t size_min;
size_t size_max;
size_t size_real;
char truncate_str[MAXLEN_PATH + 1];
size_t truncate_str_len;
} zlog_buf_t;
zlog_buf_t *zlog_buf_new(size_t min, size_t max, const char *truncate_str);
void zlog_buf_del(zlog_buf_t * a_buf);
void zlog_buf_profile(zlog_buf_t * a_buf, int flag);
int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args);
int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len);
int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
int left_adjust, int zero_pad, size_t in_width, size_t out_width);
int zlog_buf_printf_dec32(zlog_buf_t * a_buf, uint32_t ui32, int width);
int zlog_buf_printf_dec64(zlog_buf_t * a_buf, uint64_t ui64, int width);
int zlog_buf_printf_hex(zlog_buf_t * a_buf, uint32_t ui32, int width);
#define zlog_buf_restart(a_buf) do { \
a_buf->tail = a_buf->start; \
} while(0)
#define zlog_buf_len(a_buf) (a_buf->tail - a_buf->start)
#define zlog_buf_str(a_buf) (a_buf->start)
#define zlog_buf_seal(a_buf) do {*(a_buf)->tail = '\0';} while (0)
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#include "fmacros.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "category.h"
#include "rule.h"
#include "zc_defs.h"
void zlog_category_profile(zlog_category_t *a_category, int flag)
{
int i;
zlog_rule_t *a_rule;
zc_assert(a_category,);
zc_profile(flag, "--category[%p][%s][%p]--",
a_category,
a_category->name,
a_category->fit_rules);
if (a_category->fit_rules) {
zc_arraylist_foreach(a_category->fit_rules, i, a_rule) {
zlog_rule_profile(a_rule, flag);
}
}
return;
}
/*******************************************************************************/
void zlog_category_del(zlog_category_t * a_category)
{
zc_assert(a_category,);
if (a_category->fit_rules) zc_arraylist_del(a_category->fit_rules);
zc_debug("zlog_category_del[%p]", a_category);
free(a_category);
return;
}
/* overlap one rule's level bitmap to cateogry,
* so category can judge whether a log level will be output by itself
* It is safe when configure is reloaded, when rule will be released an recreated
*/
static void zlog_cateogry_overlap_bitmap(zlog_category_t * a_category, zlog_rule_t *a_rule)
{
int i;
for(i = 0; i < sizeof(a_rule->level_bitmap); i++) {
a_category->level_bitmap[i] |= a_rule->level_bitmap[i];
}
}
static int zlog_category_obtain_rules(zlog_category_t * a_category, zc_arraylist_t * rules)
{
int i;
int count = 0;
int fit = 0;
zlog_rule_t *a_rule;
zlog_rule_t *wastebin_rule = NULL;
/* before set, clean last fit rules first */
if (a_category->fit_rules) zc_arraylist_del(a_category->fit_rules);
memset(a_category->level_bitmap, 0x00, sizeof(a_category->level_bitmap));
a_category->fit_rules = zc_arraylist_new(NULL);
if (!(a_category->fit_rules)) {
zc_error("zc_arraylist_new fail");
return -1;
}
/* get match rules from all rules */
zc_arraylist_foreach(rules, i, a_rule) {
fit = zlog_rule_match_category(a_rule, a_category->name);
if (fit) {
if (zc_arraylist_add(a_category->fit_rules, a_rule)) {
zc_error("zc_arrylist_add fail");
goto err;
}
zlog_cateogry_overlap_bitmap(a_category, a_rule);
count++;
}
if (zlog_rule_is_wastebin(a_rule)) {
wastebin_rule = a_rule;
}
}
if (count == 0) {
if (wastebin_rule) {
zc_debug("category[%s], no match rules, use wastebin_rule", a_category->name);
if (zc_arraylist_add(a_category->fit_rules, wastebin_rule)) {
zc_error("zc_arrylist_add fail");
goto err;
}
zlog_cateogry_overlap_bitmap(a_category, wastebin_rule);
count++;
} else {
zc_debug("category[%s], no match rules & no wastebin_rule", a_category->name);
}
}
return 0;
err:
zc_arraylist_del(a_category->fit_rules);
a_category->fit_rules = NULL;
return -1;
}
zlog_category_t *zlog_category_new(const char *name, zc_arraylist_t * rules)
{
size_t len;
zlog_category_t *a_category;
zc_assert(name, NULL);
zc_assert(rules, NULL);
len = strlen(name);
if (len > sizeof(a_category->name) - 1) {
zc_error("name[%s] too long", name);
return NULL;
}
a_category = calloc(1, sizeof(zlog_category_t));
if (!a_category) {
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
strcpy(a_category->name, name);
a_category->name_len = len;
if (zlog_category_obtain_rules(a_category, rules)) {
zc_error("zlog_category_fit_rules fail");
goto err;
}
zlog_category_profile(a_category, ZC_DEBUG);
return a_category;
err:
zlog_category_del(a_category);
return NULL;
}
/*******************************************************************************/
/* update success: fit_rules 1, fit_rules_backup 1 */
/* update fail: fit_rules 0, fit_rules_backup 1 */
int zlog_category_update_rules(zlog_category_t * a_category, zc_arraylist_t * new_rules)
{
zc_assert(a_category, -1);
zc_assert(new_rules, -1);
/* 1st, mv fit_rules fit_rules_backup */
if (a_category->fit_rules_backup) zc_arraylist_del(a_category->fit_rules_backup);
a_category->fit_rules_backup = a_category->fit_rules;
a_category->fit_rules = NULL;
memcpy(a_category->level_bitmap_backup, a_category->level_bitmap,
sizeof(a_category->level_bitmap));
/* 2nd, obtain new_rules to fit_rules */
if (zlog_category_obtain_rules(a_category, new_rules)) {
zc_error("zlog_category_obtain_rules fail");
a_category->fit_rules = NULL;
return -1;
}
/* keep the fit_rules_backup not change, return */
return 0;
}
/* commit fail: fit_rules_backup != 0 */
/* commit success: fit_rules 1, fit_rules_backup 0 */
void zlog_category_commit_rules(zlog_category_t * a_category)
{
zc_assert(a_category,);
if (!a_category->fit_rules_backup) {
zc_warn("a_category->fit_rules_backup is NULL, never update before");
return;
}
zc_arraylist_del(a_category->fit_rules_backup);
a_category->fit_rules_backup = NULL;
memset(a_category->level_bitmap_backup, 0x00,
sizeof(a_category->level_bitmap_backup));
return;
}
/* rollback fail: fit_rules_backup != 0 */
/* rollback success: fit_rules 1, fit_rules_backup 0 */
/* so whether update succes or not, make things back to old */
void zlog_category_rollback_rules(zlog_category_t * a_category)
{
zc_assert(a_category,);
if (!a_category->fit_rules_backup) {
zc_warn("a_category->fit_rules_backup in NULL, never update before");
return;
}
if (a_category->fit_rules) {
/* update success, rm new and backup */
zc_arraylist_del(a_category->fit_rules);
a_category->fit_rules = a_category->fit_rules_backup;
a_category->fit_rules_backup = NULL;
} else {
/* update fail, just backup */
a_category->fit_rules = a_category->fit_rules_backup;
a_category->fit_rules_backup = NULL;
}
memcpy(a_category->level_bitmap, a_category->level_bitmap_backup,
sizeof(a_category->level_bitmap));
memset(a_category->level_bitmap_backup, 0x00,
sizeof(a_category->level_bitmap_backup));
return; /* always success */
}
/*******************************************************************************/
int zlog_category_output(zlog_category_t * a_category, zlog_thread_t * a_thread)
{
int i;
int rc = 0;
zlog_rule_t *a_rule;
/* go through all match rules to output */
zc_arraylist_foreach(a_category->fit_rules, i, a_rule) {
rc = zlog_rule_output(a_rule, a_thread);
}
return rc;
}
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_category_h
#define __zlog_category_h
#include "zc_defs.h"
#include "thread.h"
typedef struct zlog_category_s {
char name[MAXLEN_PATH + 1];
size_t name_len;
unsigned char level_bitmap[32];
unsigned char level_bitmap_backup[32];
zc_arraylist_t *fit_rules;
zc_arraylist_t *fit_rules_backup;
} zlog_category_t;
zlog_category_t *zlog_category_new(const char *name, zc_arraylist_t * rules);
void zlog_category_del(zlog_category_t * a_category);
void zlog_category_profile(zlog_category_t *a_category, int flag);
int zlog_category_update_rules(zlog_category_t * a_category, zc_arraylist_t * new_rules);
void zlog_category_commit_rules(zlog_category_t * a_category);
void zlog_category_rollback_rules(zlog_category_t * a_category);
int zlog_category_output(zlog_category_t * a_category, zlog_thread_t * a_thread);
#define zlog_category_needless_level(a_category, lv) \
a_category && !((a_category->level_bitmap[lv/8] >> (7 - lv % 8)) & 0x01)
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "zc_defs.h"
#include "category_table.h"
void zlog_category_table_profile(zc_hashtable_t * categories, int flag)
{
zc_hashtable_entry_t *a_entry;
zlog_category_t *a_category;
zc_assert(categories,);
zc_profile(flag, "-category_table[%p]-", categories);
zc_hashtable_foreach(categories, a_entry) {
a_category = (zlog_category_t *) a_entry->value;
zlog_category_profile(a_category, flag);
}
return;
}
/*******************************************************************************/
void zlog_category_table_del(zc_hashtable_t * categories)
{
zc_assert(categories,);
zc_hashtable_del(categories);
zc_debug("zlog_category_table_del[%p]", categories);
return;
}
zc_hashtable_t *zlog_category_table_new(void)
{
zc_hashtable_t *categories;
categories = zc_hashtable_new(20,
(zc_hashtable_hash_fn) zc_hashtable_str_hash,
(zc_hashtable_equal_fn) zc_hashtable_str_equal,
NULL, (zc_hashtable_del_fn) zlog_category_del);
if (!categories) {
zc_error("zc_hashtable_new fail");
return NULL;
} else {
zlog_category_table_profile(categories, ZC_DEBUG);
return categories;
}
}
/*******************************************************************************/
int zlog_category_table_update_rules(zc_hashtable_t * categories, zc_arraylist_t * new_rules)
{
zc_hashtable_entry_t *a_entry;
zlog_category_t *a_category;
zc_assert(categories, -1);
zc_hashtable_foreach(categories, a_entry) {
a_category = (zlog_category_t *) a_entry->value;
if (zlog_category_update_rules(a_category, new_rules)) {
zc_error("zlog_category_update_rules fail, try rollback");
return -1;
}
}
return 0;
}
void zlog_category_table_commit_rules(zc_hashtable_t * categories)
{
zc_hashtable_entry_t *a_entry;
zlog_category_t *a_category;
zc_assert(categories,);
zc_hashtable_foreach(categories, a_entry) {
a_category = (zlog_category_t *) a_entry->value;
zlog_category_commit_rules(a_category);
}
return;
}
void zlog_category_table_rollback_rules(zc_hashtable_t * categories)
{
zc_hashtable_entry_t *a_entry;
zlog_category_t *a_category;
zc_assert(categories,);
zc_hashtable_foreach(categories, a_entry) {
a_category = (zlog_category_t *) a_entry->value;
zlog_category_rollback_rules(a_category);
}
return;
}
/*******************************************************************************/
zlog_category_t *zlog_category_table_fetch_category(zc_hashtable_t * categories,
const char *category_name, zc_arraylist_t * rules)
{
zlog_category_t *a_category;
zc_assert(categories, NULL);
/* 1st find category in global category map */
a_category = zc_hashtable_get(categories, category_name);
if (a_category) return a_category;
/* else not found, create one */
a_category = zlog_category_new(category_name, rules);
if (!a_category) {
zc_error("zc_category_new fail");
return NULL;
}
if(zc_hashtable_put(categories, a_category->name, a_category)) {
zc_error("zc_hashtable_put fail");
goto err;
}
return a_category;
err:
zlog_category_del(a_category);
return NULL;
}
/*******************************************************************************/
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_category_table_h
#define __zlog_category_table_h
#include "zc_defs.h"
#include "category.h"
zc_hashtable_t *zlog_category_table_new(void);
void zlog_category_table_del(zc_hashtable_t * categories);
void zlog_category_table_profile(zc_hashtable_t * categories, int flag);
/* if none, create new and return */
zlog_category_t *zlog_category_table_fetch_category(
zc_hashtable_t * categories,
const char *category_name, zc_arraylist_t * rules);
int zlog_category_table_update_rules(zc_hashtable_t * categories, zc_arraylist_t * new_rules);
void zlog_category_table_commit_rules(zc_hashtable_t * categories);
void zlog_category_table_rollback_rules(zc_hashtable_t * categories);
#endif
This diff is collapsed.
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_conf_h
#define __zlog_conf_h
#include "zc_defs.h"
#include "format.h"
#include "rotater.h"
typedef struct zlog_conf_s {
char file[MAXLEN_PATH + 1];
char cfg_ptr[MAXLEN_CFG_LINE*MAXLINES_NO];
char mtime[20 + 1];
int strict_init;
size_t buf_size_min;
size_t buf_size_max;
char rotate_lock_file[MAXLEN_CFG_LINE + 1];
zlog_rotater_t *rotater;
char default_format_line[MAXLEN_CFG_LINE + 1];
zlog_format_t *default_format;
unsigned int file_perms;
size_t fsync_period;
size_t reload_conf_period;
zc_arraylist_t *levels;
zc_arraylist_t *formats;
zc_arraylist_t *rules;
int time_cache_count;
} zlog_conf_t;
extern zlog_conf_t * zlog_env_conf;
zlog_conf_t *zlog_conf_new(const char *config);
void zlog_conf_del(zlog_conf_t * a_conf);
void zlog_conf_profile(zlog_conf_t * a_conf, int flag);
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#define _GNU_SOURCE // For distros like Centos for syscall interface
#include "fmacros.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include "zc_defs.h"
#include "event.h"
void zlog_event_profile(zlog_event_t * a_event, int flag)
{
zc_assert(a_event,);
zc_profile(flag, "---event[%p][%s,%s][%s(%ld),%s(%ld),%ld,%d][%p,%s][%ld,%ld][%ld,%ld][%d]---",
a_event,
a_event->category_name, a_event->host_name,
a_event->file, a_event->file_len,
a_event->func, a_event->func_len,
a_event->line, a_event->level,
a_event->hex_buf, a_event->str_format,
a_event->time_stamp.tv_sec, a_event->time_stamp.tv_usec,
(long)a_event->pid, (long)a_event->tid,
a_event->time_cache_count);
return;
}
/*******************************************************************************/
void zlog_event_del(zlog_event_t * a_event)
{
zc_assert(a_event,);
if (a_event->time_caches) free(a_event->time_caches);
zc_debug("zlog_event_del[%p]", a_event);
free(a_event);
return;
}
zlog_event_t *zlog_event_new(int time_cache_count)
{
zlog_event_t *a_event;
a_event = calloc(1, sizeof(zlog_event_t));
if (!a_event) {
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
a_event->time_caches = calloc(time_cache_count, sizeof(zlog_time_cache_t));
if (!a_event->time_caches) {
zc_error("calloc fail, errno[%d]", errno);
free(a_event);
return NULL;
}
a_event->time_cache_count = time_cache_count;
/*
* at the zlog_init we gethostname,
* u don't always change your hostname, eh?
*/
if (gethostname(a_event->host_name, sizeof(a_event->host_name) - 1)) {
zc_error("gethostname fail, errno[%d]", errno);
goto err;
}
a_event->host_name_len = strlen(a_event->host_name);
/* tid is bound to a_event
* as in whole lifecycle event persists
* even fork to oth pid, tid not change
*/
a_event->tid = pthread_self();
a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)a_event->tid);
a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "%x", (unsigned int)a_event->tid);
#ifdef __linux__
a_event->ktid = syscall(SYS_gettid);
#elif __APPLE__
uint64_t tid64;
pthread_threadid_np(NULL, &tid64);
a_event->tid = (pid_t)tid64;
#endif
#if defined __linux__ || __APPLE__
a_event->ktid_str_len = sprintf(a_event->ktid_str, "%u", (unsigned int)a_event->ktid);
#endif
//zlog_event_profile(a_event, ZC_DEBUG);
return a_event;
err:
zlog_event_del(a_event);
return NULL;
}
/*******************************************************************************/
void zlog_event_set_fmt(zlog_event_t * a_event,
char *category_name, size_t category_name_len,
const char *file, size_t file_len, const char *func, size_t func_len, long line, int level,
const char *str_format, va_list str_args)
{
/*
* category_name point to zlog_category_output's category.name
*/
a_event->category_name = category_name;
a_event->category_name_len = category_name_len;
a_event->file = (char *) file;
a_event->file_len = file_len;
a_event->func = (char *) func;
a_event->func_len = func_len;
a_event->line = line;
a_event->level = level;
a_event->generate_cmd = ZLOG_FMT;
a_event->str_format = str_format;
va_copy(a_event->str_args, str_args);
/* pid should fetch eveytime, as no one knows,
* when does user fork his process
* so clean here, and fetch at spec.c
*/
a_event->pid = (pid_t) 0;
/* in a event's life cycle, time will be get when spec need,
* and keep unchange though all event's life cycle
* zlog_spec_write_time gettimeofday
*/
a_event->time_stamp.tv_sec = 0;
return;
}
void zlog_event_set_hex(zlog_event_t * a_event,
char *category_name, size_t category_name_len,
const char *file, size_t file_len, const char *func, size_t func_len, long line, int level,
const void *hex_buf, size_t hex_buf_len)
{
/*
* category_name point to zlog_category_output's category.name
*/
a_event->category_name = category_name;
a_event->category_name_len = category_name_len;
a_event->file = (char *) file;
a_event->file_len = file_len;
a_event->func = (char *) func;
a_event->func_len = func_len;
a_event->line = line;
a_event->level = level;
a_event->generate_cmd = ZLOG_HEX;
a_event->hex_buf = hex_buf;
a_event->hex_buf_len = hex_buf_len;
/* pid should fetch eveytime, as no one knows,
* when does user fork his process
* so clean here, and fetch at spec.c
*/
a_event->pid = (pid_t) 0;
/* in a event's life cycle, time will be get when spec need,
* and keep unchange though all event's life cycle
*/
a_event->time_stamp.tv_sec = 0;
return;
}
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_event_h
#define __zlog_event_h
#include <sys/types.h> /* for pid_t */
#include <sys/time.h> /* for struct timeval */
#include <pthread.h> /* for pthread_t */
#include <stdarg.h> /* for va_list */
#include "zc_defs.h"
typedef enum {
ZLOG_FMT = 0,
ZLOG_HEX = 1,
} zlog_event_cmd;
typedef struct zlog_time_cache_s {
char str[MAXLEN_CFG_LINE + 1];
size_t len;
time_t sec;
} zlog_time_cache_t;
typedef struct {
char *category_name;
size_t category_name_len;
char host_name[256 + 1];
size_t host_name_len;
const char *file;
size_t file_len;
const char *func;
size_t func_len;
long line;
int level;
const void *hex_buf;
size_t hex_buf_len;
const char *str_format;
va_list str_args;
zlog_event_cmd generate_cmd;
struct timeval time_stamp;
time_t time_local_sec;
struct tm time_local;
zlog_time_cache_t *time_caches;
int time_cache_count;
pid_t pid;
pid_t last_pid;
char pid_str[30 + 1];
size_t pid_str_len;
pthread_t tid;
char tid_str[30 + 1];
size_t tid_str_len;
char tid_hex_str[30 + 1];
size_t tid_hex_str_len;
#if defined __linux__ || __APPLE__
pid_t ktid;
char ktid_str[30+1];
size_t ktid_str_len;
#endif
} zlog_event_t;
zlog_event_t *zlog_event_new(int time_cache_count);
void zlog_event_del(zlog_event_t * a_event);
void zlog_event_profile(zlog_event_t * a_event, int flag);
void zlog_event_set_fmt(zlog_event_t * a_event,
char *category_name, size_t category_name_len,
const char *file, size_t file_len, const char *func, size_t func_len, long line, int level,
const char *str_format, va_list str_args);
void zlog_event_set_hex(zlog_event_t * a_event,
char *category_name, size_t category_name_len,
const char *file, size_t file_len, const char *func, size_t func_len, long line, int level,
const void *hex_buf, size_t hex_buf_len);
#endif
#ifndef __zlog_fmacro_h
#define __zlog_fmacro_h
#define _DEFAULT_SOURCE
#if defined(__linux__) || defined(__OpenBSD__) || defined(_AIX)
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED
#endif
#else
#define _XOPEN_SOURCE
#endif
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "zc_defs.h"
#include "thread.h"
#include "spec.h"
#include "format.h"
void zlog_format_profile(zlog_format_t * a_format, int flag)
{
zc_assert(a_format,);
zc_profile(flag, "---format[%p][%s = %s(%p)]---",
a_format,
a_format->name,
a_format->pattern,
a_format->pattern_specs);
#if 0
int i;
zlog_spec_t *a_spec;
zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) {
zlog_spec_profile(a_spec, flag);
}
#endif
return;
}
/*******************************************************************************/
void zlog_format_del(zlog_format_t * a_format)
{
zc_assert(a_format,);
if (a_format->pattern_specs) {
zc_arraylist_del(a_format->pattern_specs);
}
zc_debug("zlog_format_del[%p]", a_format);
free(a_format);
return;
}
zlog_format_t *zlog_format_new(char *line, int * time_cache_count)
{
int nscan = 0;
zlog_format_t *a_format = NULL;
int nread = 0;
const char *p_start;
const char *p_end;
char *p;
char *q;
zlog_spec_t *a_spec;
zc_assert(line, NULL);
a_format = calloc(1, sizeof(zlog_format_t));
if (!a_format) {
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
/* line default = "%d(%F %X.%l) %-6V (%c:%F:%L) - %m%n"
* name default
* pattern %d(%F %X.%l) %-6V (%c:%F:%L) - %m%n
*/
memset(a_format->name, 0x00, sizeof(a_format->name));
nread = 0;
nscan = sscanf(line, " %[^= \t] = %n", a_format->name, &nread);
if (nscan != 1) {
zc_error("format[%s], syntax wrong", line);
goto err;
}
if (*(line + nread) != '"') {
zc_error("the 1st char of pattern is not \", line+nread[%s]", line+nread);
goto err;
}
for (p = a_format->name; *p != '\0'; p++) {
if ((!isalnum(*p)) && (*p != '_')) {
zc_error("a_format->name[%s] character is not in [a-Z][0-9][_]", a_format->name);
goto err;
}
}
p_start = line + nread + 1;
p_end = strrchr(p_start, '"');
if (!p_end) {
zc_error("there is no \" at end of pattern, line[%s]", line);
goto err;
}
if (p_end - p_start > sizeof(a_format->pattern) - 1) {
zc_error("pattern is too long");
goto err;
}
memset(a_format->pattern, 0x00, sizeof(a_format->pattern));
memcpy(a_format->pattern, p_start, p_end - p_start);
if (zc_str_replace_env(a_format->pattern, sizeof(a_format->pattern))) {
zc_error("zc_str_replace_env fail");
goto err;
}
a_format->pattern_specs =
zc_arraylist_new((zc_arraylist_del_fn) zlog_spec_del);
if (!(a_format->pattern_specs)) {
zc_error("zc_arraylist_new fail");
goto err;
}
for (p = a_format->pattern; *p != '\0'; p = q) {
a_spec = zlog_spec_new(p, &q, time_cache_count);
if (!a_spec) {
zc_error("zlog_spec_new fail");
goto err;
}
if (zc_arraylist_add(a_format->pattern_specs, a_spec)) {
zlog_spec_del(a_spec);
zc_error("zc_arraylist_add fail");
goto err;
}
}
zlog_format_profile(a_format, ZC_DEBUG);
return a_format;
err:
zlog_format_del(a_format);
return NULL;
}
/*******************************************************************************/
/* return 0 success, or buf is full
* return -1 fail
*/
int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread)
{
int i;
zlog_spec_t *a_spec;
zlog_buf_restart(a_thread->msg_buf);
zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) {
if (zlog_spec_gen_msg(a_spec, a_thread) == 0) {
continue;
} else {
return -1;
}
}
return 0;
}
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_format_h
#define __zlog_format_h
#include "thread.h"
#include "zc_defs.h"
typedef struct zlog_format_s zlog_format_t;
struct zlog_format_s {
char name[MAXLEN_CFG_LINE + 1];
char pattern[MAXLEN_CFG_LINE + 1];
zc_arraylist_t *pattern_specs;
};
zlog_format_t *zlog_format_new(char *line, int * time_cache_count);
void zlog_format_del(zlog_format_t * a_format);
void zlog_format_profile(zlog_format_t * a_format, int flag);
int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread);
#define zlog_format_has_name(a_format, fname) \
STRCMP(a_format->name, ==, fname)
#endif
LIBSO_TARGET := libzlog.so
#LIBA_TARGET := libzlog.a
#include "kk_log.h"
zlog_category_t *g_zlogC;
int kk_zlog_init(char* module)
{
int rc;
rc = zlog_init("zlog.conf");
if (rc) {
printf(" kk_zlog_init init failed\n");
return -1;
}
g_zlogC = zlog_get_category(module);
if (!g_zlogC) {
printf("kk_zlog_init get cat fail\n");
zlog_fini();
return -2;
}
//zlog_info(c, "hello, zlog");
//zlog_debug(c, "debug hello, zlog");
//zlog_fini();
return 0;
}
#ifndef _LOGDEF_H_
#define _LOGDEF_H_
#include "zlog.h"
extern zlog_category_t *g_zlogC;
#define DEBUG_PRINT(info,...) zlog_debug(g_zlogC, info, ##__VA_ARGS__)
#define INFO_PRINT(info,...) zlog_info(g_zlogC, info, ##__VA_ARGS__)
#define ERROR_PRINT(info,...) zlog_error(g_zlogC, info, ##__VA_ARGS__)
#define WARNING_PRINT(info,...) zlog_warn(g_zlogC, info, ##__VA_ARGS__)
int kk_zlog_init(char *module);
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "syslog.h"
#include "zc_defs.h"
#include "level.h"
void zlog_level_profile(zlog_level_t *a_level, int flag)
{
zc_assert(a_level,);
zc_profile(flag, "---level[%p][%d,%s,%s,%d,%d]---",
a_level,
a_level->int_level,
a_level->str_uppercase,
a_level->str_lowercase,
(int) a_level->str_len,
a_level->syslog_level);
return;
}
/*******************************************************************************/
void zlog_level_del(zlog_level_t *a_level)
{
zc_assert(a_level,);
zc_debug("zlog_level_del[%p]", a_level);
free(a_level);
return;
}
static int syslog_level_atoi(char *str)
{
/* guess no unix system will choose -187
* as its syslog level, so it is a safe return value
*/
zc_assert(str, -187);
if (STRICMP(str, ==, "LOG_EMERG"))
return LOG_EMERG;
if (STRICMP(str, ==, "LOG_ALERT"))
return LOG_ALERT;
if (STRICMP(str, ==, "LOG_CRIT"))
return LOG_CRIT;
if (STRICMP(str, ==, "LOG_ERR"))
return LOG_ERR;
if (STRICMP(str, ==, "LOG_WARNING"))
return LOG_WARNING;
if (STRICMP(str, ==, "LOG_NOTICE"))
return LOG_NOTICE;
if (STRICMP(str, ==, "LOG_INFO"))
return LOG_INFO;
if (STRICMP(str, ==, "LOG_DEBUG"))
return LOG_DEBUG;
zc_error("wrong syslog level[%s]", str);
return -187;
}
/* line: TRACE = 10, LOG_ERR */
zlog_level_t *zlog_level_new(char *line)
{
zlog_level_t *a_level = NULL;
int i;
int nscan;
char str[MAXLEN_CFG_LINE + 1];
int l = 0;
char sl[MAXLEN_CFG_LINE + 1];
zc_assert(line, NULL);
memset(str, 0x00, sizeof(str));
memset(sl, 0x00, sizeof(sl));
nscan = sscanf(line, " %[^= \t] = %d ,%s", str, &l, sl);
if (nscan < 2) {
zc_error("level[%s], syntax wrong", line);
return NULL;
}
/* check level and str */
if ((l < 0) || (l > 255)) {
zc_error("l[%d] not in [0,255], wrong", l);
return NULL;
}
if (str[0] == '\0') {
zc_error("str[0] = 0");
return NULL;
}
a_level = calloc(1, sizeof(zlog_level_t));
if (!a_level) {
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
a_level->int_level = l;
/* fill syslog level */
if (sl[0] == '\0') {
a_level->syslog_level = LOG_DEBUG;
} else {
a_level->syslog_level = syslog_level_atoi(sl);
if (a_level->syslog_level == -187) {
zc_error("syslog_level_atoi fail");
goto err;
}
}
/* strncpy and toupper(str) */
for (i = 0; (i < sizeof(a_level->str_uppercase) - 1) && str[i] != '\0'; i++) {
(a_level->str_uppercase)[i] = toupper(str[i]);
(a_level->str_lowercase)[i] = tolower(str[i]);
}
if (str[i] != '\0') {
/* overflow */
zc_error("not enough space for str, str[%s] > %d", str, i);
goto err;
} else {
(a_level->str_uppercase)[i] = '\0';
(a_level->str_lowercase)[i] = '\0';
}
a_level->str_len = i;
//zlog_level_profile(a_level, ZC_DEBUG);
return a_level;
err:
zc_error("line[%s]", line);
zlog_level_del(a_level);
return NULL;
}
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_level_h
#define __zlog_level_h
#include "zc_defs.h"
typedef struct zlog_level_s {
int int_level;
char str_uppercase[MAXLEN_PATH + 1];
char str_lowercase[MAXLEN_PATH + 1];
size_t str_len;
int syslog_level;
} zlog_level_t;
zlog_level_t *zlog_level_new(char *line);
void zlog_level_del(zlog_level_t *a_level);
void zlog_level_profile(zlog_level_t *a_level, int flag);
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "syslog.h"
#include "zc_defs.h"
#include "level.h"
#include "level_list.h"
/* zlog_level_list == zc_arraylist_t<zlog_level_t> */
void zlog_level_list_profile(zc_arraylist_t *levels, int flag)
{
int i;
zlog_level_t *a_level;
zc_assert(levels,);
zc_profile(flag, "--level_list[%p]--", levels);
zc_arraylist_foreach(levels, i, a_level) {
/* skip empty slots */
if (a_level) zlog_level_profile(a_level, flag);
}
return;
}
/*******************************************************************************/
void zlog_level_list_del(zc_arraylist_t *levels)
{
zc_assert(levels,);
zc_arraylist_del(levels);
zc_debug("zc_level_list_del[%p]", levels);
return;
}
static int zlog_level_list_set_default(zc_arraylist_t *levels)
{
return zlog_level_list_set(levels, "* = 0, LOG_INFO")
|| zlog_level_list_set(levels, "DEBUG = 20, LOG_DEBUG")
|| zlog_level_list_set(levels, "INFO = 40, LOG_INFO")
|| zlog_level_list_set(levels, "NOTICE = 60, LOG_NOTICE")
|| zlog_level_list_set(levels, "WARN = 80, LOG_WARNING")
|| zlog_level_list_set(levels, "ERROR = 100, LOG_ERR")
|| zlog_level_list_set(levels, "FATAL = 120, LOG_ALERT")
|| zlog_level_list_set(levels, "UNKNOWN = 254, LOG_ERR")
|| zlog_level_list_set(levels, "! = 255, LOG_INFO");
}
zc_arraylist_t *zlog_level_list_new(void)
{
zc_arraylist_t *levels;
levels = zc_arraylist_new((zc_arraylist_del_fn)zlog_level_del);
if (!levels) {
zc_error("zc_arraylist_new fail");
return NULL;
}
if (zlog_level_list_set_default(levels)) {
zc_error("zlog_level_set_default fail");
goto err;
}
//zlog_level_list_profile(levels, ZC_DEBUG);
return levels;
err:
zc_arraylist_del(levels);
return NULL;
}
/*******************************************************************************/
int zlog_level_list_set(zc_arraylist_t *levels, char *line)
{
zlog_level_t *a_level;
a_level = zlog_level_new(line);
if (!a_level) {
zc_error("zlog_level_new fail");
return -1;
}
if (zc_arraylist_set(levels, a_level->int_level, a_level)) {
zc_error("zc_arraylist_set fail");
goto err;
}
return 0;
err:
zc_error("line[%s]", line);
zlog_level_del(a_level);
return -1;
}
zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l)
{
zlog_level_t *a_level;
#if 0
if ((l <= 0) || (l > 254)) {
/* illegal input from zlog() */
zc_error("l[%d] not in (0,254), set to UNKOWN", l);
l = 254;
}
#endif
a_level = zc_arraylist_get(levels, l);
if (a_level) {
return a_level;
} else {
/* empty slot */
zc_error("l[%d] not in (0,254), or has no level defined,"
"see configure file define, set to UNKOWN", l);
return zc_arraylist_get(levels, 254);
}
}
/*******************************************************************************/
int zlog_level_list_atoi(zc_arraylist_t *levels, char *str)
{
int i;
zlog_level_t *a_level;
if (str == NULL || *str == '\0') {
zc_error("str is [%s], can't find level", str);
return -1;
}
zc_arraylist_foreach(levels, i, a_level) {
if (a_level && STRICMP(str, ==, a_level->str_uppercase)) {
return i;
}
}
zc_error("str[%s] can't found in level list", str);
return -1;
}
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_level_list_h
#define __zlog_level_list_h
#include "zc_defs.h"
#include "level.h"
zc_arraylist_t *zlog_level_list_new(void);
void zlog_level_list_del(zc_arraylist_t *levels);
void zlog_level_list_profile(zc_arraylist_t *levels, int flag);
/* conf init use, slow */
/* if l is wrong or str=="", return -1 */
int zlog_level_list_set(zc_arraylist_t *levels, char *line);
/* spec ouput use, fast */
/* rule output use, fast */
/* if not found, return levels[254] */
zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l);
/* rule init use, slow */
/* if not found, return -1 */
int zlog_level_list_atoi(zc_arraylist_t *levels, char *str);
#endif
This diff is collapsed.
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#ifndef __zlog_mdc_h
#define __zlog_mdc_h
#include "zc_defs.h"
typedef struct zlog_mdc_s zlog_mdc_t;
struct zlog_mdc_s {
zc_hashtable_t *tab;
};
zlog_mdc_t *zlog_mdc_new(void);
void zlog_mdc_del(zlog_mdc_t * a_mdc);
void zlog_mdc_profile(zlog_mdc_t *a_mdc, int flag);
void zlog_mdc_clean(zlog_mdc_t * a_mdc);
int zlog_mdc_put(zlog_mdc_t * a_mdc, const char *key, const char *value);
char *zlog_mdc_get(zlog_mdc_t * a_mdc, const char *key);
void zlog_mdc_remove(zlog_mdc_t * a_mdc, const char *key);
typedef struct zlog_mdc_kv_s {
char key[MAXLEN_PATH + 1];
char value[MAXLEN_PATH + 1];
size_t value_len;
} zlog_mdc_kv_t;
zlog_mdc_kv_t *zlog_mdc_get_kv(zlog_mdc_t * a_mdc, const char *key);
#endif
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
*
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/
#include "errno.h"
#include "zc_defs.h"
#include "record.h"
void zlog_record_profile(zlog_record_t *a_record, int flag)
{
zc_assert(a_record,);
zc_profile(flag, "--record:[%p][%s:%p]--", a_record, a_record->name, a_record->output);
return;
}
void zlog_record_del(zlog_record_t *a_record)
{
zc_assert(a_record,);
zc_debug("zlog_record_del[%p]", a_record);
free(a_record);
return;
}
zlog_record_t *zlog_record_new(const char *name, zlog_record_fn output)
{
zlog_record_t *a_record;
zc_assert(name, NULL);
zc_assert(output, NULL);
a_record = calloc(1, sizeof(zlog_record_t));
if (!a_record) {
zc_error("calloc fail, errno[%d]", errno);
return NULL;
}
if (strlen(name) > sizeof(a_record->name) - 1) {
zc_error("name[%s] is too long", name);
goto err;
}
strcpy(a_record->name, name);
a_record->output = output;
zlog_record_profile(a_record, ZC_DEBUG);
return a_record;
err:
zlog_record_del(a_record);
return NULL;
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#define ZLOG_VERSION "1.2.12"
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment