Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

utility: refactor logging module #224

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
77 changes: 1 addition & 76 deletions include/dsn/c/api_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,82 +37,7 @@

#include <dsn/c/api_common.h>
#include <dsn/utility/ports.h>

/*!
@defgroup logging Logging Service
@ingroup service-api-utilities

Logging Service

Note developers can plug into rDSN their own logging libraryS
implementation, so as to integrate rDSN logs into
their own cluster operation systems.
@{
*/

typedef enum dsn_log_level_t {
LOG_LEVEL_INFORMATION,
LOG_LEVEL_DEBUG,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_FATAL,
LOG_LEVEL_COUNT,
LOG_LEVEL_INVALID
} dsn_log_level_t;

// logs with level smaller than this start_level will not be logged
extern DSN_API dsn_log_level_t dsn_log_start_level;
extern DSN_API dsn_log_level_t dsn_log_get_start_level();
extern DSN_API void dsn_log_set_start_level(dsn_log_level_t level);
extern DSN_API void dsn_logv(const char *file,
const char *function,
const int line,
dsn_log_level_t log_level,
const char *fmt,
va_list args);
extern DSN_API void dsn_logf(const char *file,
const char *function,
const int line,
dsn_log_level_t log_level,
const char *fmt,
...);
extern DSN_API void
dsn_log(const char *file, const char *function, const int line, dsn_log_level_t log_level);
extern DSN_API void dsn_coredump();

// __FILENAME__ macro comes from the cmake, in which we calculate a filename without path.
#define dlog(level, ...) \
do { \
if (level >= dsn_log_start_level) \
dsn_logf(__FILENAME__, __FUNCTION__, __LINE__, level, __VA_ARGS__); \
} while (false)
#define dinfo(...) dlog(LOG_LEVEL_INFORMATION, __VA_ARGS__)
#define ddebug(...) dlog(LOG_LEVEL_DEBUG, __VA_ARGS__)
#define dwarn(...) dlog(LOG_LEVEL_WARNING, __VA_ARGS__)
#define derror(...) dlog(LOG_LEVEL_ERROR, __VA_ARGS__)
#define dfatal(...) dlog(LOG_LEVEL_FATAL, __VA_ARGS__)
#define dassert(x, ...) \
do { \
if (dsn_unlikely(!(x))) { \
dlog(LOG_LEVEL_FATAL, "assertion expression: " #x); \
dlog(LOG_LEVEL_FATAL, __VA_ARGS__); \
dsn_coredump(); \
} \
} while (false)

#define dreturn_not_ok_logged(err, ...) \
do { \
if ((err) != dsn::ERR_OK) { \
derror(__VA_ARGS__); \
return err; \
} \
} while (0)

#ifndef NDEBUG
#define dbg_dassert dassert
#else
#define dbg_dassert(x, ...)
#endif
#include <dsn/utility/logging.h>

#ifdef DSN_MOCK_TEST
#define mock_private public
Expand Down
3 changes: 0 additions & 3 deletions include/dsn/tool-api/global_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ struct service_spec
std::string rwlock_nr_factory_name;
std::string semaphore_factory_name;
std::string nfs_factory_name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个nfs其实也没用了,我忘了删了。下个pr可以删了

std::string logging_factory_name;

std::list<std::string> network_aspects; // toollets compatible to the above network main
// providers in network configs
Expand All @@ -180,7 +179,6 @@ struct service_spec

// auto-set
std::string dir_coredump;
std::string dir_log;

service_spec() {}
DSN_API bool init();
Expand Down Expand Up @@ -208,7 +206,6 @@ CONFIG_FLD_STRING(lock_nr_factory_name, "", "non-recurisve exclusive lock provid
CONFIG_FLD_STRING(rwlock_nr_factory_name, "", "non-recurisve rwlock provider")
CONFIG_FLD_STRING(semaphore_factory_name, "", "semaphore provider")
CONFIG_FLD_STRING(nfs_factory_name, "", "nfs provider")
CONFIG_FLD_STRING(logging_factory_name, "", "logging provider")

CONFIG_FLD_STRING_LIST(network_aspects, "network aspect providers, usually for tooling purpose")
CONFIG_FLD_STRING_LIST(aio_aspects, "aio aspect providers, usually for tooling purpose")
Expand Down
8 changes: 0 additions & 8 deletions include/dsn/tool-api/task_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@
#include <dsn/tool-api/auto_codes.h>
#include <dsn/c/api_utilities.h>

ENUM_BEGIN(dsn_log_level_t, LOG_LEVEL_INVALID)
ENUM_REG(LOG_LEVEL_INFORMATION)
ENUM_REG(LOG_LEVEL_DEBUG)
ENUM_REG(LOG_LEVEL_WARNING)
ENUM_REG(LOG_LEVEL_ERROR)
ENUM_REG(LOG_LEVEL_FATAL)
ENUM_END(dsn_log_level_t)

namespace dsn {

enum task_state
Expand Down
5 changes: 1 addition & 4 deletions include/dsn/tool_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Component providers define the interface for the local components (e.g., network
#include <dsn/tool-api/aio_provider.h>
#include <dsn/tool-api/env_provider.h>
#include <dsn/tool-api/message_parser.h>
#include <dsn/tool-api/logging_provider.h>
#include <dsn/utility/logging.h>
#include <dsn/tool-api/timer_service.h>

namespace dsn {
Expand Down Expand Up @@ -143,9 +143,6 @@ DSN_API bool
register_component_provider(const char *name, aio_provider::factory f, ::dsn::provider_type type);
DSN_API bool
register_component_provider(const char *name, env_provider::factory f, ::dsn::provider_type type);
DSN_API bool register_component_provider(const char *name,
logging_provider::factory f,
::dsn::provider_type type);
DSN_API bool register_component_provider(network_header_format fmt,
const std::vector<const char *> &signatures,
message_parser::factory f,
Expand Down
122 changes: 122 additions & 0 deletions include/dsn/utility/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Microsoft Corporation
*
* -=- Robust Distributed System Nucleus (rDSN) -=-
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#pragma once

#include <dsn/utility/enum_helper.h>
#include <stdarg.h>

namespace dsn {

enum log_level_t
{
LOG_LEVEL_INFORMATION,
LOG_LEVEL_DEBUG,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
LOG_LEVEL_FATAL,
LOG_LEVEL_COUNT,
LOG_LEVEL_INVALID
};

ENUM_BEGIN(log_level_t, LOG_LEVEL_INVALID)
ENUM_REG(LOG_LEVEL_INFORMATION)
ENUM_REG(LOG_LEVEL_DEBUG)
ENUM_REG(LOG_LEVEL_WARNING)
ENUM_REG(LOG_LEVEL_ERROR)
ENUM_REG(LOG_LEVEL_FATAL)
ENUM_END(log_level_t)

// logs with level smaller than `s_log_start_level` will not be logged
extern log_level_t s_log_start_level;

/// An interface for logging messages.
class logging_provider
{
public:
virtual ~logging_provider() = default;

virtual void logv(const char *file,
const char *function,
int line,
log_level_t log_level,
const char *fmt,
va_list args) = 0;

virtual void flush() = 0;
};

/// Initializes default logger for rDSN.
extern void logging_init(const char *log_dir);

/// Customize the logging implementation.
extern void set_logging_provider(std::unique_ptr<logging_provider> provider);

} // namespace dsn

extern void dsn_logf(const char *file,
const char *function,
int line,
dsn::log_level_t log_level,
const char *fmt,
...);

extern void dsn_coredump();

// __FILENAME__ macro comes from the cmake, in which we calculate a filename without path.
#define dlog(level, ...) \
do { \
if (level >= dsn::s_log_start_level) \
dsn_logf(__FILENAME__, __FUNCTION__, __LINE__, level, __VA_ARGS__); \
} while (false)

#define dinfo(...) dlog(dsn::LOG_LEVEL_INFORMATION, __VA_ARGS__)
#define ddebug(...) dlog(dsn::LOG_LEVEL_DEBUG, __VA_ARGS__)
#define dwarn(...) dlog(dsn::LOG_LEVEL_WARNING, __VA_ARGS__)
#define derror(...) dlog(dsn::LOG_LEVEL_ERROR, __VA_ARGS__)
#define dfatal(...) dlog(dsn::LOG_LEVEL_FATAL, __VA_ARGS__)
#define dassert(x, ...) \
do { \
if (dsn_unlikely(!(x))) { \
dlog(dsn::LOG_LEVEL_FATAL, "assertion expression: " #x); \
dlog(dsn::LOG_LEVEL_FATAL, __VA_ARGS__); \
dsn_coredump(); \
} \
} while (false)

#define dreturn_not_ok_logged(err, ...) \
do { \
if ((err) != dsn::ERR_OK) { \
Copy link
Contributor

@shengofsun shengofsun Mar 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方应该不能这么写。因为如果err是个表达式的话,可能会执行两遍。
很难想象这段代码居然是我写的…… 我总觉得应该是留洋给我搞的,因为我和他专门讨论过这个问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哈哈哈哈哈哈

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

顺手把这个地方改了吧,别的我这边没啥问题。

derror(__VA_ARGS__); \
return err; \
} \
} while (0)

#ifndef NDEBUG
#define dbg_dassert dassert
#else
#define dbg_dassert(x, ...)
#endif
1 change: 1 addition & 0 deletions src/core/core/coredump.posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <dsn/tool_api.h>
#include <sys/types.h>
#include <signal.h>
#include <dsn/c/app_model.h>

namespace dsn {
namespace utils {
Expand Down
Loading