1 // Copyright 2020 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 <stdarg.h> 17 18 #include "pw_preprocessor/arguments.h" 19 #include "pw_preprocessor/compiler.h" 20 #include "pw_preprocessor/util.h" 21 22 PW_EXTERN_C_START 23 24 // Log a message with the listed attributes. 25 void pw_Log(int level, 26 unsigned int flags, 27 const char* module_name, 28 const char* file_name, 29 int line_number, 30 const char* function_name, 31 const char* message, 32 ...) PW_PRINTF_FORMAT(7, 8); 33 34 void pw_Log_HandleMessageVaList(int level, 35 unsigned int flags, 36 const char* module_name, 37 const char* file_name, 38 int line_number, 39 const char* function_name, 40 const char* message, 41 va_list args); 42 43 PW_EXTERN_C_END 44 45 // Log a message with many attributes included. 46 // 47 // This is the log macro frontend that funnels everything into the C handler 48 // above, pw_Log(). It's not efficient at the callsite, since it passes many 49 // arguments. Additionally, the use of the __FUNC__ macro adds a static const 50 // char[] variable inside functions with a log. 51 // 52 // TODO: b/235289435 - Reconsider the naming of this module when more is in 53 // place. 54 #define PW_HANDLE_LOG(level, module, flags, message, ...) \ 55 do { \ 56 pw_Log((level), \ 57 (flags), \ 58 module, \ 59 __FILE__, \ 60 __LINE__, \ 61 __func__, \ 62 message PW_COMMA_ARGS(__VA_ARGS__)); \ 63 } while (0) 64 65 #ifdef __cplusplus 66 67 #include <string_view> 68 69 namespace pw::log_basic { 70 71 // Sets the function to use to send log messages. Defaults to 72 // pw::sys_io::WriteLine. 73 void SetOutput(void (*log_output)(std::string_view log)); 74 75 } // namespace pw::log_basic 76 77 #endif // __cplusplus 78