Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization of logging #1050

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,10 @@ NNG_DECL bool nng_lmq_empty(nng_lmq *);
typedef struct conn_param conn_param;
typedef struct pub_packet_struct pub_packet_struct;
typedef struct pipe_db nano_pipe_db;

NNG_DECL int nng_access(const char* name, int flag);

// NANOMQ MQTT API ends
// UDP operations. These are provided for convenience,
// and should be considered somewhat experimental.

Expand Down
2 changes: 2 additions & 0 deletions include/nng/supplemental/util/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
extern "C" {
#endif

// Return unix timestamp (milliseconds) .
NNG_DECL nng_time nng_timestamp(void);

// Get current process Id.
NNG_DECL int nng_getpid();
Expand Down
2 changes: 2 additions & 0 deletions src/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ extern int nni_plat_getpid(void);
// nni_plat_file_size get file size.
extern int nni_plat_file_size(const char *, size_t *);

// nni_access check accessibility of path
extern int nni_plat_access(const char* name, int flag);
//
// Actual platforms we support. This is included up front so that we can
// get the specific types that are supplied by the platform.
Expand Down
8 changes: 7 additions & 1 deletion src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -2478,7 +2478,7 @@ conn_param_set_username(conn_param *cparam, const char *username)
void
conn_param_set_password(conn_param *cparam, const char *password)
{
cparam->password.body = nng_strdup(password);
cparam->password.body = (uint8_t *)nng_strdup(password);
cparam->password.len = strlen(password) + 1;
}

Expand Down Expand Up @@ -2555,6 +2555,12 @@ nng_file_is_dir(const char *path)
return nni_file_is_dir(path);
}

int nng_access(const char* name, int flag)
{
return nni_plat_access(name, flag);
}
// Ends of NANOMQ API

void
nng_init_set_parameter(nng_init_parameter p, uint64_t value)
{
Expand Down
4 changes: 4 additions & 0 deletions src/platform/posix/posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,8 @@ nni_plat_file_size(const char *path, size_t *size)
return (0);
}

int nni_plat_access(const char* name, int flag)
{
return access(name, flag);
}
#endif // NNG_PLATFORM_POSIX
4 changes: 4 additions & 0 deletions src/platform/windows/win_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,8 @@ nni_plat_file_size(const char *path, size_t *size)
return (rv);
}

int nni_plat_access(const char* name, int flag)
{
return _access(name, flag);
}
#endif // NNG_PLATFORM_WINDOWS
2 changes: 1 addition & 1 deletion src/supplemental/mqtt/mqtt_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ mqtt_close_unack_aio_cb(void *key, void *val)
nni_aio * aio = val;

if (aio) {
nni_aio_finish_error(aio, NNG_ECLOSED);
nni_aio_finish_sync(aio, NNG_ECLOSED, 0);
nni_msg_free(nni_aio_get_msg(aio));
nni_aio_set_msg(aio, NULL);
nni_aio_set_prov_data(aio, NULL);
Expand Down
11 changes: 8 additions & 3 deletions src/supplemental/mqtt/mqtt_public.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,8 @@ mqtt_property_free(property *prop)
void
mqtt_property_foreach(property *prop, void (*cb)(property *))
{
return property_foreach(prop, cb);
property_foreach(prop, cb);
return;
}

int
Expand Down Expand Up @@ -809,7 +810,8 @@ mqtt_property_get_value(property *prop, uint8_t prop_id)
void
mqtt_property_append(property *prop_list, property *last)
{
return property_append(prop_list, last);
property_append(prop_list, last);
return;
}


Expand Down Expand Up @@ -839,11 +841,14 @@ nng_mqtt_client_send_cb(void* arg)
{
nng_mqtt_client *client = (nng_mqtt_client *) arg;
nng_aio * aio = client->send_aio;
nng_msg * msg = nng_aio_get_msg(aio);
nng_msg * tmsg = NULL;

nni_lmq * lmq = (nni_lmq *)client->msgq;
// in case of data conention while fini pipes
if (nng_aio_result(aio) == NNG_ECLOSED)
return;

nng_msg * msg = nng_aio_get_msg(aio);
if (msg == NULL || nng_aio_result(aio) != 0) {
client->cb(client, NULL, client->obj);
return;
Expand Down
46 changes: 39 additions & 7 deletions src/supplemental/nanolib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ file_callback(log_event *ev)
#else
pid_t pid = syscall(__NR_gettid);
#endif
#ifndef NNG_PLATFORM_WINDOWS
if (nng_access(ev->config->dir, W_OK) < 0) {
fprintf(stderr, "open path %s failed! close file!\n",
ev->config->dir);
if (ev->config->fp != NULL)
fclose(ev->config->fp);
ev->config->fp = NULL;
return;
}
#endif
if (ev->config->fp == NULL)
ev->config->fp = fopen(ev->config->abs_path, "a");
FILE *fp = ev->config->fp;
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ev->time)] = '\0';
fprintf(fp, "%s [%i] %-5s %s:%d: ", buf, pid,
Expand Down Expand Up @@ -300,19 +312,31 @@ log_clear_callback()
static void
file_rotation(FILE *fp, conf_log *config)
{
// Note : do not call log_xxx() in this function, it will cause dead
// lock
// Note : do not call log_xxx() in this function, it will cause dead lock
size_t sz = 0;
int rv;
if ((rv = nni_plat_file_size(config->abs_path, &sz)) != 0) {
fprintf(stderr, "get file %s size failed: %s\n",
config->abs_path, nng_strerror(rv));
if (!nni_plat_file_exists(config->abs_path)) {
// file missing, recreate one
fclose(fp);
if (nng_file_put(config->abs_path, "\n", 1) != 0)
fprintf(stderr, "create file %s failed: %s\n",
if (fp)
fclose(fp);
#ifndef NNG_PLATFORM_WINDOWS
if (nng_access(config->dir, W_OK) < 0) {
fprintf(stderr, "open path %s failed\n",
config->dir);
config->fp = NULL;
return;
}
#endif
if (nng_file_put(config->abs_path, "\n", 1) != 0) {
fprintf(stderr, "write to file %s failed: %s\n",
config->abs_path, nng_strerror(rv));
config->fp = NULL;
return;
}

config->fp = fopen(config->abs_path, "a");
fp = config->fp;
}
Expand Down Expand Up @@ -342,13 +366,21 @@ file_rotation(FILE *fp, conf_log *config)
log_name, log_name_len, "%s.%lu", config->file, index);
char *backup_log_path =
nano_concat_path(config->dir, log_name);
fclose(fp);
if (fp)
fclose(fp);
fp = NULL;
remove(backup_log_path);
rename(config->abs_path, backup_log_path);
nni_free(log_name, log_name_len);
nni_strfree(backup_log_path);

#ifndef NNG_PLATFORM_WINDOWS
if (nng_access(config->dir, W_OK) < 0) {
fprintf(stderr, "open path %s failed\n",
config->dir);
config->fp = NULL;
return;
}
#endif
fp = fopen(config->abs_path, "a");
config->fp = fp;
char num[20] = { 0 };
Expand Down
Loading