This class has a Log function that appends text to a log text file. It takes a format string and a variable amount of arguments, much like a printf sort of function. It then writes the formatted string with the interpolated arguments to the file.
I wanted to know if there are any edge cases I might've missed, or if anything can be done to improve this code?
class Logger {
private:
Logger() = delete;
Logger(const Logger&) = delete;
public:
template <class... FormatArgs>
static void Log(const std::string_view format, FormatArgs&&... args) {
std::ofstream logfile("log.txt", std::ios::app);
logfile << std::vformat(format, std::make_format_args(args...)) << std::endl;
}
};
Edit
Here are some unit tests as askedusage examples*.Logger::Log("This is an example log message");
Logger::Log("This is a log message with {}", "one argument");
Logger::Log("This is a log message with {} {}", 2, "arguments");
Should produce a log.txt text file in the process's working directory with the following contents.
This is an example log message
This is a log message with one argument
This is a log message with 2 arguments