-
Notifications
You must be signed in to change notification settings - Fork 1.8k
add logging interface #1163
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
Open
jf99
wants to merge
7
commits into
uNetworking:master
Choose a base branch
from
jf99:logging-interface
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add logging interface #1163
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12d138b
add logging interface
jf99 99e2de9
make log verbosity compile-time configurable
jf99 6d40144
check loglevel in UWS_LOG_REQUEST macro
jf99 1af11d3
make logging more efficient
jf99 cba6025
adapt includes
jf99 d4ba584
replace exception throwing by return
jf99 c153092
even more logging
jf99 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 hidden or 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 hidden or 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,72 @@ | ||
| #ifndef UWS_LOG_H | ||
| #define UWS_LOG_H | ||
| #include <array> | ||
| #include <functional> | ||
| #include <iostream> | ||
| #include <string_view> | ||
|
|
||
| #define UWS_LOG_LEVEL 0 // override this to get more verbose logging | ||
|
|
||
| namespace uWS { | ||
|
|
||
| /*! | ||
| * \brief Log a message. The user of this lib is free to override this function object with a custom logger. | ||
| * \param [in] message The message to be logged. | ||
| * \param [in] logLevel The severity of the message. 0 is error, 1 is warning, 2 is info. With each increment the severity decrements. | ||
| */ | ||
| typedef std::function<void(std::string_view message, int logLevel)> LogFunction; | ||
|
|
||
| inline LogFunction log = [](std::string_view message, int logLevel) -> void { | ||
| if(logLevel <= 1) { | ||
| std::cerr << message << std::endl; | ||
| } | ||
| else { | ||
| std::cout << message << std::endl; | ||
| } | ||
| }; | ||
|
|
||
| class LogBuffer | ||
| { | ||
| private: | ||
| std::array<char,1024> buf{0}; | ||
| size_t cursor = 0; | ||
|
|
||
| LogBuffer& operator<< (std::string_view sv) { | ||
| const size_t bytesFree = buf.size() - cursor; | ||
| if(sv.size() > bytesFree) | ||
| throw std::length_error("log message too long"); | ||
| memcpy(&buf[cursor], sv.data(), sv.size()); | ||
| cursor += sv.size(); | ||
| return *this; | ||
| } | ||
|
|
||
| public: | ||
| template <typename T> | ||
| void put(T msg) { | ||
| *this << msg; | ||
| } | ||
| template <typename T, typename... Ts> | ||
| void put(T t, Ts... ts) { | ||
| put(t); | ||
| put(ts...); | ||
| } | ||
| std::string_view get() { | ||
| std::string_view ret(buf.data(), cursor); | ||
| cursor = 0; | ||
| return ret; | ||
| } | ||
| }; | ||
|
|
||
| thread_local LogBuffer logBuffer; | ||
|
|
||
| #define UWS_LOG_REQUEST(loglevel, ...) { \ | ||
| if constexpr(loglevel <= UWS_LOG_LEVEL) { \ | ||
| ::uWS::logBuffer.put(__VA_ARGS__); \ | ||
| ::uWS::log(::uWS::logBuffer.get(), loglevel); \ | ||
| } \ | ||
| } | ||
|
|
||
| } // namespace uWS | ||
|
|
||
| #endif // UWS_LOG_H | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.