我正在尝试用 C++ 制作一个优雅的日志记录系统。我目前正在使用printf()
,虽然cout
也可以是一个选项。
我想要实现的是这样的
console_log( "ClassName", "funcName", "Message." );
我目前的代码很简单:
static void console_log( const std::string & className, const std::string & funcName, const std::string & message ) {
printf( "%s : %s : %s\n", className.c_str(), funcName.c_str(), message.c_str() );
}
它打印得很好,像这样
// For example:
console_log( "MenuPanel", "selectedThisButton", "Something happened." );
console_log( "MenuPanel", "selectedAnotherButton", "Another thing happened." );
// Output:
MenuPanel : selectedThisButton : Something happened.
MenuPanel : selectedAnotherButton : Another thing happened.
但是,我希望它以类似表格的方式打印,其中所有“列”都正确对齐。例如:
MenuPanel : selectedThisButton : Something happened.
MenuPanel : selectedAnotherButton : Another thing happened.
如何使第一个和第二个“列”具有完全相同的宽度/字符数,并在必要时使用额外的空格?它不需要是动态的。将“列”设置为 16 个字符之类的东西就可以了。
不过,我不希望将任何第三方库用于如此简单的事情。如果可能的话,不boost
。