This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
utility: refactor logging module #224
Closed
Closed
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b81fc59
utility: refactor logging module
08e6ce4
fix test
1eaef77
modify api
1f98d2c
minor fix
f99ea33
Merge branch 'master' into master
d48d94b
Merge branch 'master' into master
109b648
Merge branch 'master' into master
22385e0
Merge branch 'master' into master
b1ba7dc
Merge branch 'master' into master
5d08aed
Merge branch 'master' into master
9d115f8
Merge branch 'master' into master
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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) { \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个地方应该不能这么写。因为如果err是个表达式的话,可能会执行两遍。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 哈哈哈哈哈哈 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个nfs其实也没用了,我忘了删了。下个pr可以删了