Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
84d885b
Add basic logging functionality
eddieliao Sep 20, 2025
5203de7
Formatting
eddieliao Sep 20, 2025
625a627
Refactor logger to use variadic function and add record function
eddieliao Oct 6, 2025
b4a6ff6
Formatting
eddieliao Oct 6, 2025
213fe9c
Add console and file logging using spdlog
eddieliao Nov 3, 2025
c818ecd
Formatting
eddieliao Nov 3, 2025
b6599ca
Add multi file logging; fix params not showing in logging
eddieliao Nov 3, 2025
1fdb940
Fix color logging
eddieliao Nov 4, 2025
d8a8414
Add windows color logger
eddieliao Nov 4, 2025
0cd773c
Add file-specific severity option
eddieliao Nov 7, 2025
10a9ae1
Remove windows specific sinks, add unit tests
eddieliao Nov 26, 2025
5c1fcff
Formatting
eddieliao Nov 26, 2025
60b64c8
Cleanup
eddieliao Nov 26, 2025
666e2c1
Merge branch 'develop' into logger
eddieliao Nov 26, 2025
66f1d10
Remove spdlog usage, add thread safe access to sinks, update tests
eddieliao Dec 2, 2025
5fbc196
Formatting
eddieliao Dec 2, 2025
6889864
Add doc comments, error messages, use linux specific timestamp, remov…
eddieliao Dec 2, 2025
3866156
Formatting
eddieliao Dec 2, 2025
6d54ebd
Merge branch 'develop' into logger
eddieliao Dec 2, 2025
4c751bf
Fix tidy
eddieliao Dec 9, 2025
7d99e29
Revert requirements.txt change
eddieliao Dec 10, 2025
801d681
Use any_of instead of for loop
eddieliao Dec 10, 2025
57423be
Suppress cppcheck false positives
eddieliao Dec 10, 2025
a41e04a
Update file logger tests, clean up other tests
eddieliao Dec 18, 2025
4e37b23
Add redefine macro for windows localtime
eddieliao Dec 18, 2025
15db01f
Spelling
eddieliao Dec 18, 2025
5517df0
Address comments
eddieliao Jan 6, 2026
366d508
Formatting
eddieliao Jan 6, 2026
2444301
Licensing
eddieliao Jan 6, 2026
b5e1e7c
Address additional comments
eddieliao Jan 7, 2026
6ae9a92
Fix tidy
eddieliao Jan 7, 2026
3da51e2
Licensing
eddieliao Jan 7, 2026
c138b29
Formatting
eddieliao Jan 7, 2026
e87de59
Move logger parser init out of command.hpp
eddieliao Jan 8, 2026
c9461b8
Use local logger_opts variable instead
eddieliao Jan 9, 2026
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
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#####################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
# Copyright (c) 2015-2026 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
Expand Down Expand Up @@ -54,6 +54,7 @@ add_library(migraphx
autocast_fp8.cpp
auto_contiguous.cpp
base64.cpp
color.cpp
common.cpp
common_dims.cpp
compile_src.cpp
Expand Down Expand Up @@ -89,6 +90,7 @@ add_library(migraphx
layout_convolution.cpp
lexing.cpp
load_save.cpp
logger.cpp
make_op.cpp
memory_coloring.cpp
module.cpp
Expand Down
58 changes: 58 additions & 0 deletions src/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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.
*/
#include <migraphx/color.hpp>
#include <sstream>

#ifndef _WIN32
#include <unistd.h>
#endif

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

std::ostream& operator<<(std::ostream& os, const color& c)
{
#ifndef _WIN32
int fd = -1;
if(&os == &std::cout)
fd = STDOUT_FILENO;
else if(&os == &std::cerr)
fd = STDERR_FILENO;
if(fd != -1 and isatty(fd) != 0)
return os << "\033[" << static_cast<std::size_t>(c) << "m";
#else
(void)c;
#endif
return os;
}

std::string colorize(color c, const std::string& s)
{
std::stringstream ss;
ss << c << s << color::reset;
return ss.str();
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
53 changes: 13 additions & 40 deletions src/driver/argument_parser.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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
Expand All @@ -28,6 +28,7 @@
#include <functional>
#include <iostream>
#include <list>
#include <optional>
#include <set>
#include <string>
#include <sstream>
Expand All @@ -38,6 +39,7 @@
#include <vector>

#include <migraphx/config.hpp>
#include <migraphx/color.hpp>
#include <migraphx/requires.hpp>
#include <migraphx/type_name.hpp>
#include <migraphx/functional.hpp>
Expand All @@ -47,10 +49,6 @@
#include <migraphx/ranges.hpp>
#include <migraphx/rank.hpp>

#ifndef _WIN32
#include <unistd.h>
#endif

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {
Expand Down Expand Up @@ -81,40 +79,9 @@ template <class T>
using is_multi_value =
std::integral_constant<bool, (is_container<T>{} and not std::is_convertible<T, std::string>{})>;

enum class color
{
reset = 0,
bold = 1,
underlined = 4,
fg_red = 31,
fg_green = 32,
fg_yellow = 33,
fg_blue = 34,
fg_default = 39,
bg_red = 41,
bg_green = 42,
bg_yellow = 43,
bg_blue = 44,
bg_default = 49
};
inline std::ostream& operator<<(std::ostream& os, const color& c)
{
#ifndef _WIN32
static const bool use_color = isatty(STDOUT_FILENO) != 0;
if(use_color)
return os << "\033[" << static_cast<std::size_t>(c) << "m";
#else
(void)c;
#endif
return os;
}

inline std::string colorize(color c, const std::string& s)
{
std::stringstream ss;
ss << c << s << color::reset;
return ss.str();
}
// Use color utilities from migraphx::color
using migraphx::color;
using migraphx::colorize;

template <class T>
struct type_name
Expand Down Expand Up @@ -433,7 +400,7 @@ struct argument_parser
auto required_usages = get_argument_usages(get_required_arguments());
if(required_usages.empty() and input_argument)
required_usages.push_back(input_argument->metavar);
required_usages.insert(required_usages.begin(), "<options>");
required_usages.push_back("<options>");
print_usage(required_usages);
std::cout << std::endl;
if(self.find_argument([](const auto& arg) { return arg.nargs == 0; }))
Expand Down Expand Up @@ -732,6 +699,12 @@ struct argument_parser
return result;
}

template <class F>
void post_action(F f)
{
actions.push_back(f);
}

private:
std::list<argument> arguments;
std::string exe_name = "";
Expand Down
19 changes: 7 additions & 12 deletions src/driver/command.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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
Expand Down Expand Up @@ -31,7 +31,6 @@
#include <migraphx/stringutils.hpp>

#include <unordered_map>
#include <utility>
#include <vector>

namespace migraphx {
Expand All @@ -41,9 +40,8 @@ inline namespace MIGRAPHX_INLINE_NS {
inline auto& get_commands()
{
// NOLINTNEXTLINE
static std::unordered_map<
std::string,
std::function<void(const std::string& exe_name, std::vector<std::string> args)>>
static std::unordered_map<std::string,
std::function<void(argument_parser&, std::vector<std::string>)>>
m;
return m;
}
Expand All @@ -68,13 +66,10 @@ const std::string& command_name()
}

template <class T>
void run_command(const std::string& exe_name,
const std::vector<std::string>& args,
bool add_help = false)
void run_command(argument_parser& ap, const std::vector<std::string>& args, bool add_help = false)
{
T x;
argument_parser ap;
ap.set_exe_name(exe_name + " " + command_name<T>());
ap.set_exe_name(ap.get_exe_name() + " " + command_name<T>());
if(add_help)
ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help());
x.parse(ap);
Expand All @@ -87,8 +82,8 @@ template <class T>
int auto_register_command()
{
auto& m = get_commands();
m[command_name<T>()] = [](const std::string& exe_name, const std::vector<std::string>& args) {
run_command<T>(exe_name, args, true);
m[command_name<T>()] = [](argument_parser& ap, const std::vector<std::string>& args) {
run_command<T>(ap, args, true);
};
return 0;
}
Expand Down
84 changes: 79 additions & 5 deletions src/driver/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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
Expand Down 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,72 @@ std::string get_formatted_timestamp(std::chrono::time_point<std::chrono::system_
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 const& level_str = params.back();
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));
ap.post_action([this](auto&&) { this->apply(); });
}

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;
}
};

} // namespace

namespace migraphx {
Expand Down Expand Up @@ -1005,6 +1073,11 @@ using namespace migraphx::driver; // NOLINT
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;

migraphx::driver::argument_parser ap;

// no argument, print the help infomration by default
if(args.empty())
{
Expand All @@ -1028,15 +1101,16 @@ int main(int argc, const char* argv[], const char* envp[])
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
auto start_time = std::chrono::system_clock::now();
std::cout << "[" << get_formatted_timestamp(start_time) << "]" << std::endl;

m.at(cmd)(argv[0],
{args.begin() + 1, args.end()}); // run driver command found in commands map
logger_options log_opts;
log_opts.parse(ap);
m.at(cmd)(ap, {args.begin() + 1, args.end()}); // run driver command found in commands map

// Dump all the MIGraphX (consumed) Environment Variables:
const auto mgx_env_map = migraphx::get_all_envs();
Expand All @@ -1059,7 +1133,7 @@ int main(int argc, const char* argv[], const char* envp[])
}
else
{
run_command<main_command>(argv[0], args);
run_command<main_command>(ap, args);
}

return 0;
Expand Down
Loading
Loading