* MDF [log] optimize log init logic.
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
-#include <nng.h>
#include "mqtt_api.h"
#include "nanomq.h"
#include "nng/nng.h"
#include "nng/protocol/mqtt/mqtt_parser.h"
-#include "nng/supplemental/nanolib/log.h"
#include "nng/supplemental/nanolib/file.h"
+#include "nng/supplemental/nanolib/log.h"
+#include "nng/nng.h"
-#if defined(DEBUG_SYSLOG)
+#if defined(SUPP_SYSLOG)
#include <syslog.h>
#endif
* @return int
*/
int
-nano_listen(
- nng_socket sid, const char *addr, nng_listener *lp, int flags, conf *config)
+nano_listen(nng_socket sid, const char *addr, nng_listener *lp, int flags,
+ conf *config)
{
int rv;
nng_listener l;
@@ -183,6 +183,9 @@ encode_common_mqtt_msg(
return 0;
}
+static nng_mtx *log_file_mtx = NULL;
+static FILE * log_fp = NULL;
+
static int
log_file_init(conf_log *log)
{
@@ -199,8 +202,8 @@ log_file_init(conf_log *log)
snprintf(path, path_len, "%s%s%s", log->dir,
log->dir[strlen(log->dir) - 1] == '/' ? "" : "/", log->file);
- log->fp = fopen(path, "a");
- if (log->fp == NULL) {
+ log_fp = fopen(path, "a");
+ if (log_fp == NULL) {
log_fatal("open log file '%s' failed", path);
nng_free(path, path_len);
return NNG_EINVAL;
@@ -213,22 +216,24 @@ int
log_init(conf_log *log)
{
int rv = 0;
+
log_set_level(log->level);
if (0 != (log->type & LOG_TO_CONSOLE)) {
- log_add_console(log->level);
+ log_add_console(log->level, NULL);
}
if (0 != (log->type & LOG_TO_FILE)) {
- if (0 != (rv = log_file_init(log))) {
+ if (0 != (rv = log_file_init(log)) ||
+ 0 != (rv = nng_mtx_alloc(&log_file_mtx))) {
return rv;
}
- log_add_fp(log->fp, log->level);
+ log_add_fp(log_fp, log->level, log_file_mtx);
}
-#if defined(DEBUG_SYSLOG)
+#if defined(SUPP_SYSLOG)
if (0 != (log->type & LOG_TO_SYSLOG)) {
- log_add_syslog("nng-nanomq", log->level);
+ log_add_syslog("nng-nanomq", log->level, NULL);
}
#endif
@@ -238,12 +243,12 @@ log_init(conf_log *log)
int
log_fini(conf_log *log)
{
-
if (0 != (log->type & LOG_TO_FILE)) {
- fclose(log->fp);
+ fclose(log_fp);
+ nng_mtx_free(log_file_mtx);
}
-#if defined(DEBUG_SYSLOG)
+#if defined(SUPP_SYSLOG)
if (0 != (log->type & LOG_TO_SYSLOG)) {
closelog();
}
