Skip to main content
deleted 4 characters in body
Source Link

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

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 asked.
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

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 usage 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
Tweeted twitter.com/StackCodeReview/status/1600550718045405191
prettify hint
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

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 asked.
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
This is an example log message
This is a log message with one argument
This is a log message with 2 arguments

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 asked.
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

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 asked.
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
Became Hot Network Question
added 475 characters in body
Source Link

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 asked.
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

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

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 asked.
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
Source Link
Loading