Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Variadic templates example: simple logger
#include <chrono>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
using std::chrono::system_clock;
template <typename... Args>
void log(const char* format, Args... args) {
string formatAsString = "[%s] ";
formatAsString += format;
formatAsString += "\n";
const time_t currentTime = system_clock::to_time_t(system_clock::now());
printf(formatAsString.c_str(), strtok(ctime(&currentTime), "\n"), args...);
int main() {
log("Example 4: Simple logger");
log("Test message %d", 1);
log("Multiple elements %d, %.3f", 10, 152.39798);
log("IO Error: Could not load file %s", "hello_variadic.txt");
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment