我正在寫我自己的記錄器。我知道那裏有很多,但我想自己寫。它有消息在離開示波器時被記錄。所以如果我打電話給Logger::Error(__FILE__,__LINE__) << "some error"
,它會被直接記錄下來,因爲沒有對變量進行分配。記錄器記錄2次而不是一次,因爲複製
但我想有一個消息記錄範圍的時間。因此它確定了自創建和刪除以來的時間。因此我需要它被分配給一個變量中的範圍與例如本馬爾科:#define LOG_SCOPE_TIME LogTimer ___t = Logger::Timer(__FILE__,__LINE__)
它可用於這樣的:
int main()
{
{
LOG_SCOPE_TIME << "Some scope";
//do something to mesure
}
}
輸出示例:
[timer][File:main.cpp][Line:19][thread:8024][21-05-2015][13:15:11] Some scope[0µs]
[timer][File:main.cpp][Line:19][thread:8788][21-05-2015][13:15:11] Some scope[118879µs]
但這實際上導致2日誌。臨時創建的第一個LogTime對象(時間爲0μs),第二個爲實際示波器時間。
我該如何預防?有什麼建議麼?這裏是一個簡化的例子:
#include <iostream>
#include <chrono>
class LogTimer {
std::string str;
std::chrono::high_resolution_clock::time_point m_start;
public:
LogTimer(const std::string& file, int i)
: m_start(std::chrono::high_resolution_clock::now())
{
str = file + ':' + std::to_string(i);
}
~LogTimer() {
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>
(end - m_start).count();
std::cout << str << " [" << duration << "µs]\n";
}
LogTimer& operator<<(const std::string& p) {
str += '[' + p + ']';
return *this;
}
};
namespace Logger {
LogTimer Timer(const std::string& f, int i) {
return LogTimer(f, i);
}
}
#define LOG_SCOPE_TIME LogTimer ___t = Logger::Timer(__FILE__,__LINE__)
int main()
{
LOG_SCOPE_TIME << "something"; // logs two lines
}
你能想出一個更簡單的例子嗎?這是很多代碼。 – Barry
虐待盡我所能給我幾分鐘 – BennX
這裏是一個[簡短的例子](http://coliru.stacked-crooked.com/a/b00dabea34c4364f) – Barry