-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* NEW [log] enable log in sdk for neuron
Signed-off-by: wayne <[email protected]>
- Loading branch information
1 parent
19e601c
commit 0a4963a
Showing
9 changed files
with
766 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.