1 // Copyright 2021 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 <zephyr/logging/log.h> 17 #include <zephyr/logging/log_ctrl.h> 18 19 #include "pw_log_zephyr/config.h" 20 21 // If the consumer defined PW_LOG_LEVEL use it, otherwise fallback to the global 22 // CONFIG_PIGWEED_LOG_LEVEL set by Kconfig. 23 #ifdef PW_LOG_LEVEL 24 #if PW_LOG_LEVEL == PW_LOG_LEVEL_DEBUG 25 // Map PW_LOG_LEVEL_DEBUG to LOG_LEVEL_DBG 26 #define LOG_LEVEL LOG_LEVEL_DBG 27 #elif PW_LOG_LEVEL == PW_LOG_LEVEL_INFO 28 // Map PW_LOG_LEVEL_INFO to LOG_LEVEL_INF 29 #define LOG_LEVEL LOG_LEVEL_INF 30 #elif PW_LOG_LEVEL == PW_LOG_LEVEL_WARN 31 // Map PW_LOG_LEVEL_WARN to LOG_LEVEL_WRN 32 #define LOG_LEVEL LOG_LEVEL_WRN 33 #elif (PW_LOG_LEVEL == PW_LOG_LEVEL_ERROR) || \ 34 (PW_LOG_LEVEL == PW_LOG_LEVEL_CRITICAL) || \ 35 (PW_LOG_LEVEL == PW_LOG_LEVEL_FATAL) 36 // Map PW_LOG_LEVEL_(ERROR|CRITICAL|FATAL) to LOG_LEVEL_ERR 37 #define LOG_LEVEL LOG_LEVEL_ERR 38 #endif 39 #else 40 // Default to the Kconfig value 41 #define LOG_LEVEL CONFIG_PIGWEED_LOG_LEVEL 42 #endif 43 44 LOG_MODULE_DECLARE(PW_LOG_ZEPHYR_MODULE_NAME, LOG_LEVEL); 45 46 #define PW_HANDLE_LOG(level, module, flags, ...) \ 47 do { \ 48 switch (level) { \ 49 case PW_LOG_LEVEL_INFO: \ 50 LOG_INF(module " " __VA_ARGS__); \ 51 break; \ 52 case PW_LOG_LEVEL_WARN: \ 53 LOG_WRN(module " " __VA_ARGS__); \ 54 break; \ 55 case PW_LOG_LEVEL_ERROR: \ 56 case PW_LOG_LEVEL_CRITICAL: \ 57 LOG_ERR(module " " __VA_ARGS__); \ 58 break; \ 59 case PW_LOG_LEVEL_FATAL: \ 60 LOG_ERR(module " " __VA_ARGS__); \ 61 LOG_PANIC(); \ 62 break; \ 63 case PW_LOG_LEVEL_DEBUG: \ 64 default: \ 65 LOG_DBG(module " " __VA_ARGS__); \ 66 break; \ 67 } \ 68 } while (0) 69