aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/config/loggingconfig.cpp
blob: 9ec816b1b5c7eb8d0a8e3458c1acdee427b00d24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zenutil/config/loggingconfig.h"

#include <zenbase/zenbase.h>
#include <zencore/filesystem.h>
#include <zencore/logging.h>

ZEN_THIRD_PARTY_INCLUDES_START
#include <cxxopts.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

namespace zen {

void
ZenLoggingCmdLineOptions::AddCliOptions(cxxopts::Options& options, ZenLoggingConfig& LoggingConfig)
{
	// clang-format off
	options.add_options("logging")
		("abslog",			"Path to log file",											cxxopts::value<std::string>(m_AbsLogFile))
		("log-id",			"Specify id for adding context to log output",				cxxopts::value<std::string>(LoggingConfig.LogId))
		("quiet",			"Configure console logger output to level WARN",			cxxopts::value<bool>(LoggingConfig.QuietConsole)->default_value("false"))
		("noconsole",		"Disable console logging",									cxxopts::value<bool>(LoggingConfig.NoConsoleOutput)->default_value("false"))
		("log-trace", 		"Change selected loggers to level TRACE", 					cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Trace]))
		("log-debug", 		"Change selected loggers to level DEBUG", 					cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Debug]))
		("log-info", 		"Change selected loggers to level INFO", 					cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Info]))
		("log-warn", 		"Change selected loggers to level WARN", 					cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Warn]))
		("log-error", 		"Change selected loggers to level ERROR", 					cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Err]))
		("log-critical", 	"Change selected loggers to level CRITICAL", 				cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Critical]))
		("log-off", 		"Change selected loggers to level OFF", 					cxxopts::value<std::string>(LoggingConfig.Loggers[logging::level::Off]))
		("otlp-endpoint", 	"OpenTelemetry endpoint URI (e.g http://localhost:4318)", 	cxxopts::value<std::string>(LoggingConfig.OtelEndpointUri))
	;
	// clang-format on
}

void
ZenLoggingCmdLineOptions::ApplyOptions(ZenLoggingConfig& LoggingConfig)
{
	LoggingConfig.AbsLogFile = MakeSafeAbsolutePath(m_AbsLogFile);
}

void
ApplyLoggingOptions(cxxopts::Options& options, ZenLoggingConfig& LoggingConfig)
{
	ZEN_UNUSED(options);

	if (LoggingConfig.QuietConsole)
	{
		bool HasExplicitConsoleLevel = false;
		for (int i = 0; i < logging::level::LogLevelCount; ++i)
		{
			if (LoggingConfig.Loggers[i].find("console") != std::string::npos)
			{
				HasExplicitConsoleLevel = true;
				break;
			}
		}

		if (!HasExplicitConsoleLevel)
		{
			std::string& WarnLoggers = LoggingConfig.Loggers[logging::level::Warn];
			if (!WarnLoggers.empty())
			{
				WarnLoggers += ",";
			}
			WarnLoggers += "console";
		}
	}

	for (int i = 0; i < logging::level::LogLevelCount; ++i)
	{
		logging::ConfigureLogLevels(logging::level::LogLevel(i), LoggingConfig.Loggers[i]);
	}
	logging::RefreshLogLevels();
}

}  // namespace zen