Imre Kis | e8808a9 | 2020-01-28 16:23:02 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <stdarg.h> |
| 8 | #include <assert.h> |
| 9 | #include <stdio.h> |
| 10 | #include <setjmp.h> |
| 11 | extern "C" { |
| 12 | #include <common/debug.h> |
| 13 | } |
| 14 | |
| 15 | #include "CppUTestExt/MockSupport.h" |
| 16 | #include "common/mock_debug.h" |
| 17 | |
| 18 | /* Set the default maximum log level to the `LOG_LEVEL` build flag */ |
| 19 | static unsigned int max_log_level = LOG_LEVEL_VERBOSE; |
| 20 | |
| 21 | int expect_panic(panic_environment_t *env) { |
| 22 | mock("debug").expectOneCall("do_panic").andReturnValue(env); |
| 23 | return 1; |
| 24 | } |
| 25 | |
| 26 | void expect_tf_log(const char *str) { |
| 27 | mock().expectOneCall("tf_log").withStringParameter("str", str); |
| 28 | } |
| 29 | |
| 30 | extern "C" { |
| 31 | void do_panic(void) { |
| 32 | panic_environment_t *env = (panic_environment_t *)mock("debug").actualCall("do_panic").returnPointerValue(); |
| 33 | longjmp(*env, 1); |
| 34 | } |
| 35 | |
| 36 | void tf_log(const char *fmt, ...) |
| 37 | { |
| 38 | unsigned int log_level; |
| 39 | va_list args; |
| 40 | const char *prefix_str; |
| 41 | |
| 42 | /* We expect the LOG_MARKER_* macro as the first character */ |
| 43 | log_level = fmt[0]; |
| 44 | |
| 45 | /* Verify that log_level is one of LOG_MARKER_* macro defined in debug.h */ |
| 46 | assert((log_level > 0U) && (log_level <= LOG_LEVEL_VERBOSE)); |
| 47 | assert((log_level % 10U) == 0U); |
| 48 | |
| 49 | if (log_level > max_log_level) |
| 50 | return; |
| 51 | |
| 52 | char log_buffer[1024]; |
| 53 | va_start(args, fmt); |
| 54 | (void)vsnprintf(log_buffer, sizeof(log_buffer), fmt + 1, args); |
| 55 | va_end(args); |
| 56 | |
| 57 | mock().actualCall("tf_log").withStringParameter("str", log_buffer); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * The helper function to set the log level dynamically by platform. The |
| 62 | * maximum log level is determined by `LOG_LEVEL` build flag at compile time |
| 63 | * and this helper can set a lower (or equal) log level than the one at compile. |
| 64 | */ |
| 65 | void tf_log_set_max_level(unsigned int log_level) |
| 66 | { |
| 67 | assert(log_level <= LOG_LEVEL_VERBOSE); |
| 68 | assert((log_level % 10U) == 0U); |
| 69 | |
| 70 | /* Cap log_level to the compile time maximum. */ |
| 71 | if (log_level <= (unsigned int)LOG_LEVEL_VERBOSE) |
| 72 | max_log_level = log_level; |
| 73 | } |
| 74 | } // extern "C" |
| 75 | |