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

[easylog][feat]set get options #415

Merged
merged 1 commit into from
Aug 18, 2023
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
45 changes: 45 additions & 0 deletions include/ylt/easylog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ class logger {

void stop_async_log() { appender_->stop(); }

// set and get
void set_min_severity(Severity severity) { min_severity_ = severity; }
Severity get_min_severity() { return min_severity_; }

void set_console(bool enable) {
enable_console_ = enable;
if (appender_) {
appender_->enable_console(enable);
}
}
bool get_console() { return enable_console_; }

void set_async(bool enable) { async_ = enable; }
bool get_async() { return async_; }

private:
logger() {
static appender appender{};
Expand Down Expand Up @@ -117,6 +132,36 @@ inline void init_log(Severity min_severity, const std::string &filename = "",
max_file_size, max_files, flush_every_time);
}

template <size_t Id = 0>
inline void set_min_severity(Severity severity) {
logger<Id>::instance().set_min_severity(severity);
}

template <size_t Id = 0>
inline Severity get_min_severity() {
return logger<Id>::instance().get_min_severity();
}

template <size_t Id = 0>
inline void set_console(bool enable) {
logger<Id>::instance().set_console(enable);
}

template <size_t Id = 0>
inline bool get_console() {
return logger<Id>::instance().get_console();
}

template <size_t Id = 0>
inline void set_async(bool enable) {
logger<Id>::instance().set_async(enable);
}

template <size_t Id = 0>
inline bool get_async() {
return logger<Id>::instance().get_async();
}

template <size_t Id = 0>
inline void flush() {
logger<Id>::instance().flush();
Expand Down
7 changes: 7 additions & 0 deletions include/ylt/easylog/record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <charconv>
#include <chrono>
#include <cstring>

#include "ylt/util/time_util.h"
#ifdef __linux__
#include <sys/syscall.h>
#include <unistd.h>
Expand Down Expand Up @@ -177,6 +179,11 @@ class record_t {
else if constexpr (detail::has_str_v<U>) {
ss_.append(data.str());
}
else if constexpr (std::is_same_v<std::chrono::system_clock::time_point,
U>) {
ss_.append(
ylt::get_local_time_str(std::chrono::system_clock::to_time_t(data)));
}
else {
std::stringstream ss;
ss << data;
Expand Down
20 changes: 20 additions & 0 deletions src/easylog/tests/test_easylog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ylt/util/time_util.h>

#include <filesystem>
#include <ylt/easylog.hpp>

Expand All @@ -38,6 +40,24 @@ TEST_CASE("test basic") {
std::filesystem::remove(filename);
easylog::init_log(Severity::DEBUG, filename, false, true, 5000, 1, true);

std::chrono::seconds seconds(3);
std::chrono::system_clock::time_point p(seconds);
std::chrono::system_clock::now();

std::time_t now = std::time(0);
std::chrono::system_clock::time_point pt =
std::chrono::system_clock::from_time_t(now);
ELOG_INFO << pt;
ELOG_INFO << std::chrono::system_clock::now();

easylog::set_console(false);
ELOG_INFO << "no console";
easylog::set_console(true);

easylog::set_min_severity(Severity::WARN);
ELOG_INFO << "info log";
easylog::set_min_severity(Severity::DEBUG);

std::unique_ptr<int> ptr(new int(42));
ELOG_INFO << ptr.get();
ELOG_INFO << 42 << " " << 4.5 << 'a' << Severity::DEBUG;
Expand Down
Loading