PoC_Team / Mbed OS ADMW1001v0510002
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers admw_log.cpp Source File

admw_log.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright 2017 (c) Analog Devices, Inc.
00003 
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are met:
00008   - Redistributions of source code must retain the above copyright
00009     notice, this list of conditions and the following disclaimer.
00010   - Redistributions in binary form must reproduce the above copyright
00011     notice, this list of conditions and the following disclaimer in
00012     the documentation and/or other materials provided with the
00013     distribution.
00014   - Neither the name of Analog Devices, Inc. nor the names of its
00015     contributors may be used to endorse or promote products derived
00016     from this software without specific prior written permission.
00017   - The use of this software may or may not infringe the patent rights
00018     of one or more patent holders. This license does not release you
00019     from the requirement that you obtain separate licenses from these
00020     patent holders to use this software.
00021   - Use of the software either in source or binary form, must be run
00022     on or directly connected to an Analog Devices Inc. component.
00023 
00024 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00025 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00026 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00027 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00030 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *****************************************************************************/
00036 
00037 /*!
00038  ******************************************************************************
00039  * @file:
00040  * @brief:  ADMW OS-dependent wrapper layer for log functions
00041  *-----------------------------------------------------------------------------
00042  */
00043 
00044 #include <mbed.h>
00045 #include "inc/admw_log.h"
00046 
00047 static Serial *gpUartDevice;
00048 
00049 static ADMW_LOG_LEVEL  gLogLevel = ADMW_LOG_LEVEL_DEBUG ;
00050 
00051 #ifdef __cplusplus
00052 extern "C" {
00053 #endif
00054 
00055 /*
00056  * Open the Log interface and allocate resources
00057  */
00058 ADMW_RESULT  admw_LogOpen(
00059     ADMW_PLATFORM_LOG_CONFIG *pConfig)
00060 {
00061     if (pConfig->disableLogs)
00062         return ADMW_SUCCESS ;
00063 
00064     gpUartDevice = new Serial((PinName)pConfig->txPin,
00065                               (PinName)pConfig->rxPin,
00066                                pConfig->baudRate);
00067     if (!gpUartDevice)
00068     {
00069         ADMW_LOG_ERROR("Failed to allocate memory for Log UART context");
00070         return ADMW_NO_MEM ;
00071     }
00072 
00073     return ADMW_SUCCESS ;
00074 }
00075 
00076 /*
00077  * Close the Log interface and free resources
00078  */
00079 void admw_LogClose(void)
00080 {
00081     if (gpUartDevice)
00082     {
00083         delete gpUartDevice;
00084         gpUartDevice = 0;
00085     }
00086 }
00087 
00088 /*
00089  * Set the log level for uart communication
00090  */
00091 void admw_LogLevel(ADMW_LOG_LEVEL  maxLevel)
00092 {
00093     gLogLevel = maxLevel;
00094 }
00095 
00096 /*
00097  * Print a log message to the platform log interface
00098  */
00099 void admw_Log(ADMW_LOG_LEVEL  level, const char* format, ...)
00100 {
00101     char buffer[256];
00102     va_list va_args;
00103 
00104     if (!gpUartDevice)
00105         return;
00106 
00107     if (level > gLogLevel)
00108         return;
00109 
00110     va_start(va_args, format);
00111     vsnprintf(buffer, sizeof(buffer), format, va_args);
00112     va_end(va_args);
00113 
00114     gpUartDevice->printf("%s\r\n", buffer);
00115 }
00116 
00117 #ifdef __cplusplus
00118 }
00119 #endif