Skip to content
Open
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ pybind/pybind11@3e9dfa2866941655c56877882565e7577de6fc7b --build
msgpack/[email protected] -DMSGPACK_BUILD_TESTS=Off -DMSGPACK_BUILD_EXAMPLES=Off -DCMAKE_POLICY_VERSION_MINIMUM=3.5
[email protected] -DCMAKE_POSITION_INDEPENDENT_CODE=On
ROCm/composable_kernel@b7775add2d28251674d81e220cd4a857b90b997a -DCK_BUILD_JIT_LIB=On -DCMAKE_POSITION_INDEPENDENT_CODE=On
ROCm/rocMLIR@b6c46dd51895148be0957fb0ba2d72b3efc4b87b -DBUILD_FAT_LIBROCKCOMPILER=On -DLLVM_INCLUDE_TESTS=Off
ROCm/rocMLIR@3a034fd4fa6c25ada90f1786700748b2f58aaf85 -DBUILD_FAT_LIBROCKCOMPILER=On -DLLVM_INCLUDE_TESTS=Off
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ add_library(migraphx
layout_convolution.cpp
lexing.cpp
load_save.cpp
logger.cpp
make_op.cpp
memory_coloring.cpp
module.cpp
Expand Down
125 changes: 124 additions & 1 deletion src/driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <migraphx/json.hpp>
#include <migraphx/version.h>
#include <migraphx/env.hpp>
#include <migraphx/logger.hpp>

#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
Expand All @@ -62,6 +63,7 @@

#include <fstream>
#include <iomanip>
#include <optional>

namespace {

Expand Down Expand Up @@ -94,6 +96,120 @@
ss << std::put_time(now_as_tm_date, "%Y-%m-%d %H:%M:%S");
return ss.str();
}

struct logger_options
{
std::string log_level;
std::vector<std::string> log_files;

void parse(migraphx::driver::argument_parser& ap)
{
ap(log_level,
{"--log-level"},
ap.help("Set log level (none/0, error/1, warn/2, info/3, debug/4, trace/5)"),
ap.validate([](auto&, auto&, auto& params) {
if(not params.empty())
{
auto level_str = params.back();

Check warning on line 113 in src/driver/main.cpp

View workflow job for this annotation

GitHub Actions / tidy

the variable 'level_str' is copy-constructed from a const reference but is only used as const reference; consider making it a const reference [performance-unnecessary-copy-initialization,-warnings-as-errors]
if(not parse_log_level_string(level_str))
{
throw std::runtime_error(
"Invalid log level: " + level_str +
". Valid levels: none/0, error/1, warn/2, info/3, debug/4, trace/5");
}
}
}));
ap(log_files,
{"--log-file"},
ap.help("Log to file(s) (--log-file file1.log file2.log ...)"),
ap.append(),
ap.nargs(2));
}

void apply() const
{
if(not log_level.empty())
{
auto level = parse_log_level_string(log_level);
if(level)
migraphx::log::set_severity(*level);
}
for(const auto& log_file : log_files)
{
migraphx::log::add_file_logger(log_file);
}
}

private:
static std::optional<migraphx::log::severity>
parse_log_level_string(const std::string& level_str)
{
if(level_str == "trace" or level_str == "5")
return migraphx::log::severity::trace;
else if(level_str == "debug" or level_str == "4")
return migraphx::log::severity::debug;
else if(level_str == "info" or level_str == "3")
return migraphx::log::severity::info;
else if(level_str == "warn" or level_str == "2")
return migraphx::log::severity::warn;
else if(level_str == "error" or level_str == "1")
return migraphx::log::severity::error;
else if(level_str == "none" or level_str == "0")
return migraphx::log::severity::none;

return std::nullopt;
}
};

bool parse_and_apply_logger_options(std::vector<std::string>& args)
{
// Extract only logger option flags from args for parsing
std::vector<std::string> logger_args;
auto it = args.begin();
while(it != args.end())
{
if(*it == "--log-level")
{
logger_args.push_back(*it);
it = args.erase(it);
// Grab the single value if present
if(it != args.end() and not it->empty() and (*it)[0] != '-')
{
logger_args.push_back(*it);
it = args.erase(it);
}
}
else if(*it == "--log-file")
{
logger_args.push_back(*it);
it = args.erase(it);
// Grab all values until the next flag (for unlimited log files)
while(it != args.end() and not it->empty() and (*it)[0] != '-')
{
logger_args.push_back(*it);
it = args.erase(it);
}
}
else
{
++it;
}
}

if(not logger_args.empty())
{
logger_options opts;
migraphx::driver::argument_parser ap;
opts.parse(ap);

if(ap.parse(logger_args))
return false;

opts.apply();
}

return true;
}
} // namespace

namespace migraphx {
Expand Down Expand Up @@ -1005,6 +1121,13 @@
int main(int argc, const char* argv[], const char* envp[])
{
std::vector<std::string> args(argv + 1, argv + argc);
// Save original args for display purposes before they get modified
const std::vector<std::string> original_args = args;

// Parse and apply logger options (--log-level, --log-file)
if(not parse_and_apply_logger_options(args))
return 1;

// no argument, print the help infomration by default
if(args.empty())
{
Expand All @@ -1028,7 +1151,7 @@
if(m.count(cmd) > 0)
{
std::string driver_invocation =
std::string(argv[0]) + " " + migraphx::to_string_range(args, " ");
std::string(argv[0]) + " " + migraphx::to_string_range(original_args, " ");
std::cout << "Running [ " << get_version() << " ]: " << driver_invocation << std::endl;

// Print start timestamp
Expand Down
169 changes: 169 additions & 0 deletions src/include/migraphx/logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MIGRAPHX_GUARD_MIGRAPHX_LOGGER_HPP
#define MIGRAPHX_GUARD_MIGRAPHX_LOGGER_HPP

#include <migraphx/env.hpp>
#include <migraphx/source_location.hpp>
#include <functional>
#include <sstream>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace log {

enum class severity
{
none,
error,
warn,
info,
debug,
trace
};

using sink = std::function<void(severity, std::string_view, source_location)>;

/**
* @brief Records a log message. This will invoke the callback for all sinks that are enabled at the
* given severity.
*
* @param s The severity of the log message
* @param msg The message to log
* @param loc The source location of the log message
*/
void record(severity s, std::string_view msg, source_location loc = source_location::current());

/**
* @brief Checks if any sink is enabled at the given severity.
*
* @param level The severity to check
* @return true if any sink is enabled at the given severity, false otherwise
*/
bool is_enabled(severity level);

/**
* @brief Adds a sink to the logger.
*
* @param s The sink to add
* @param level The severity level of the sink
* @return The ID of the added sink
*/
size_t add_sink(sink s, severity level = severity::info);

/**
* @brief Removes a sink from the logger.
*
* @param id The ID of the sink to remove
*/
void remove_sink(size_t id);

/**
* @brief Sets the severity level for a specific sink.
*
* @param level The severity level to set
* @param id The ID of the sink to set the severity for; defaults to 0 for the stderr sink
*/
void set_severity(severity level, size_t id = 0);

/**
* @brief Adds a file sink to the logger.
*
* @param filename The name of the file to log to
* @param level The severity level of the file logger
* @return The ID of the added file logger
*/
size_t add_file_logger(std::string_view filename, severity level = severity::info);

template <severity Severity>
struct print
{
print(source_location ploc = source_location::current()) : loc(ploc) {}

struct stream
{
template <class T>
stream(severity ps, T&& x, source_location ploc = source_location::current())
: s(ps), loc(ploc), enabled(is_enabled(s))
{
if(enabled)
ss << x;
}

template <class T>
stream& operator<<(T&& x)
{
if(enabled)
ss << x;
return *this;
}

~stream()
{
if(enabled)
record(s, ss.str(), loc);
}

stream(const stream&) = delete;
stream& operator=(const stream&) = delete;

severity s = severity::none;
source_location loc;
bool enabled;
std::ostringstream ss;
};

template <class T>
stream operator<<(T&& x)
{
return stream{Severity, x, loc};
}

template <class... Ts>
void operator()(Ts&&... xs) const
{
if(is_enabled(Severity))
{
std::ostringstream ss;
(ss << ... << xs);
record(Severity, ss.str(), loc);
}
}

print(const print&) = delete;
print& operator=(const print&) = delete;

source_location loc;
};

using error = print<severity::error>;
using warn = print<severity::warn>;
using info = print<severity::info>;
using debug = print<severity::debug>;
using trace = print<severity::trace>;

} // namespace log
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
#endif
Loading
Loading