diff --git a/api/include/opentelemetry/logs/log_record.h b/api/include/opentelemetry/logs/log_record.h index f8d905ba6e..70d07083e3 100644 --- a/api/include/opentelemetry/logs/log_record.h +++ b/api/include/opentelemetry/logs/log_record.h @@ -78,7 +78,7 @@ struct LogRecord trace::TraceId trace_id; // byte sequence trace::SpanId span_id; // byte sequence trace::TraceFlags trace_flags; // byte - Severity severity; // Severity enum that combines severity_text and severity_number + Severity severity; // Severity enum that combines severity_text and severity_number // other fields that will not be set by default nostd::string_view name; // string @@ -94,10 +94,10 @@ struct LogRecord LogRecord() { // Assign default values - timestamp = core::SystemTimestamp(std::chrono::seconds(0)); - severity = Severity::kDefault; - trace_id = trace::TraceId(); - span_id = trace::SpanId(); + timestamp = core::SystemTimestamp(std::chrono::seconds(0)); + severity = Severity::kDefault; + trace_id = trace::TraceId(); + span_id = trace::SpanId(); trace_flags = trace::TraceFlags(); } diff --git a/api/include/opentelemetry/logs/logger.h b/api/include/opentelemetry/logs/logger.h index 2f88ee038a..d74ecead36 100644 --- a/api/include/opentelemetry/logs/logger.h +++ b/api/include/opentelemetry/logs/logger.h @@ -54,31 +54,32 @@ class Logger * @throws No exceptions under any circumstances. */ - /** - * Logs a LogRecord, which contains all the fields of the Log Data Model. Normally called indirectly from other Log() Methods, but can be called directly for high detail. + /** + * Logs a LogRecord, which contains all the fields of the Log Data Model. Normally called + * indirectly from other Log() Methods, but can be called directly for high detail. * @param record A log record filled with information from the user. */ virtual void Log(const LogRecord &record) noexcept = 0; - ///Overloaded Log methods, which are simpler than calling a LogRecord directly - - /** + /// Overloaded Log methods, which are simpler than calling a LogRecord directly + + /** * Writes a log. * @param severity The log's severity * @param message The message to log */ inline void Log(Severity severity, nostd::string_view message) noexcept { - //Create a LogRecord to hold this information + // Create a LogRecord to hold this information LogRecord r; - r.severity = severity; - r.body = message; + r.severity = severity; + r.body = message; - //Call the main Log(LogRecord) method + // Call the main Log(LogRecord) method Log(r); } - /** + /** * Writes a log. * @param severity The log's severity * @param name The name of the log @@ -86,64 +87,63 @@ class Logger */ inline void Log(Severity severity, nostd::string_view name, nostd::string_view message) noexcept { - //Create a LogRecord to hold this information + // Create a LogRecord to hold this information LogRecord r; r.severity = severity; r.name = name; r.body = message; - //Call the main Log(LogRecord) method + // Call the main Log(LogRecord) method Log(r); } - /** + /** * Writes a log. * @param severity The severity of the log, from 1 to 24 * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Log(Severity severity, const T &attributes) noexcept { - //Create a LogRecord to hold this information + // Create a LogRecord to hold this information LogRecord r; - r.severity = severity; - r.attributes = nostd::shared_ptr(new common::KeyValueIterableView{attributes}); + r.severity = severity; + r.attributes = nostd::shared_ptr( + new common::KeyValueIterableView{attributes}); - //Call the main Log(LogRecord) method + // Call the main Log(LogRecord) method Log(r); } - /** + /** * Writes a log. * @param severity The severity of the log, from 1 to 24 * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Log(Severity severity, nostd::string_view name, const T &attributes) noexcept { - //Create a LogRecord to hold this information + // Create a LogRecord to hold this information LogRecord r; r.severity = severity; r.name = name; - r.attributes = nostd::shared_ptr(new common::KeyValueIterableView{attributes}); + r.attributes = nostd::shared_ptr( + new common::KeyValueIterableView{attributes}); - //Call the main Log(LogRecord) method + // Call the main Log(LogRecord) method Log(r); } - /** + /** * Writes a log with a severity of trace. * @param message The message to log */ - inline void Trace(nostd::string_view message) noexcept - { - Log(Severity::kTrace, message); - } + inline void Trace(nostd::string_view message) noexcept { Log(Severity::kTrace, message); } - /** + /** * Writes a log with a severity of trace. * @param name The name of the log * @param message The message to log @@ -153,39 +153,36 @@ class Logger Log(Severity::kTrace, name, message); } - /** + /** * Writes a log with a severity of trace. * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Trace(const T &attributes) noexcept { Log(Severity::kTrace, attributes); } - /** + /** * Writes a log with a severity of trace. * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Trace(nostd::string_view name, const T &attributes) noexcept { Log(Severity::kTrace, name, attributes); } - /** + /** * Writes a log with a severity of debug. * @param message The message to log */ - inline void Debug(nostd::string_view message) noexcept - { - Log(Severity::kDebug, message); - } + inline void Debug(nostd::string_view message) noexcept { Log(Severity::kDebug, message); } - /** + /** * Writes a log with a severity of debug. * @param name The name of the log * @param message The message to log @@ -195,39 +192,36 @@ class Logger Log(Severity::kDebug, name, message); } - /** + /** * Writes a log with a severity of debug. * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Debug(const T &attributes) noexcept { Log(Severity::kDebug, attributes); } - /** + /** * Writes a log with a severity of debug. * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Debug(nostd::string_view name, const T &attributes) noexcept { Log(Severity::kDebug, name, attributes); } - /** + /** * Writes a log with a severity of info. * @param message The message to log */ - inline void Info(nostd::string_view message) noexcept - { - Log(Severity::kInfo, message); - } + inline void Info(nostd::string_view message) noexcept { Log(Severity::kInfo, message); } - /** + /** * Writes a log with a severity of info. * @param name The name of the log * @param message The message to log @@ -237,39 +231,36 @@ class Logger Log(Severity::kInfo, name, message); } - /** + /** * Writes a log with a severity of info. * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Info(const T &attributes) noexcept { Log(Severity::kInfo, attributes); } - /** + /** * Writes a log with a severity of info. * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Info(nostd::string_view name, const T &attributes) noexcept { Log(Severity::kInfo, name, attributes); } - /** + /** * Writes a log with a severity of warn. * @param message The message to log */ - inline void Warn(nostd::string_view message) noexcept - { - Log(Severity::kWarn, message); - } + inline void Warn(nostd::string_view message) noexcept { Log(Severity::kWarn, message); } - /** + /** * Writes a log with a severity of warn. * @param name The name of the log * @param message The message to log @@ -279,39 +270,36 @@ class Logger Log(Severity::kWarn, name, message); } - /** + /** * Writes a log with a severity of warn. * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Warn(const T &attributes) noexcept { Log(Severity::kWarn, attributes); } - /** + /** * Writes a log with a severity of warn. * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Warn(nostd::string_view name, const T &attributes) noexcept { Log(Severity::kWarn, name, attributes); } - /** + /** * Writes a log with a severity of error. * @param message The message to log */ - inline void Error(nostd::string_view message) noexcept - { - Log(Severity::kError, message); - } + inline void Error(nostd::string_view message) noexcept { Log(Severity::kError, message); } - /** + /** * Writes a log with a severity of error. * @param name The name of the log * @param message The message to log @@ -321,39 +309,36 @@ class Logger Log(Severity::kError, name, message); } - /** + /** * Writes a log with a severity of error. * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Error(const T &attributes) noexcept { Log(Severity::kError, attributes); } - /** + /** * Writes a log with a severity of error. * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Error(nostd::string_view name, const T &attributes) noexcept { Log(Severity::kError, name, attributes); } - /** + /** * Writes a log with a severity of fatal. * @param message The message to log */ - inline void Fatal(nostd::string_view message) noexcept - { - Log(Severity::kFatal, message); - } + inline void Fatal(nostd::string_view message) noexcept { Log(Severity::kFatal, message); } - /** + /** * Writes a log with a severity of fatal. * @param name The name of the log * @param message The message to log @@ -363,30 +348,29 @@ class Logger Log(Severity::kFatal, name, message); } - /** + /** * Writes a log with a severity of fatal. * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Fatal(const T &attributes) noexcept { Log(Severity::kFatal, attributes); } - /** + /** * Writes a log with a severity of fatal. * @param name The name of the log * @param attributes A key/value object */ template ::value> * = nullptr> + nostd::enable_if_t::value> * = nullptr> inline void Fatal(nostd::string_view name, const T &attributes) noexcept { Log(Severity::kFatal, name, attributes); } - /** Future enhancement: templated method for objects / custom types (e.g. JSON, XML, custom * classes, etc) **/ // template virtual void log(T &some_obj) noexcept; diff --git a/api/include/opentelemetry/logs/noop.h b/api/include/opentelemetry/logs/noop.h index 43406771d9..489074676b 100644 --- a/api/include/opentelemetry/logs/noop.h +++ b/api/include/opentelemetry/logs/noop.h @@ -41,7 +41,7 @@ class NoopLogger final : public Logger public: NoopLogger() = default; - void Log(const LogRecord& record) noexcept override {} + void Log(const LogRecord &record) noexcept override {} const nostd::string_view GetName() noexcept override { return "noop logger"; }; }; diff --git a/api/test/logs/logger_test.cc b/api/test/logs/logger_test.cc index b05e333a93..cbbe66cce5 100644 --- a/api/test/logs/logger_test.cc +++ b/api/test/logs/logger_test.cc @@ -45,7 +45,7 @@ static opentelemetry::nostd::shared_ptr record_; // Define a basic Logger class class TestLogger : public Logger { - void Log(const LogRecord &record) noexcept override + void Log(const LogRecord &record) noexcept override { record_ = opentelemetry::nostd::shared_ptr(new LogRecord(record)); } @@ -86,11 +86,12 @@ TEST(Logger, LogMethodOverloads) auto test_provider = shared_ptr(new TestProvider()); Provider::SetLoggerProvider(test_provider); - auto lp = Provider::GetLoggerProvider(); + auto lp = Provider::GetLoggerProvider(); auto logger = lp->GetLogger("TestLogger"); - // Check that calling the Log() overloads correctly constructs a log record which is automatically put into the static logger_ for testing - + // Check that calling the Log() overloads correctly constructs a log record which is automatically + // put into the static logger_ for testing + // Test Log(severity, name, message) method logger->Log(Severity::kError, "Log Name", "This is the log message"); ASSERT_EQ(record_->severity, Severity::kError);