就像
myLogger("who are you?", "I‘m ZRH", "I‘m in thread:", std::this_thread::get_id()); myLogger(1, "+", 2u, "=", "3");
多个thread输出形如:
2016-04-16 10:54:54 who are you? I‘m ZRH I‘m in thread: 6736
2016-04-16 10:54:54 1 + 2 = 3
2016-04-16 10:54:54 who are you? I‘m ZRH I‘m in thread: 13528
2016-04-16 10:54:54 1 + 2 = 3
2016-04-16 10:54:54 who are you? I‘m ZRH I‘m in thread: 3036
2016-04-16 10:54:54 1 + 2 = 3
用到了
- 括号 操作符重载
- C++11 chrono, iomanip 获取和格式化输出时间戳;
- C++11 lock_guard, 保证一条log记录是线程安全的;
- C++11 variadic template, 这样就可以做到随意的参数个数和参数类型(当然,要保证参数的可打印,比如有 << 操作符实现);
- C++11 list initialization, braced-init-list, 列表初始化的形式顺序打印log,这比递归实现好多了;
时间: 2024-10-29 01:11:23