Skip to content

Commit

Permalink
* NEW [log] enable log in sdk for neuron
Browse files Browse the repository at this point in the history
Signed-off-by: wayne <[email protected]>
  • Loading branch information
StargazerWayne committed Dec 29, 2023
1 parent 19e601c commit 0a4963a
Show file tree
Hide file tree
Showing 9 changed files with 766 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ if (NOT (BUILD_SHARED_LIBS))
message(STATUS "Building static libs.")
endif ()

# log is enabled in default
if (NOT NOLOG)
add_definitions(-DENABLE_LOG)
endif ()

# These are library targets. The "nng" library is the main public library.
# The "nng_testing" is a full build of the library for test cases
# only, which is done statically and includes even portions of the code
Expand Down
26 changes: 26 additions & 0 deletions include/nng/supplemental/nanolib/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

//
// Copyright 2023 NanoMQ Team, Inc. <[email protected]> //
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#ifndef NANO_FILE_H
#define NANO_FILE_H

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "nng/nng.h"

NNG_DECL bool nano_file_exists(const char *fpath);
NNG_DECL char * nano_getcwd(char *buf, size_t size);
NNG_DECL int64_t nano_getline(
char **restrict line, size_t *restrict len, FILE *restrict fp);
NNG_DECL char * nano_concat_path(const char *dir, const char *file_name);
NNG_DECL int file_write_string(const char *fpath, const char *string);
NNG_DECL size_t file_load_data(const char *filepath, void **data);

#endif
120 changes: 120 additions & 0 deletions include/nng/supplemental/nanolib/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//
// Copyright 2023 NanoMQ Team, Inc. <[email protected]> //
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#ifndef NNG_NANOLIB_LOG_H
#define NNG_NANOLIB_LOG_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>

#include "nng/nng.h"

#define LOG_VERSION "0.2.1"

/**
* 2023-12-08 move conf_log to log.h
*/

// log type
#define LOG_TO_FILE (1 << 0)
#define LOG_TO_CONSOLE (1 << 1)
#define LOG_TO_SYSLOG (1 << 2)

typedef struct conf_log conf_log;
struct conf_log {
uint8_t type;
int level;
char *dir;
char *file;
FILE *fp;
char *abs_path; // absolut path of log file
char *rotation_sz_str; // 1000KB, 100MB, 10GB
uint64_t rotation_sz; // unit: byte
size_t rotation_count; // rotation count
};

typedef struct {
va_list ap;
const char *fmt;
const char *file;
const char *func;
struct tm time;
void * udata;
int line;
int level;
conf_log * config;
} log_event;

typedef void (*log_func)(log_event *ev);

enum {
NNG_LOG_FATAL = 0,
NNG_LOG_ERROR,
NNG_LOG_WARN,
NNG_LOG_INFO,
NNG_LOG_DEBUG,
NNG_LOG_TRACE,
};

NNG_DECL const char *log_level_string(int level);
NNG_DECL int log_level_num(const char *level);
NNG_DECL void log_set_level(int level);
NNG_DECL int log_add_callback(
log_func fn, void *udata, int level, void *mtx, conf_log *config);
NNG_DECL void log_add_console(int level, void *mtx);
NNG_DECL int log_add_fp(FILE *fp, int level, void *mtx, conf_log *config);
NNG_DECL void log_add_syslog(const char *log_name, uint8_t level, void *mtx);
NNG_DECL void log_log(int level, const char *file, int line, const char *func,
const char *fmt, ...);
NNG_DECL void log_clear_callback();

// level: check enum above
// type: 2--> console, 1-->file, 3--> console & file
NNG_DECL int conf_log_init(int level, int type, char *dir, char *file, uint64_t rotation_sz, size_t rotation_count);
NNG_DECL int conf_log_fini();

#ifdef ENABLE_LOG

#define log_trace(...) \
log_log(NNG_LOG_TRACE, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define log_debug(...) \
log_log(NNG_LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define log_info(...) \
log_log(NNG_LOG_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define log_warn(...) \
log_log(NNG_LOG_WARN, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define log_error(...) \
log_log(NNG_LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define log_fatal(...) \
log_log(NNG_LOG_FATAL, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)

#else

#define log_trace(...)
#define log_debug(...)
#define log_info(...)
#define log_warn(...)
#define log_error(...)
#define log_fatal(...)

#endif


#ifdef __cplusplus
}
#endif

#endif
6 changes: 6 additions & 0 deletions src/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,15 @@ extern char *nni_plat_join_dir(const char *, const char *);
// string, and may not be altered.
extern const char *nni_plat_file_basename(const char *);

// nni_plat_file_exists return if file is exists.
extern bool nni_plat_file_exists(const char *);

// nni_plat_getcwd get current directory where program is running.
extern char *nni_plat_getcwd(char *, size_t );

// nni_plat_file_size get file size.
extern int nni_plat_file_size(const char *, size_t *);

// NanoSDK
// Get standard/UNIX timestamp
extern nni_time nni_timestamp(void);
Expand Down
12 changes: 12 additions & 0 deletions src/mqtt/transport/tcp/mqtt_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "core/nng_impl.h"
#include "core/sockimpl.h"
#include "nng/mqtt/mqtt_client.h"
#include "nng/supplemental/nanolib/log.h"
#include "supplemental/mqtt/mqtt_msg.h"

// TCP transport. Platform specific TCP operations must be
Expand Down Expand Up @@ -911,6 +912,17 @@ mqtt_tcptran_pipe_send_start(mqtt_tcptran_pipe *p)
iov[niov].iov_len = nni_msg_len(msg);
niov++;
}
int msg_body_len = 30 < nni_msg_len(msg) ? 30 : nni_msg_len(msg);

char *data = iov[0].iov_buf;
for (int i = 0; i < nni_msg_header_len(msg);++i) {
log_debug("msg header %d: %x", i, data[i]);
}

data = iov[1].iov_buf;
for (int i = 0; i < msg_body_len; ++i) {
log_debug("msg body %d: %x", i, data[i]);
}
nni_aio_set_iov(txaio, niov, iov);
nng_stream_send(p->conn, txaio);
}
Expand Down
3 changes: 2 additions & 1 deletion src/supplemental/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ add_subdirectory(util)
add_subdirectory(websocket)
add_subdirectory(mqtt)
add_subdirectory(sqlite)
add_subdirectory(quic)
add_subdirectory(quic)
add_subdirectory(nanolib)
10 changes: 10 additions & 0 deletions src/supplemental/nanolib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright 2023 NanoMQ Team, Inc. <[email protected]> //
# This software is supplied under the terms of the MIT License, a
# copy of which should be located in the distribution where this
# file was obtained (LICENSE.txt). A copy of the license may also be
# found online at https://opensource.org/licenses/MIT.
#

nng_sources(file.c log.c)
nng_headers(nng/supplemental/nanolib/file.h nng/supplemental/nanolib/log.h)
157 changes: 157 additions & 0 deletions src/supplemental/nanolib/file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//
// Copyright 2023 NanoMQ Team, Inc. <[email protected]> //
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "core/nng_impl.h"
#include "nng/supplemental/nanolib/file.h"

#ifdef NNG_PLATFORM_WINDOWS
#define nano_mkdir(path, mode) mkdir(path)
#else
#define nano_mkdir(path, mode) mkdir(path, mode)
#endif

#ifndef NNG_PLATFORM_WINDOWS

int64_t
nano_getline(char **restrict line, size_t *restrict len, FILE *restrict fp)
{
return getline(line, len, fp);
}

#else

int64_t
nano_getline(char **restrict line, size_t *restrict len, FILE *restrict fp)
{
// Check if either line, len or fp are NULL pointers
if (line == NULL || len == NULL || fp == NULL) {
errno = EINVAL;
return -1;
}

// Use a chunk array of 128 bytes as parameter for fgets
char chunk[128];

// Allocate a block of memory for *line if it is NULL or smaller than
// the chunk array
if (*line == NULL || *len < sizeof(chunk)) {
*len = sizeof(chunk);
if ((*line = malloc(*len)) == NULL) {
errno = ENOMEM;
return -1;
}
}

// "Empty" the string
(*line)[0] = '\0';

while (fgets(chunk, sizeof(chunk), fp) != NULL) {
// Resize the line buffer if necessary
size_t len_used = strlen(*line);
size_t chunk_used = strlen(chunk);

if (*len - len_used < chunk_used) {
// Check for overflow
if (*len > SIZE_MAX / 2) {
errno = EOVERFLOW;
return -1;
} else {
*len *= 2;
}

if ((*line = realloc(*line, *len)) == NULL) {
errno = ENOMEM;
return -1;
}
}

// Copy the chunk to the end of the line buffer
memcpy(*line + len_used, chunk, chunk_used);
len_used += chunk_used;
(*line)[len_used] = '\0';

// Check if *line contains '\n', if yes, return the *line
// length
if ((*line)[len_used - 1] == '\n') {
return len_used;
}
}

return -1;
}

#endif

/*return true if exists*/
bool
nano_file_exists(const char *fpath)
{
return nni_plat_file_exists(fpath);
}

char *
nano_getcwd(char *buf, size_t size)
{
return nni_plat_getcwd(buf, size);
}

int
file_write_string(const char *fpath, const char *string)
{
return nni_plat_file_put(fpath, string, strlen(string));
}

size_t
file_load_data(const char *filepath, void **data)
{
size_t size;

if (nni_plat_file_get(filepath, data, &size) != 0) {
return 0;
}
size++;
uint8_t *buf = *data;
buf = realloc(buf, size);
buf[size - 1] = '\0';
*data = buf;
return size;
}

char *
nano_concat_path(const char *dir, const char *file_name)
{
if (file_name == NULL) {
return NULL;
}

#if defined(NNG_PLATFORM_WINDOWS)
char *directory = dir == NULL ? nni_strdup(".\\") : nni_strdup(dir);
#else
char *directory = dir == NULL ? nni_strdup("./") : nni_strdup(dir);
#endif

size_t path_len = strlen(directory) + strlen(file_name) + 3;
char * path = nng_zalloc(path_len);

#if defined(NNG_PLATFORM_WINDOWS)
snprintf(path, path_len, "%s%s%s", directory,
directory[strlen(directory) - 1] == '\\' ? "" : "\\", file_name);
#else
snprintf(path, path_len, "%s%s%s", directory,
directory[strlen(directory) - 1] == '/' ? "" : "/", file_name);
#endif

nni_strfree(directory);

return path;
}
Loading

0 comments on commit 0a4963a

Please sign in to comment.