Skip to content

Commit

Permalink
Rework logger to include multi-domain levels
Browse files Browse the repository at this point in the history
To configure log levels per domain, use:
calaos_config set debug_domains hifirose:5,network:0

To set default log level for all non-specified domains, use:
calaos_config set debug_level 3
  • Loading branch information
raoulh committed Apr 25, 2024
1 parent e1f49d5 commit 8161491
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/bin/calaos_server/Audio/AVRRose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ void AVRRose::postRequest(string urlPath, string data, std::function<void(const
{
string url = "http://" + host + ":" + Utils::to_string(port) + "/" + urlPath;

cDebugDom("hifirose") << "POST " << url << " data: " << data;

UrlDownloader *dl = new UrlDownloader(url, true);
dl->setHeader("Content-Type", "application/json");
dl->httpPost({}, data);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/calaos_server/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ void WebSocket::processControlFrame()
if (status == WSClosing) return;

double elapsed = Utils::getMainLoopTime() - ping_time;
cInfoDom("websocket") << "Received a PONG back in " << Utils::time2string_digit(elapsed, elapsed * 1000.);
cDebugDom("websocket") << "Received a PONG back in " << Utils::time2string_digit(elapsed, elapsed * 1000.);
}
else if (currentFrame.isCloseFrame())
{
Expand All @@ -452,7 +452,7 @@ void WebSocket::processControlFrame()
string close_reason;
currentFrame.parseCloseCodeReason(code, close_reason);

cInfoDom("websocket") << "Close frame received, code:" << code << " reason: " << close_reason;
cDebugDom("websocket") << "Close frame received, code:" << code << " reason: " << close_reason;

DELETE_NULL(closeTimeout);
closeReceived = true;
Expand Down
71 changes: 58 additions & 13 deletions src/lib/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,74 @@ bool LogStream::isTerminal()
* LOG_LEVEL_WARNING = 3
* LOG_LEVEL_INFO = 4
* LOG_LEVEL_DEBUG = 5
*
* To configure log levels per domain, use:
* calaos_config set debug_domains hifirose:5,network:0
*
* To set default log level for all non-specified domains, use:
* calaos_config set debug_level 3
*
*/

static int maxLevelPrintable()
static int maxLevelPrintable(string domain)
{
static int max_level_print = -1;
static std::unordered_map<std::string, int> logger_domains;

if (max_level_print > -1)
return max_level_print;

string lvl = Utils::get_config_option("debug_level");
if (Utils::is_of_type<int>(lvl))
Utils::from_string(lvl, max_level_print);
if (logger_domains.empty())
{
int logger_default_level = Logger::LOG_LEVEL_INFO;
string lvl = Utils::get_config_option("debug_level");
if (Utils::is_of_type<int>(lvl))
Utils::from_string(lvl, logger_default_level);

if (logger_default_level < Logger::LOG_LEVEL_UNKNOWN ||
logger_default_level > Logger::LOG_LEVEL_DEBUG)
logger_default_level = Logger::LOG_LEVEL_INFO;

//default logger level for all non-specified domains
logger_domains["default"] = logger_default_level;

string domains = Utils::get_config_option("debug_domains");
if (!domains.empty())
{
std::vector<std::string> parts;
Utils::split(domains, parts, ",");

for (const auto &d: parts)
{
std::vector<std::string> dp;
Utils::split(d, dp, ":");

if (dp.size() == 2)
{
int l;
if (Utils::is_of_type<int>(dp[1]))
{
Utils::from_string(dp[1], l);
if (l >= Logger::LOG_LEVEL_UNKNOWN && l <= Logger::LOG_LEVEL_DEBUG)
logger_domains[dp[0]] = l;
}
}
}
}

if (logger_default_level == Logger::LOG_LEVEL_DEBUG)
{
for (const auto &d: logger_domains)
cDebug() << "Logger: Domain: " << d.first << " Level: " << d.second;
}
}

if (max_level_print < 0 ||
max_level_print > Logger::LOG_LEVEL_DEBUG)
max_level_print = Logger::LOG_LEVEL_INFO;
auto it = logger_domains.find(domain);
if (it != logger_domains.end())
return it->second;

return max_level_print;
return logger_domains["default"];
}

LogStream::~LogStream()
{
if (logData->level > maxLevelPrintable())
if (logData->level > maxLevelPrintable(logData->domain))
return;

//Strip away file path and only keep filename
Expand Down

0 comments on commit 8161491

Please sign in to comment.