Changes to enabled on-line compiler

Committer:
JMF
Date:
Wed May 30 20:59:51 2018 +0000
Revision:
0:082731ede69f
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:082731ede69f 1 /**
JMF 0:082731ede69f 2 * copyright (c) 2017-2018, James Flynn
JMF 0:082731ede69f 3 * SPDX-License-Identifier: Apache-2.0
JMF 0:082731ede69f 4 */
JMF 0:082731ede69f 5
JMF 0:082731ede69f 6 /*
JMF 0:082731ede69f 7 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:082731ede69f 8 * you may not use this file except in compliance with the License.
JMF 0:082731ede69f 9 * You may obtain a copy of the License at
JMF 0:082731ede69f 10 *
JMF 0:082731ede69f 11 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:082731ede69f 12 *
JMF 0:082731ede69f 13 * Unless required by applicable law or agreed to in writing, software
JMF 0:082731ede69f 14 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:082731ede69f 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:082731ede69f 16 *
JMF 0:082731ede69f 17 * See the License for the specific language governing permissions and
JMF 0:082731ede69f 18 * limitations under the License.
JMF 0:082731ede69f 19 */
JMF 0:082731ede69f 20
JMF 0:082731ede69f 21 /**
JMF 0:082731ede69f 22 * @file WNCDebug.h
JMF 0:082731ede69f 23 * @brief A debug class that coordinates the output of debug messages to
JMF 0:082731ede69f 24 * either stdio or a serial port based on instantiation.
JMF 0:082731ede69f 25 *
JMF 0:082731ede69f 26 * @author James Flynn
JMF 0:082731ede69f 27 *
JMF 0:082731ede69f 28 * @date 1-Feb-2018
JMF 0:082731ede69f 29 *
JMF 0:082731ede69f 30 */
JMF 0:082731ede69f 31
JMF 0:082731ede69f 32 #ifndef __WNCDEBUG__
JMF 0:082731ede69f 33 #define __WNCDEBUG__
JMF 0:082731ede69f 34 #include <stdio.h>
JMF 0:082731ede69f 35 #include "WNCIO.h"
JMF 0:082731ede69f 36
JMF 0:082731ede69f 37 /** WNCDebug class
JMF 0:082731ede69f 38 * Used to write debug data to the user designated IO. Currently
JMF 0:082731ede69f 39 * The class expects either a stdio element (defaults to stderr) or
JMF 0:082731ede69f 40 * a pointer to a WncIO object.
JMF 0:082731ede69f 41 */
JMF 0:082731ede69f 42
JMF 0:082731ede69f 43 class WNCDebug
JMF 0:082731ede69f 44 {
JMF 0:082731ede69f 45 public:
JMF 0:082731ede69f 46 //! Create class with either stdio or a pointer to a uart
JMF 0:082731ede69f 47 WNCDebug( FILE * fd = stderr ): m_puart(NULL) {m_stdiofp=fd;};
JMF 0:082731ede69f 48 WNCDebug( WncIO * uart): m_stdiofp(NULL) {m_puart=uart;};
JMF 0:082731ede69f 49 ~WNCDebug() {};
JMF 0:082731ede69f 50
JMF 0:082731ede69f 51 //! standard printf() functionallity
JMF 0:082731ede69f 52 int printf( char * fmt, ...) {
JMF 0:082731ede69f 53 char buffer[256];
JMF 0:082731ede69f 54 int ret=0;
JMF 0:082731ede69f 55 va_list args;
JMF 0:082731ede69f 56 va_start (args, fmt);
JMF 0:082731ede69f 57 vsnprintf(buffer, sizeof(buffer), fmt, args);
JMF 0:082731ede69f 58 prt.lock();
JMF 0:082731ede69f 59 if( m_stdiofp )
JMF 0:082731ede69f 60 ret=fputs(buffer,m_stdiofp);
JMF 0:082731ede69f 61 else
JMF 0:082731ede69f 62 ret=m_puart->puts(buffer);
JMF 0:082731ede69f 63 prt.unlock();
JMF 0:082731ede69f 64 va_end (args);
JMF 0:082731ede69f 65 return ret;
JMF 0:082731ede69f 66 }
JMF 0:082731ede69f 67
JMF 0:082731ede69f 68 //! standard putc() functionallity
JMF 0:082731ede69f 69 int putc( int c ) {
JMF 0:082731ede69f 70 int ret=0;
JMF 0:082731ede69f 71 prt.lock();
JMF 0:082731ede69f 72 if( m_stdiofp )
JMF 0:082731ede69f 73 ret=fputc(c, m_stdiofp);
JMF 0:082731ede69f 74 else
JMF 0:082731ede69f 75 ret=m_puart->putc(c);
JMF 0:082731ede69f 76 prt.unlock();
JMF 0:082731ede69f 77 return ret;
JMF 0:082731ede69f 78 }
JMF 0:082731ede69f 79
JMF 0:082731ede69f 80 //! standard puts() functionallity
JMF 0:082731ede69f 81 int puts( const char * str ) {
JMF 0:082731ede69f 82 int ret=0;
JMF 0:082731ede69f 83 prt.lock();
JMF 0:082731ede69f 84 if( m_stdiofp )
JMF 0:082731ede69f 85 ret=fputs(str,m_stdiofp);
JMF 0:082731ede69f 86 else
JMF 0:082731ede69f 87 ret=m_puart->puts(str);
JMF 0:082731ede69f 88 prt.unlock();
JMF 0:082731ede69f 89 return ret;
JMF 0:082731ede69f 90 }
JMF 0:082731ede69f 91
JMF 0:082731ede69f 92 private:
JMF 0:082731ede69f 93 std::FILE *m_stdiofp;
JMF 0:082731ede69f 94 WncIO *m_puart;
JMF 0:082731ede69f 95 Mutex prt;
JMF 0:082731ede69f 96 };
JMF 0:082731ede69f 97
JMF 0:082731ede69f 98 #endif
JMF 0:082731ede69f 99