1 // Copyright 2024 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 <cstring> // strerror() 17 18 #include "pw_log/log.h" 19 20 // TODO(b/328262654): Move this to a more appropriate module. 21 22 // A format string for logging an errno plus its string representation. 23 // Use with the provided ERRNO_FORMAT_ARGS function in logging functions. 24 // This can be used directly with PW_LOG_*, PW_CHECK_*, and PW_CRASH. 25 // 26 // For example: 27 // 28 // PW_CHECK_INT_EQ(size, expected, "Error reading foo: " ERRNO_FORMAT_STRING, 29 // ERRNO_FORMAT_ARGS(errno)); 30 // 31 // Prefer to use one of the helper macros below for common use cases. 32 // 33 #define ERRNO_FORMAT_STRING "errno=%d (%s)" 34 #define ERRNO_FORMAT_ARGS(e) e, std::strerror(e) 35 36 // Macros used to log errno plus its string representation. This ensures 37 // consistent formatting and always remembering to include the string. 38 // 39 // Note that we accept the extra memory cost of logging the string directly 40 // instead of attempting to tokenize it. These logs should only be used in 41 // Linux platform-specific code and generally only for logging errors. 42 // This could be updated to use nested tokenization: 43 // https://pigweed.dev/seed/0105-pw_tokenizer-pw_log-nested-tokens.html 44 // 45 // Example: 46 // 47 // LOG_ERROR_WITH_ERRNO("Error calling foo:", errno); 48 // 49 #define LOG_DEBUG_WITH_ERRNO(message, e, ...) \ 50 PW_LOG_DEBUG( \ 51 message " " ERRNO_FORMAT_STRING, ##__VA_ARGS__, ERRNO_FORMAT_ARGS(e)); 52 53 #define LOG_INFO_WITH_ERRNO(message, e, ...) \ 54 PW_LOG_INFO( \ 55 message " " ERRNO_FORMAT_STRING, ##__VA_ARGS__, ERRNO_FORMAT_ARGS(e)); 56 57 #define LOG_WARN_WITH_ERRNO(message, e, ...) \ 58 PW_LOG_WARN( \ 59 message " " ERRNO_FORMAT_STRING, ##__VA_ARGS__, ERRNO_FORMAT_ARGS(e)); 60 61 #define LOG_ERROR_WITH_ERRNO(message, e, ...) \ 62 PW_LOG_ERROR( \ 63 message " " ERRNO_FORMAT_STRING, ##__VA_ARGS__, ERRNO_FORMAT_ARGS(e)); 64 65 #define LOG_CRITICAL_WITH_ERRNO(message, e, ...) \ 66 PW_LOG_CRITICAL( \ 67 message " " ERRNO_FORMAT_STRING, ##__VA_ARGS__, ERRNO_FORMAT_ARGS(e)); 68