Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackarain committed Sep 20, 2024
1 parent c767694 commit 3a2fce2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 86 deletions.
154 changes: 73 additions & 81 deletions proxy/include/proxy/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ namespace xlogger {


inline bool global_logging___ = true;
inline bool global_console_logging___ = true;
inline bool global_write_logging___ = true;
inline int64_t global_logfile_size___ = DEFAULT_LOG_MAXFILE_SIZE;


Expand Down Expand Up @@ -773,17 +775,9 @@ class auto_logger_file__
return m_log_path.string();
}

inline void logging(bool disable) noexcept
{
m_disable_write = disable;
}

inline void write([[maybe_unused]] int64_t time,
const char* str, std::streamsize size)
{
if (m_disable_write)
return;

bool condition = false;
auto hours = time / 1000 / 3600;
auto last_hours = m_last_time / 1000 / 3600;
Expand Down Expand Up @@ -881,7 +875,6 @@ class auto_logger_file__
ofstream_ptr m_ofstream;
int64_t m_last_time{ -1 };
int64_t m_log_size{ 0 };
bool m_disable_write{ false };
};

#ifndef DISABLE_LOGGER_THREAD_SAFE
Expand Down Expand Up @@ -927,8 +920,7 @@ const inline std::string _LOGGER_WARN_STR__ = " WARN ";
const inline std::string _LOGGER_ERR_STR__ = " ERROR ";
const inline std::string _LOGGER_FILE_STR__ = " FILE ";

inline void logger_output_console__([[maybe_unused]] bool disable_cout,
[[maybe_unused]] const logger_level__& level,
inline void logger_output_console__([[maybe_unused]] const logger_level__& level,
[[maybe_unused]] const std::string& prefix,
[[maybe_unused]] const std::string& message) noexcept
{
Expand All @@ -940,79 +932,73 @@ inline void logger_output_console__([[maybe_unused]] bool disable_cout,
#endif

#if !defined(DISABLE_LOGGER_TO_CONSOLE)
if (!disable_cout)
{
HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(handle_stdout, &csbi);

switch (level)
{
case _logger_info_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN);
break;
case _logger_debug_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case _logger_warn_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
case _logger_error_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
}
HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(handle_stdout, &csbi);

WriteConsoleW(handle_stdout,
title.data(), (DWORD)title.size(), nullptr, nullptr);
switch (level)
{
case _logger_info_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);

WriteConsoleW(handle_stdout,
msg.data(), (DWORD)msg.size(), nullptr, nullptr);
SetConsoleTextAttribute(handle_stdout, csbi.wAttributes);
FOREGROUND_GREEN);
break;
case _logger_debug_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case _logger_warn_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
case _logger_error_id__:
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
}

WriteConsoleW(handle_stdout,
title.data(), (DWORD)title.size(), nullptr, nullptr);
SetConsoleTextAttribute(handle_stdout,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);

WriteConsoleW(handle_stdout,
msg.data(), (DWORD)msg.size(), nullptr, nullptr);
SetConsoleTextAttribute(handle_stdout, csbi.wAttributes);
#endif

#if !defined(DISABLE_LOGGER_TO_DBGVIEW)
LOGGER_DBG_VIEW_(title + msg);
#endif

#elif !defined(DISABLE_LOGGER_TO_CONSOLE)
if (!disable_cout)
{
std::string out;

switch (level)
{
case _logger_info_id__:
std::format_to(std::back_inserter(out),
"\033[32m{}\033[0m{}", prefix, message);
break;
case _logger_debug_id__:
std::format_to(std::back_inserter(out),
"\033[1;32m{}\033[0m{}", prefix, message);
break;
case _logger_warn_id__:
std::format_to(std::back_inserter(out),
"\033[1;33m{}\033[0m{}", prefix, message);
break;
case _logger_error_id__:
std::format_to(std::back_inserter(out),
"\033[1;31m{}\033[0m{}", prefix, message);
break;
case _logger_file_id__:
// std::format_to(std::back_inserter(out),
// "\033[1;34m{}\033[0m{}", prefix, message);
break;
}

std::cout << out;
std::cout.flush();
}
std::string out;

switch (level)
{
case _logger_info_id__:
std::format_to(std::back_inserter(out),
"\033[32m{}\033[0m{}", prefix, message);
break;
case _logger_debug_id__:
std::format_to(std::back_inserter(out),
"\033[1;32m{}\033[0m{}", prefix, message);
break;
case _logger_warn_id__:
std::format_to(std::back_inserter(out),
"\033[1;33m{}\033[0m{}", prefix, message);
break;
case _logger_error_id__:
std::format_to(std::back_inserter(out),
"\033[1;31m{}\033[0m{}", prefix, message);
break;
case _logger_file_id__:
// std::format_to(std::back_inserter(out),
// "\033[1;34m{}\033[0m{}", prefix, message);
break;
}

std::cout << out;
std::cout.flush();
#endif
}

Expand Down Expand Up @@ -1114,7 +1100,8 @@ inline void logger_writer__(int64_t time, const logger_level__& level,
return;

#ifndef DISABLE_WRITE_LOGGING
logger.write(time, whole.c_str(), whole.size());
if (global_write_logging___)
logger.write(time, whole.c_str(), whole.size());
#endif // !DISABLE_WRITE_LOGGING

// Output to systemd.
Expand All @@ -1129,7 +1116,8 @@ inline void logger_writer__(int64_t time, const logger_level__& level,

// Output to console.
#if !defined(USE_SYSTEMD_LOGGING) && !defined(__ANDROID__)
logger_output_console__(disable_cout, level, prefix, tmp);
if (global_console_logging___ && !disable_cout)
logger_output_console__(level, prefix, tmp);
#endif
}

Expand Down Expand Up @@ -1305,11 +1293,14 @@ inline void turnon_logging() noexcept
global_logging___ = true;
}

inline void toggle_write_logging(bool disable)
inline void toggle_write_logging(bool enable)
{
auto_logger_file__& file =
logger_aux__::writer_single<xlogger::auto_logger_file__>();
file.logging(disable);
global_write_logging___ = enable;
}

inline void toggle_console_logging(bool enable)
{
global_console_logging___ = enable;
}

inline void set_logfile_maxsize(int64_t size) noexcept
Expand Down Expand Up @@ -1826,7 +1817,8 @@ namespace xlogger {
inline void shutdown_logging();
inline void turnoff_logging() noexcept;
inline void turnon_logging() noexcept;
inline void toggle_write_logging(bool disable);
inline void toggle_write_logging(bool enable);
inline void toggle_console_logging(bool enable);
inline void set_logfile_maxsize(int64_t size) noexcept;
}

Expand Down
8 changes: 3 additions & 5 deletions server/proxy_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,10 @@ and/or open issues at https://github.com/Jackarain/proxy)"
po::notify(vm);
}

if (disable_logs)
xlogger::toggle_write_logging(true);

if (disable_logs || log_dir.empty())
xlogger::toggle_write_logging(false);
else
xlogger::init_logging(log_dir);
if (log_dir.empty())
xlogger::toggle_write_logging(true);

print_args(argc, argv, vm);

Expand Down

0 comments on commit 3a2fce2

Please sign in to comment.