本篇介紹TensorRT的logger.h
,logger.cpp
及logging.h
這三個檔案。logger.h
作為與外部聯繫的接口,logging.h
則負則log功能的具體實現。
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LOGGER_H
#define LOGGER_H
#include "logging.h"
extern Logger gLogger;
extern LogStreamConsumer gLogVerbose;
extern LogStreamConsumer gLogInfo;
extern LogStreamConsumer gLogWarning;
extern LogStreamConsumer gLogError;
extern LogStreamConsumer gLogFatal;
void setReportableSeverity(Logger::Severity severity);
#endif // LOGGER_H
logger.h
相當於一個與外部溝通的接口,外部的cpp檔如果想要使用與log有關的功能,只要引用logger.h
即可。
log功能的具體實現則由logger.cpp
,logging.h
負責。
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "logger.h"
#include "logging.h"
//from logging.h:
//Logger建構子:Logger(Severity severity = Severity::kWARNING)
Logger gLogger{Logger::Severity::kINFO};
//from logging.h:
//LogStreamConsumer建構子:LogStreamConsumer(LogStreamConsumer&& other)
//LOG_VERBOSE函數,定義於logging.h:LogStreamConsumer LOG_VERBOSE(const Logger& logger)
LogStreamConsumer gLogVerbose{LOG_VERBOSE(gLogger)};
LogStreamConsumer gLogInfo{LOG_INFO(gLogger)};
LogStreamConsumer gLogWarning{LOG_WARN(gLogger)};
LogStreamConsumer gLogError{LOG_ERROR(gLogger)};
LogStreamConsumer gLogFatal{LOG_FATAL(gLogger)};
//此處使用了list initialization
//Logger是nvinfer1::ILogger的子類別,繼承了父類別的Severity
void setReportableSeverity(Logger::Severity severity)
{
//呼叫Logger::setReportableSeverity
gLogger.setReportableSeverity(severity);
//呼叫LogStreamConsumer::setReportableSeverity
gLogVerbose.setReportableSeverity(severity);
gLogInfo.setReportableSeverity(severity);
gLogWarning.setReportableSeverity(severity);
gLogError.setReportableSeverity(severity);
gLogFatal.setReportableSeverity(severity);
}
logger.cpp
借用了logging.h
中定義好的類別:Logger
,LogStreamConsumer
,Logger::Severity
及函數LOG_VERBOSE
,LOG_INFO
,LOG_WARN
,LOG_ERROR
,LOG_FATAL
等來定義logger.h
裡宣告的變數。
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TENSORRT_LOGGING_H
#define TENSORRT_LOGGING_H
#include "NvInferRuntimeCommon.h"
#include <cassert>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
/*
enum nvinfer1::ILogger::Severity : int
The severity corresponding to a log message.
Enumerator
kINTERNAL_ERROR
An internal error has occurred. Execution is unrecoverable.
kERROR
An application error has occurred.
kWARNING
An application error has been discovered, but TensorRT has recovered or fallen back to a default.
kINFO
Informational messages with instructional information.
kVERBOSE
Verbose messages with debugging information.
*/
using Severity = nvinfer1::ILogger::Severity;
/*
typedef basic_stringbuf<char> stringbuf;
Stream buffer to read from and write to string objects.
*/
//本身是一個std::stringbuf,可以做為字串的緩衝區
//另外還帶了std::ostream&型別的成員變數,用來為它自身緩衝區裡的字串做輸出
class LogStreamConsumerBuffer : public std::stringbuf
{
public:
LogStreamConsumerBuffer(std::ostream& stream, const std::string& prefix, bool shouldLog)
: mOutput(stream)
, mPrefix(prefix)
, mShouldLog(shouldLog)
{
}
//move constructor
LogStreamConsumerBuffer(LogStreamConsumerBuffer&& other)
: mOutput(other.mOutput)
{
}
~LogStreamConsumerBuffer()
{
//LogStreamConsumerBuffer繼承自std::stringbuf,
//而std::stringbuf又繼承自std::streambuf,
//所以這裡可以直接調用std::streambuf::pbase及std::streambuf::pptr
// std::streambuf::pbase() gives a pointer to the beginning of the buffered part of the output sequence
//即回傳一個指向輸出序列開頭的指標
// std::streambuf::pptr() gives a pointer to the current position of the output sequence
//即回傳一個指向輸出序列中當前位置的指標
// if the pointer to the beginning is not equal to the pointer to the current position,
// call putOutput() to log the output to the stream
//如果一個當前位置並非輸出序列的開頭,便將輸出序列中的東西給打印出來
if (pbase() != pptr())
{
putOutput();
}
}
// synchronizes the stream buffer and returns 0 on success
// synchronizing the stream buffer consists of inserting the buffer contents into the stream,
// resetting the buffer and flushing the stream
// 虛擬函數,在子類別中可以重新定義(override)它
// 當我們使用父類別的pointer或reference指向一個子類別的物件時,
// C++能在runtime知道要調用子類別的版本而非父類別的版本
//(即runtime polymorphism)
virtual int sync()
{
putOutput();
return 0;
}
void putOutput()
{
if (mShouldLog)
{
// prepend timestamp
std::time_t timestamp = std::time(nullptr);
tm* tm_local = std::localtime(×tamp);
std::cout << "[";
std::cout << std::setw(2) << std::setfill('0') << 1 + tm_local->tm_mon << "/";
std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_mday << "/";
std::cout << std::setw(4) << std::setfill('0') << 1900 + tm_local->tm_year << "-";
std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_hour << ":";
std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_min << ":";
std::cout << std::setw(2) << std::setfill('0') << tm_local->tm_sec << "] ";
//LogStreamConsumerBuffer繼承自std::stringbuf,
//所以這裡可以直接調用std::stringbuf::str()
// std::stringbuf::str() gets the string contents of the buffer
//str()用於獲取buffer裡的字串
//"<<": Insert formatted output. This operator (<<) applied to an output stream is known as insertion operator.3
// insert the buffer contents pre-appended by the appropriate prefix into the stream
//將mPrefix及buffer裡的內容插入到mOutput這個ostream中
mOutput << mPrefix << str();
// set the buffer to empty
//將buffer裡的值設為"",即清空buffer
str("");
// flush the stream
//std::ostream::flush(): Flush output stream buffer
mOutput.flush();
}
}
void setShouldLog(bool shouldLog)
{
mShouldLog = shouldLog;
}
private:
//參考成員變數
std::ostream& mOutput;
std::string mPrefix;
bool mShouldLog;
};
//!
//! \class LogStreamConsumerBase
//! \brief Convenience object used to initialize LogStreamConsumerBuffer before std::ostream in LogStreamConsumer
//!
class LogStreamConsumerBase
{
public:
//注意這裡initializer list的寫法:我們可以在initializer list調用成員變數的constructor
//LogStreamConsumerBuffer建構子:LogStreamConsumerBuffer(std::ostream& stream, const std::string& prefix, bool shouldLog)
LogStreamConsumerBase(std::ostream& stream, const std::string& prefix, bool shouldLog)
: mBuffer(stream, prefix, shouldLog)
{
}
protected:
LogStreamConsumerBuffer mBuffer;
};
//!
//! \class LogStreamConsumer
//! \brief Convenience object used to facilitate use of C++ stream syntax when logging messages.
//! Order of base classes is LogStreamConsumerBase and then std::ostream.
//! This is because the LogStreamConsumerBase class is used to initialize the LogStreamConsumerBuffer member field
//! in LogStreamConsumer and then the address of the buffer is passed to std::ostream.
//! This is necessary to prevent the address of an uninitialized buffer from being passed to std::ostream.
//! Please do not change the order of the parent classes.
//!
//本身是std::ostream,
//還自帶了LogStreamConsumerBuffer(即std::stringbuf型別的成員變數)
//其建構子接受Severity型別的變數severity做為參數,用以表示其輸出訊息的重要程度,越小越重要
//訊息實際輸出與否是由reportableSeverity控制,當severity小於reportableSeverity時才會輸出
class LogStreamConsumer : protected LogStreamConsumerBase, public std::ostream
{
public:
//! \brief Creates a LogStreamConsumer which logs messages with level severity.
//! Reportable severity determines if the messages are severe enough to be logged.
//此處initializer list的順序:先LogStreamConsumerBase後std::ostream
LogStreamConsumer(Severity reportableSeverity, Severity severity)
//LogStreamConsumerBase(stream, prefix, shouldLog)
: LogStreamConsumerBase(severityOstream(severity), severityPrefix(severity), severity <= reportableSeverity)
//mBuffer繼承自LogStreamConsumerBase
//注意這裡是先呼叫LogStreamConsumerBase建構子,
//使得mBuffer指向一個有效的位置後,
//才使用mBuffer來初始化ostream
, std::ostream(&mBuffer) // links the stream buffer with the stream
, mShouldLog(severity <= reportableSeverity)
, mSeverity(severity)
{
}
//move constructor
LogStreamConsumer(LogStreamConsumer&& other)
: LogStreamConsumerBase(severityOstream(other.mSeverity), severityPrefix(other.mSeverity), other.mShouldLog)
, std::ostream(&mBuffer) // links the stream buffer with the stream
, mShouldLog(other.mShouldLog)
, mSeverity(other.mSeverity)
{
}
void setReportableSeverity(Severity reportableSeverity)
{
//如果reportableSeverity大於等於事先設定好的mSeverity,才會開啟log功能
mShouldLog = mSeverity <= reportableSeverity;
mBuffer.setShouldLog(mShouldLog);
}
private:
//依據傳入的參數severity決定要寫到標準輸出還是標準錯誤
static std::ostream& severityOstream(Severity severity)
{
//決定寫到標準輸出還是標準錯誤
return severity >= Severity::kINFO ? std::cout : std::cerr;
}
static std::string severityPrefix(Severity severity)
{
switch (severity)
{
//F for FATAL?
case Severity::kINTERNAL_ERROR: return "[F] ";
case Severity::kERROR: return "[E] ";
case Severity::kWARNING: return "[W] ";
case Severity::kINFO: return "[I] ";
case Severity::kVERBOSE: return "[V] ";
default: assert(0); return "";
}
}
bool mShouldLog;
Severity mSeverity;
};
//! \class Logger
//!
//! \brief Class which manages logging of TensorRT tools and samples
//!
//! \details This class provides a common interface for TensorRT tools and samples to log information to the console,
//! and supports logging two types of messages:
//!
//! - Debugging messages with an associated severity (info, warning, error, or internal error/fatal)
//! - Test pass/fail messages
//!
//! The advantage of having all samples use this class for logging as opposed to emitting directly to stdout/stderr is
//! that the logic for controlling the verbosity and formatting of sample output is centralized in one location.
//!
//! In the future, this class could be extended to support dumping test results to a file in some standard format
//! (for example, JUnit XML), and providing additional metadata (e.g. timing the duration of a test run).
//!
//! TODO: For backwards compatibility with existing samples, this class inherits directly from the nvinfer1::ILogger
//! interface, which is problematic since there isn't a clean separation between messages coming from the TensorRT
//! library and messages coming from the sample.
//!
//! In the future (once all samples are updated to use Logger::getTRTLogger() to access the ILogger) we can refactor the
//! class to eliminate the inheritance and instead make the nvinfer1::ILogger implementation a member of the Logger
//! object.
class Logger : public nvinfer1::ILogger
{
public:
Logger(Severity severity = Severity::kWARNING)
: mReportableSeverity(severity)
{
}
//!
//! \enum TestResult
//! \brief Represents the state of a given test
//!
enum class TestResult
{
kRUNNING, //!< The test is running
kPASSED, //!< The test passed
kFAILED, //!< The test failed
//放棄,延遲
kWAIVED //!< The test was waived
};
//!
//! \brief Forward-compatible method for retrieving the nvinfer::ILogger associated with this Logger
//! \return The nvinfer1::ILogger associated with this Logger
//!
//! TODO Once all samples are updated to use this method to register the logger with TensorRT,
//! we can eliminate the inheritance of Logger from ILogger
//!
nvinfer1::ILogger& getTRTLogger()
{
return *this;
}
//!
//! \brief Implementation of the nvinfer1::ILogger::log() virtual method
//!
//! Note samples should not be calling this function directly; it will eventually go away once we eliminate the
//! inheritance from nvinfer1::ILogger
//!
//這裡是override TensorRT/include/NvInferRuntimeCommon.h中宣告了純虛擬函數:
//virtual void log(Severity severity, const char* msg) TRTNOEXCEPT = 0;
void log(Severity severity, const char* msg) override
{
//LogStreamConsumer是std::ostream的子類別,
//所以可以使用"<<"運算子
LogStreamConsumer(mReportableSeverity, severity) << "[TRT] " << std::string(msg) << std::endl;
}
//!
//! \brief Method for controlling the verbosity of logging output
//!
//! \param severity The logger will only emit messages that have severity of this level or higher.
//!
void setReportableSeverity(Severity severity)
{
mReportableSeverity = severity;
}
//!
//! \brief Opaque handle that holds logging information for a particular test
//!
//! This object is an opaque handle to information used by the Logger to print test results.
//! The sample must call Logger::defineTest() in order to obtain a TestAtom that can be used
//! with Logger::reportTest{Start,End}().
//!
//用於記錄測試相關資訊的數據結構
class TestAtom
{
public:
TestAtom(TestAtom&&) = default;
private:
//Logger為TestAtom的friend class
friend class Logger;
TestAtom(bool started, const std::string& name, const std::string& cmdline)
: mStarted(started)
, mName(name)
, mCmdline(cmdline)
{
}
bool mStarted;
std::string mName;
std::string mCmdline;
};
//!
//! \brief Define a test for logging
//!
//! \param[in] name The name of the test. This should be a string starting with
//! "TensorRT" and containing dot-separated strings containing
//! the characters [A-Za-z0-9_].
//! For example, "TensorRT.sample_googlenet"
//! \param[in] cmdline The command line used to reproduce the test
//
//! \return a TestAtom that can be used in Logger::reportTest{Start,End}().
//!
//根據傳入的參數name及cmdline新建一個TestAtom物件後回傳
static TestAtom defineTest(const std::string& name, const std::string& cmdline)
{
return TestAtom(false, name, cmdline);
}
//!
//! \brief A convenience overloaded version of defineTest() that accepts an array of command-line arguments
//! as input
//!
//! \param[in] name The name of the test
//! \param[in] argc The number of command-line arguments
//! \param[in] argv The array of command-line arguments (given as C strings)
//!
//! \return a TestAtom that can be used in Logger::reportTest{Start,End}().
//上面defineTest函數的wrapper:將argc及argv解析成正確的cmdline後呼叫上面defineTest函數
static TestAtom defineTest(const std::string& name, int argc, char const* const* argv)
{
auto cmdline = genCmdlineString(argc, argv);
return defineTest(name, cmdline);
}
//!
//! \brief Report that a test has started.
//!
//! \pre reportTestStart() has not been called yet for the given testAtom
//!
//! \param[in] testAtom The handle to the test that has started
//!
//輸出測試狀態(為running)並將TestAtom物件的mStarted屬性設為true
static void reportTestStart(TestAtom& testAtom)
{
reportTestResult(testAtom, TestResult::kRUNNING);
assert(!testAtom.mStarted);
testAtom.mStarted = true;
}
//!
//! \brief Report that a test has ended.
//!
//! \pre reportTestStart() has been called for the given testAtom
//!
//! \param[in] testAtom The handle to the test that has ended
//! \param[in] result The result of the test. Should be one of TestResult::kPASSED,
//! TestResult::kFAILED, TestResult::kWAIVED
//!
//確認運行狀態後輸出測試結果
static void reportTestEnd(const TestAtom& testAtom, TestResult result)
{
assert(result != TestResult::kRUNNING);
//確認testAtom.mStarted是unset的
assert(testAtom.mStarted);
reportTestResult(testAtom, result);
}
//測試結束的情況之一
static int reportPass(const TestAtom& testAtom)
{
reportTestEnd(testAtom, TestResult::kPASSED);
return EXIT_SUCCESS;
}
//測試結束的情況之二
static int reportFail(const TestAtom& testAtom)
{
reportTestEnd(testAtom, TestResult::kFAILED);
return EXIT_FAILURE;
}
//測試結束的情況之三
static int reportWaive(const TestAtom& testAtom)
{
reportTestEnd(testAtom, TestResult::kWAIVED);
//kWAIVED狀態也算成功?
return EXIT_SUCCESS;
}
//根據參數pass決定要調用reportPass還是reportFail
static int reportTest(const TestAtom& testAtom, bool pass)
{
return pass ? reportPass(testAtom) : reportFail(testAtom);
}
Severity getReportableSeverity() const
{
return mReportableSeverity;
}
private:
//!
//! \brief returns an appropriate string for prefixing a log message with the given severity
//!
static const char* severityPrefix(Severity severity)
{
switch (severity)
{
case Severity::kINTERNAL_ERROR: return "[F] ";
case Severity::kERROR: return "[E] ";
case Severity::kWARNING: return "[W] ";
case Severity::kINFO: return "[I] ";
case Severity::kVERBOSE: return "[V] ";
default: assert(0); return "";
}
}
//!
//! \brief returns an appropriate string for prefixing a test result message with the given result
//!
//將enum class TestResult類型的result轉換為const char*
static const char* testResultString(TestResult result)
{
switch (result)
{
case TestResult::kRUNNING: return "RUNNING";
case TestResult::kPASSED: return "PASSED";
case TestResult::kFAILED: return "FAILED";
case TestResult::kWAIVED: return "WAIVED";
default: assert(0); return "";
}
}
//!
//! \brief returns an appropriate output stream (cout or cerr) to use with the given severity
//!
//依據傳入的參數severity來決定是回傳std::cout或std::cerr
static std::ostream& severityOstream(Severity severity)
{
return severity >= Severity::kINFO ? std::cout : std::cerr;
}
//!
//! \brief method that implements logging test results
//!
//輸出測試結果到severityOstream(Severity::kINFO)這個std::ostream&裡
static void reportTestResult(const TestAtom& testAtom, TestResult result)
{
severityOstream(Severity::kINFO) << "&&&& " << testResultString(result) << " " << testAtom.mName << " # "
<< testAtom.mCmdline << std::endl;
}
//!
//! \brief generate a command line string from the given (argc, argv) values
//!
//解析argc及argv,設定好ss的buffer裡的內容後,取出其值回傳
static std::string genCmdlineString(int argc, char const* const* argv)
{
//std::stringstream : Stream class to operate on strings.
//std::ostream::operator<< : Insert formatted output
//std::stringstream::str : returns a string object with a copy of the current contents of the stream.
std::stringstream ss;
for (int i = 0; i < argc; i++)
{
if (i > 0)
ss << " ";
ss << argv[i];
}
return ss.str();
}
Severity mReportableSeverity;
};
namespace
{
//!
//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kVERBOSE
//!
//! Example usage:
//!
//! LOG_VERBOSE(logger) << "hello world" << std::endl;
//!
inline LogStreamConsumer LOG_VERBOSE(const Logger& logger)
{
return LogStreamConsumer(logger.getReportableSeverity(), Severity::kVERBOSE);
}
//!
//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kINFO
//!
//! Example usage:
//!
//! LOG_INFO(logger) << "hello world" << std::endl;
//!
inline LogStreamConsumer LOG_INFO(const Logger& logger)
{
return LogStreamConsumer(logger.getReportableSeverity(), Severity::kINFO);
}
//!
//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kWARNING
//!
//! Example usage:
//!
//! LOG_WARN(logger) << "hello world" << std::endl;
//!
inline LogStreamConsumer LOG_WARN(const Logger& logger)
{
return LogStreamConsumer(logger.getReportableSeverity(), Severity::kWARNING);
}
//!
//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kERROR
//!
//! Example usage:
//!
//! LOG_ERROR(logger) << "hello world" << std::endl;
//!
inline LogStreamConsumer LOG_ERROR(const Logger& logger)
{
return LogStreamConsumer(logger.getReportableSeverity(), Severity::kERROR);
}
//!
//! \brief produces a LogStreamConsumer object that can be used to log messages of severity kINTERNAL_ERROR
// ("fatal" severity)
//!
//! Example usage:
//!
//! LOG_FATAL(logger) << "hello world" << std::endl;
//!
inline LogStreamConsumer LOG_FATAL(const Logger& logger)
{
return LogStreamConsumer(logger.getReportableSeverity(), Severity::kINTERNAL_ERROR);
}
} // anonymous namespace
#endif // TENSORRT_LOGGING_H
LogStreamConsumerBuffer
類別繼承自std::stringbuf
,管理著字串緩沖區。
LogStreamConsumerBase
:LogStreamConsumerBuffer
的wrapper。
LogStreamConsumer
:繼承自LogStreamConsumerBase
及std::ostream
,所以本身兼具字串緩沖區及ostream
的角色。
Logger
:繼承自nvinfer1::ILogger
。透過調用LogStreamConsumer
物件來進行輸出。
LOG_VERBOSE
,LOG_INFO
,LOG_WARN
,LOG_ERROR
,LOG_FATAL
:這些函數的作用是生成相對應severity的LogStreamConsumer
物件,被用於logger.cpp
中gLogVerbose
,gLogInfo
,gLogWarning
,gLogError
,gLogFatal
等變數的定義中。
在logger.h
中宣告gLogger
,gLogVerbose
時使用了關鍵字extern
,更多介紹可參考C extern關鍵字。
在logger.cpp
中定義gLogger
,gLogVerbose
等變數時使用了所謂的list initialization,更多介紹可參考C++ list initialization。
在logging.h
中將nvinfer1::ILogger::Severity
取了一個別名為Severity
,更多介紹可參考C++ using。
在logging.h
中使用initializer list對LogStreamConsumerBuffer
的成員變數mOutput
進行初始化,同樣地,對於LogStreamConsumerBase
的成員變數mBuffer
,我們一樣使用initializer list進行初始化,更多介紹可參考C++ member initializer list。
在logging.h
中使用了move constructor:
LogStreamConsumerBuffer(LogStreamConsumerBuffer&& other)
: mOutput(other.mOutput)
{
}
關於move constructor的介紹,詳見C++ move semantics。
與時間相關的部份己被筆者改成可執行的代碼,可至cpp-code-snippets/time_t_tm_cout.cpp查看。
在logging.h
中LogStreamConsumer
繼承自LogStreamConsumerBase
及std::ostream
兩個父類別,並且在官方註釋中強調這兩個父類別的順序不可被調換,背後原因詳見C++多重繼承的順序。
logging.h
中在定義Logger
的成員函數log
時,後面加了一個關鍵字override
,這是什麼意思呢?
還有在TensorRT/include/NvInferRuntimeCommon.h
中定義nvinfer1::ILogger
的成員函數log
時,在它前面加了一個virtual
,並在後面又加了=0
,這又是什麼意思呢?詳見C++ (pure)virutal function & abstract class。
TestAtom
類別被定義在Logger
類別內,這是所謂的"nested class",詳見C++ nested class。
在TestAtom
類別的定義中,有這麼一句:friend class Logger;
。這是將Logger
這個類別定義為TestAtom
類別的friend class,詳見C++ friend class。
在官方註釋中說明TestAtom
這個類別是Opaque handle that holds logging information for a particular test
,這是什麼意思呢?詳見C++ (opaque) handle。
在Logger
類別中定義了一個enum class TestResult
,關於enum class,詳見C++ enum class。
在LogStreamConsumer
類別的成員函數severityPrefix
定義中用到了assert(0)
,這是什麼意思呢?詳見C++ assert(0)。
在logging.h
的Logger
類別的成員函數reportPass
,reportFail
及reportWaive
中,使用了EXIT_SUCCESS
, EXIT_FAILURE
這兩個常數,關於它們的介紹,詳見C++ EXIT_SUCCESS, EXIT_FAILURE。
logging.h
中使用一個unnamed namespace把LOG_VERBOSE
, LOG_INFO
, LOG_WARN
, LOG_ERROR
, LOG_FATAL
等函數包起來,為何要這麼做呢?詳見C++ translation unit & anonymous namespace(unnamed namespace)。
在logging.h
裡定義了LOG_VERBOSE
, LOG_INFO
, LOG_WARN
, LOG_ERROR
, LOG_FATAL
等inline function。此處為何要將它們定義為inline
的呢?詳見C++ inline function。
cpp-code-snippets/time_t_tm_cout.cpp
C++ (pure)virutal function & abstract class
C++ EXIT_SUCCESS, EXIT_FAILURE
C++ translation unit & anonymous namespace(unnamed namespace)