leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed-trace-helper.c Source File

mbed-trace-helper.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //  
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //  
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //  
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 
00017 #include "pv_log.h"
00018 #include <stdarg.h>
00019 #include <inttypes.h>
00020 #include <stdlib.h>
00021 #include "pal.h"
00022 #include "pv_error_handling.h"
00023 #include "mbed-trace/mbed_trace.h"
00024 
00025 /**
00026 * Mutex for printing logs in a thread safe manner.
00027 */
00028 palMutexID_t g_pv_logger_mutex = NULLPTR;
00029 
00030 void mbed_trace_helper_print(const char* format)
00031 {
00032     printf("%s\n", format);
00033 }
00034 
00035 void mbed_trace_helper_mutex_wait()
00036 {
00037     (void)pal_osMutexWait(g_pv_logger_mutex, PAL_RTOS_WAIT_FOREVER);
00038 }
00039 
00040 void mbed_trace_helper_mutex_release()
00041 {
00042     (void)pal_osMutexRelease(g_pv_logger_mutex);
00043 }
00044 /**
00045 * Creates mutex
00046 */
00047 bool mbed_trace_helper_create_mutex(void)
00048 {
00049     palStatus_t status;
00050 
00051     // g_pv_logger_mutex already created - no need to recreate it.
00052     if (g_pv_logger_mutex) {
00053         goto exit;
00054     }
00055 
00056     status = pal_osMutexCreate(&g_pv_logger_mutex);
00057     if (status != PAL_SUCCESS) {
00058         SA_PV_LOG_INFO("Error creating g_pv_logger_mutex (pal err = %d)", (int)status);
00059         return false;
00060     }
00061 
00062 exit:
00063     return true;
00064 }
00065 
00066 /**
00067 * Deletes mutex
00068 */
00069 void mbed_trace_helper_delete_mutex(void)
00070 {
00071     // g_pv_logger_mutex already created - no need to recreate it.
00072     if (g_pv_logger_mutex == NULLPTR) {
00073         return;
00074     }
00075 
00076     pal_osMutexDelete(&g_pv_logger_mutex);
00077     g_pv_logger_mutex = NULLPTR;
00078 }
00079 
00080 uint8_t mbed_trace_helper_check_activated_trace_level()
00081 {
00082     uint8_t config_active_level = 0;
00083     uint8_t activated_level = 0;
00084 
00085     SA_PV_LOG_INFO_FUNC_ENTER("MBED_TRACE_MAX_LEVEL = %d", MBED_TRACE_MAX_LEVEL);
00086 
00087     config_active_level = mbed_trace_config_get() & TRACE_MASK_LEVEL;
00088     SA_PV_LOG_INFO("config_active_level is %d", config_active_level);
00089     
00090     activated_level = config_active_level & MBED_TRACE_MAX_LEVEL;
00091     SA_PV_LOG_INFO("activated_level is %d", activated_level);
00092 
00093     if (activated_level == 0) {
00094         SA_PV_LOG_CRITICAL("The compiled maximum trace level %d, is higher than activated trace level", MBED_TRACE_MAX_LEVEL);
00095         SA_PV_LOG_CRITICAL("If you want to use the requested log level, please change MBED_TRACE_MAX_LEVEL compilation flag and recompile the code");
00096     }
00097 
00098     SA_PV_LOG_INFO_FUNC_EXIT_NO_ARGS();
00099 
00100     return activated_level;
00101 }
00102 
00103 bool mbed_trace_helper_init(uint8_t config, bool is_mutex_used)
00104 {
00105     bool success = true;
00106     int rc = 0;
00107 
00108     rc = mbed_trace_init();
00109 
00110     if (rc != 0) {
00111         return false;
00112     }
00113 
00114     if (is_mutex_used) {
00115         // Create mutex
00116         success = mbed_trace_helper_create_mutex();
00117         if (success != true) {
00118             mbed_trace_free();
00119             return false;
00120         }
00121     }
00122     // Set trace level, TRACE_MODE_PLAIN used to ignore mbed trace print pattern ([trace_level] [trace_group] format)
00123     mbed_trace_config_set(config);
00124 
00125     // Set trace print function
00126     mbed_trace_print_function_set(mbed_trace_helper_print);
00127 
00128     if (is_mutex_used) {
00129         // Set mutex wait function for mbed trace
00130         mbed_trace_mutex_wait_function_set(mbed_trace_helper_mutex_wait);
00131         // Set mutex release function for mbed trace
00132         mbed_trace_mutex_release_function_set(mbed_trace_helper_mutex_release);
00133     }
00134     return  true;
00135 }
00136 
00137 void mbed_trace_helper_finish()
00138 {
00139     mbed_trace_helper_delete_mutex();
00140     mbed_trace_free();
00141 }
00142 
00143 
00144