1 // Copyright 2022 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_assert/check.h" 17 #include "pw_log/glog_adapter_config.h" 18 #include "pw_log/levels.h" 19 #include "pw_log/log.h" 20 #include "pw_string/string_builder.h" 21 22 namespace pw::log::internal { 23 24 class GlogStreamingLog { 25 public: 26 GlogStreamingLog() = default; 27 28 template <typename T> 29 GlogStreamingLog& operator<<(const T& value) { 30 string_builder_ << value; 31 return *this; 32 } 33 34 protected: 35 pw::StringBuffer<PW_LOG_CFG_GLOG_BUFFER_SIZE_BYTES> string_builder_; 36 }; 37 38 } // namespace pw::log::internal 39 40 // Declares a unique GlogStreamingLog class definition with a destructor which 41 // matches the desired pw_log_level. 42 #define _PW_LOG_GLOG_DECLARATION_PW_LOG(pw_log_level, unique) \ 43 class unique : public ::pw::log::internal::GlogStreamingLog { \ 44 public: \ 45 ~unique() { \ 46 PW_HANDLE_LOG(pw_log_level, \ 47 PW_LOG_MODULE_NAME, \ 48 PW_LOG_FLAGS, \ 49 "%s", \ 50 string_builder_.c_str()); \ 51 } \ 52 } 53 54 // Declares a unique GlogStreamingLog class definition with a destructor which 55 // invokes PW_CRASH. 56 #define _PW_LOG_GLOG_DECLARATION_PW_CRASH(unique) \ 57 class unique : public ::pw::log::internal::GlogStreamingLog { \ 58 public: \ 59 ~unique() { PW_CRASH("%s", string_builder_.c_str()); } \ 60 } 61 62 // Dispatching macros to translate the glog level to PW_LOG and PW_CRASH. 63 #define _PW_LOG_GLOG_DECLARATION_DEBUG(unique) \ 64 _PW_LOG_GLOG_DECLARATION_PW_LOG(PW_LOG_LEVEL_DEBUG, unique) 65 66 #define _PW_LOG_GLOG_DECLARATION_INFO(unique) \ 67 _PW_LOG_GLOG_DECLARATION_PW_LOG(PW_LOG_LEVEL_INFO, unique) 68 69 #define _PW_LOG_GLOG_DECLARATION_WARNING(unique) \ 70 _PW_LOG_GLOG_DECLARATION_PW_LOG(PW_LOG_LEVEL_WARN, unique) 71 72 #define _PW_LOG_GLOG_DECLARATION_ERROR(unique) \ 73 _PW_LOG_GLOG_DECLARATION_PW_LOG(PW_LOG_LEVEL_ERROR, unique) 74 75 #define _PW_LOG_GLOG_DECLARATION_FATAL(unique) \ 76 _PW_LOG_GLOG_DECLARATION_PW_CRASH(unique) 77 78 #if defined(NDEBUG) 79 #define _PW_LOG_GLOG_DECLARATION_DFATAL(unique) \ 80 _PW_LOG_GLOG_DECLARATION_PW_LOG(PW_LOG_LEVEL_ERROR, unique) 81 #else // !defined(NDEBUG) 82 #define _PW_LOG_GLOG_DECLARATION_DFATAL(unique) \ 83 _PW_LOG_GLOG_DECLARATION_PW_CRASH(unique) 84 #endif // defined(NDEBUG) 85 86 #define _PW_LOG_GLOG(glog_declaration, unique) \ 87 glog_declaration(unique); \ 88 unique() 89 90 #define _PW_LOG_GLOG_IF(glog_declaration, expr, unique) \ 91 glog_declaration(unique); \ 92 if (!(expr)) { \ 93 } else \ 94 unique() 95