1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 #pragma once
7 
8 #include <aws/core/Core_EXPORTS.h>
9 
10 #include <aws/core/utils/logging/LogLevel.h>
11 #include <aws/core/utils/logging/AWSLogging.h>
12 #include <aws/core/utils/logging/LogSystemInterface.h>
13 #include <aws/core/utils/memory/stl/AWSStringStream.h>
14 
15 // While macros are usually grotty, using them here lets us have a simple function call interface for logging that
16 //
17 //  (1) Can be compiled out completely, so you don't even have to pay the cost to check the log level (which will be a virtual function call and a std::atomic<> read) if you don't want any AWS logging
18 //  (2) If you use logging and the log statement doesn't pass the conditional log filter level, not only do you not pay the cost of building the log string, you don't pay the cost for allocating or
19 //      getting any of the values used in building the log string, as they're in a scope (if-statement) that never gets entered.
20 
21 #ifdef DISABLE_AWS_LOGGING
22 
23     #define AWS_LOG(level, tag, ...)
24     #define AWS_LOG_FATAL(tag, ...)
25     #define AWS_LOG_ERROR(tag, ...)
26     #define AWS_LOG_WARN(tag, ...)
27     #define AWS_LOG_INFO(tag, ...)
28     #define AWS_LOG_DEBUG(tag, ...)
29     #define AWS_LOG_TRACE(tag, ...)
30     #define AWS_LOG_FLUSH()
31 
32     #define AWS_LOGSTREAM(level, tag, streamExpression)
33     #define AWS_LOGSTREAM_FATAL(tag, streamExpression)
34     #define AWS_LOGSTREAM_ERROR(tag, streamExpression)
35     #define AWS_LOGSTREAM_WARN(tag, streamExpression)
36     #define AWS_LOGSTREAM_INFO(tag, streamExpression)
37     #define AWS_LOGSTREAM_DEBUG(tag, streamExpression)
38     #define AWS_LOGSTREAM_TRACE(tag, streamExpression)
39     #define AWS_LOGSTREAM_FLUSH()
40 
41 #else
42 
43     #define AWS_LOG_FLUSH() \
44         { \
45             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
46             if ( logSystem ) \
47             { \
48                 logSystem->Flush(); \
49             } \
50         }
51 
52     #define AWS_LOG(level, tag, ...) \
53         { \
54             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
55             if ( logSystem && logSystem->GetLogLevel() >= level ) \
56             { \
57                 logSystem->Log(level, tag, __VA_ARGS__); \
58             } \
59         }
60 
61     #define AWS_LOG_FATAL(tag, ...) \
62         { \
63             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
64             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Fatal ) \
65             { \
66                 logSystem->Log(Aws::Utils::Logging::LogLevel::Fatal, tag, __VA_ARGS__); \
67             } \
68         }
69 
70     #define AWS_LOG_ERROR(tag, ...) \
71         { \
72             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
73             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Error ) \
74             { \
75                 logSystem->Log(Aws::Utils::Logging::LogLevel::Error, tag, __VA_ARGS__); \
76             } \
77         }
78 
79     #define AWS_LOG_WARN(tag, ...) \
80         { \
81             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
82             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Warn ) \
83             { \
84                 logSystem->Log(Aws::Utils::Logging::LogLevel::Warn, tag, __VA_ARGS__); \
85             } \
86         }
87 
88     #define AWS_LOG_INFO(tag, ...) \
89         { \
90             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
91             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Info ) \
92             { \
93                 logSystem->Log(Aws::Utils::Logging::LogLevel::Info, tag, __VA_ARGS__); \
94             } \
95         }
96 
97     #define AWS_LOG_DEBUG(tag, ...) \
98         { \
99             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
100             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Debug ) \
101             { \
102                 logSystem->Log(Aws::Utils::Logging::LogLevel::Debug, tag, __VA_ARGS__); \
103             } \
104         }
105 
106     #define AWS_LOG_TRACE(tag, ...) \
107         { \
108             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
109             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Trace ) \
110             { \
111                 logSystem->Log(Aws::Utils::Logging::LogLevel::Trace, tag, __VA_ARGS__); \
112             } \
113         }
114 
115     #define AWS_LOGSTREAM(level, tag, streamExpression) \
116         { \
117             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
118             if ( logSystem && logSystem->GetLogLevel() >= level ) \
119             { \
120                 Aws::OStringStream logStream; \
121                 logStream << streamExpression; \
122                 logSystem->LogStream( logLevel, tag, logStream ); \
123             } \
124         }
125 
126     #define AWS_LOGSTREAM_FATAL(tag, streamExpression) \
127         { \
128             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
129             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Fatal ) \
130             { \
131                 Aws::OStringStream logStream; \
132                 logStream << streamExpression; \
133                 logSystem->LogStream( Aws::Utils::Logging::LogLevel::Fatal, tag, logStream ); \
134             } \
135         }
136 
137     #define AWS_LOGSTREAM_ERROR(tag, streamExpression) \
138         { \
139             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
140             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Error ) \
141             { \
142                 Aws::OStringStream logStream; \
143                 logStream << streamExpression; \
144                 logSystem->LogStream( Aws::Utils::Logging::LogLevel::Error, tag, logStream ); \
145             } \
146         }
147 
148     #define AWS_LOGSTREAM_WARN(tag, streamExpression) \
149         { \
150             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
151             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Warn ) \
152             { \
153                 Aws::OStringStream logStream; \
154                 logStream << streamExpression; \
155                 logSystem->LogStream( Aws::Utils::Logging::LogLevel::Warn, tag, logStream ); \
156             } \
157         }
158 
159     #define AWS_LOGSTREAM_INFO(tag, streamExpression) \
160         { \
161             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
162             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Info ) \
163             { \
164                 Aws::OStringStream logStream; \
165                 logStream << streamExpression; \
166                 logSystem->LogStream( Aws::Utils::Logging::LogLevel::Info, tag, logStream ); \
167             } \
168         }
169 
170     #define AWS_LOGSTREAM_DEBUG(tag, streamExpression) \
171         { \
172             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
173             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Debug ) \
174             { \
175                 Aws::OStringStream logStream; \
176                 logStream << streamExpression; \
177                 logSystem->LogStream( Aws::Utils::Logging::LogLevel::Debug, tag, logStream ); \
178             } \
179         }
180 
181     #define AWS_LOGSTREAM_TRACE(tag, streamExpression) \
182         { \
183             Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
184             if ( logSystem && logSystem->GetLogLevel() >= Aws::Utils::Logging::LogLevel::Trace ) \
185             { \
186                 Aws::OStringStream logStream; \
187                 logStream << streamExpression; \
188                 logSystem->LogStream( Aws::Utils::Logging::LogLevel::Trace, tag, logStream ); \
189             } \
190         }
191 
192     #define AWS_LOGSTREAM_FLUSH()  AWS_LOG_FLUSH()
193 
194 #endif // DISABLE_AWS_LOGGING
195