HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
capture.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <cstdarg>
7 #include "core/common/gsl.h"
8 #include "core/common/common.h"
11 
12 namespace onnxruntime {
13 namespace logging {
14 
15 class Logger;
16 enum class DataType;
17 
18 /**
19  Class to capture the details of a log message.
20 */
21 class Capture {
22  public:
23  /**
24  Initializes a new instance of the Capture class.
25  @param logger The logger.
26  @param severity The severity.
27  @param category The category.
28  @param dataType Type of the data.
29  @param location The file location the log message is coming from.
30  */
31  Capture(const Logger& logger, logging::Severity severity, const char* category,
33  : logger_{&logger}, severity_{severity}, category_{category}, data_type_{dataType}, location_{location} {
34  }
35 
36  /**
37  The stream that can capture the message via operator<<.
38  @returns Output stream.
39  */
40  std::ostream& Stream() noexcept {
41  return stream_;
42  }
43 
44 #ifdef _MSC_VER
45 // add SAL annotation for printf format string. requires Code Analysis to run to validate usage.
46 #define msvc_printf_check _Printf_format_string_
47 #define __attribute__(x) // Disable for MSVC. Supported by GCC and CLang.
48 #else
49 #define msvc_printf_check
50 #endif
51 
52  /**
53  Captures a printf style log message.
54  @param name="format">The printf format.
55  @param name="">Arguments to the printf format if needed.
56  @remarks
57  A maximum of 2K of output will be captured currently.
58  Non-static method, so 'this' is implicit first arg, and we use format(printf(2,3)
59  */
60  void CapturePrintf(msvc_printf_check const char* format, ...) __attribute__((format(printf, 2, 3)));
61 
62  /**
63  Process a printf style log message.
64  @param format The printf format.
65  @param ... Arguments to the printf format if needed.
66  @remarks
67  A maximum of 2K of output will be captured currently.
68  Note: As va_list is 'char *', we have to disambiguate this from CapturePrintf
69  so that something like "One string: %s", "the string" does not consider "the string"
70  to be the va_list.
71  */
72  void ProcessPrintf(msvc_printf_check const char* format, va_list args);
73 
74  logging::Severity Severity() const noexcept {
75  return severity_;
76  }
77 
78  char SeverityPrefix() const noexcept {
79  // Carefully setup so severity_ is a valid index
80  GSL_SUPPRESS(bounds .2) {
81  return logging::SEVERITY_PREFIX[static_cast<int>(severity_)];
82  }
83  }
84 
85  const char* Category() const noexcept {
86  return category_;
87  }
88 
90  return data_type_;
91  }
92 
93  const CodeLocation& Location() const noexcept {
94  return location_;
95  }
96 
97  std::string Message() const noexcept {
98  return stream_.str();
99  }
100 
101  ~Capture();
102 
103  private:
104  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(Capture);
105 
106  const Logger* logger_;
107  const logging::Severity severity_;
108  const char* category_;
109  const logging::DataType data_type_;
110  const CodeLocation location_;
111 
112  std::ostringstream stream_;
113 };
114 } // namespace logging
115 } // namespace onnxruntime
std::string Message() const noexcept
Definition: capture.h:97
char SeverityPrefix() const noexcept
Definition: capture.h:78
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
constexpr const char * SEVERITY_PREFIX
Definition: severity.h:19
__attribute__((visibility("default")))
logging::DataType DataType() const noexcept
Definition: capture.h:89
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
GLint location
Definition: glcorearb.h:805
GLenum GLenum severity
Definition: glcorearb.h:2539
std::ostream & Stream() noexcept
Definition: capture.h:40
void CapturePrintf(msvc_printf_check const char *format,...) __attribute__((format(printf
#define msvc_printf_check
Definition: capture.h:49
**If you just want to fire and args
Definition: thread.h:609
#define const
Definition: zconf.h:214
void void ProcessPrintf(msvc_printf_check const char *format, va_list args)
const CodeLocation & Location() const noexcept
Definition: capture.h:93
HUSD_API const char * dataType()
const char * Category() const noexcept
Definition: capture.h:85
Capture(const Logger &logger, logging::Severity severity, const char *category, logging::DataType dataType, const CodeLocation &location)
Definition: capture.h:31