Wiselib
wiselib.stable/external_interface/lorien/lorien_debug.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef CONNECTOR_LORIEN_DEBUGOUTPUT_H
00020 #define CONNECTOR_LORIEN_DEBUGOUTPUT_H
00021 
00022 #include <stdarg.h>
00023 #include <stdio.h>
00024 
00025 extern "C" {
00026 #include "lorien.h"
00027 }
00028 
00029 namespace wiselib
00030 {
00031 
00039    template<typename OsModel_P>
00040    class LorienDebug
00041    {
00042       public:
00043          typedef OsModel_P OsModel;
00044 
00045          typedef LorienDebug<OsModel> self_type;
00046          typedef self_type* self_pointer_t;
00047          // -----------------------------------------------------------------
00048          void init( Component *comp )
00049          {
00050             comp_ = comp;
00051          }
00052          
00053          
00060          char* op_ltoa(long value, char* result, int base)
00061             {
00062             // check that the base if valid
00063             if (base < 2 || base > 36)
00064                {
00065                *result = '\0';
00066                return result;
00067                }
00068 
00069             char* ptr = result, *ptr1 = result, tmp_char;
00070             long tmp_value;
00071 
00072             do
00073                {
00074                tmp_value = value;
00075                value /= base;
00076                *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
00077                } while ( value );
00078 
00079             // Apply negative sign
00080             if (tmp_value < 0) *ptr++ = '-';
00081             *ptr-- = '\0';
00082             while(ptr1 < ptr)
00083                {
00084                tmp_char = *ptr;
00085                *ptr--= *ptr1;
00086                *ptr1++ = tmp_char;
00087                }
00088             return result;
00089             }
00090 
00091          char* op_ultoa(unsigned long value, char* result, int base)
00092             {
00093             // check that the base if valid
00094             if (base < 2 || base > 36)
00095                {
00096                *result = '\0';
00097                return result;
00098                }
00099 
00100             char* ptr = result, *ptr1 = result, tmp_char;
00101             unsigned long tmp_value;
00102 
00103             do
00104                {
00105                tmp_value = value;
00106                value /= base;
00107                *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
00108                } while ( value );
00109 
00110             // Apply negative sign
00111             *ptr-- = '\0';
00112             while(ptr1 < ptr)
00113                {
00114                tmp_char = *ptr;
00115                *ptr--= *ptr1;
00116                *ptr1++ = tmp_char;
00117                }
00118             return result;
00119             }
00120          
00121          #define BUFSIZE 1024
00122          // -----------------------------------------------------------------
00123          void debug( const char *format, ... )
00124          {
00125          //below code taken from Lorien's stdout library component
00126          //TODO: check we don't exceed BUFSIZE in the below procedure
00127          char finalLine[BUFSIZE];
00128          va_list vars;
00129          memset(finalLine, '\0', BUFSIZE);
00130    
00131          va_start(vars, format);
00132          //vsprintf(finalLine, format, vars); //this is generally too expensive to have in fundamentals
00133          char *fm;
00134          while ((fm = strchr(format, '%')) != NULL)
00135             {
00136             strncpy(&finalLine[strlen(finalLine)], format, fm - format);
00137       
00138             if (fm[1] == 'u')
00139                {
00140                unsigned int ui = va_arg(vars, unsigned int);
00141                op_ultoa(ui, &finalLine[strlen(finalLine)], 10);
00142                }
00143                else if ((fm[1] == 'i') || (fm[1] == 'd'))
00144                {
00145                int i = va_arg(vars, int);
00146                op_ltoa(i, &finalLine[strlen(finalLine)], 10);
00147                }
00148                else if (fm[1] == 'l')
00149                {
00150                if (fm[2] == 'u')
00151                   {
00152                   unsigned long ul = va_arg(vars, unsigned long);
00153                   op_ultoa(ul, &finalLine[strlen(finalLine)], 10);
00154                   fm ++;
00155                   }
00156                   else
00157                   {
00158                   long ul = va_arg(vars, long);
00159                   op_ltoa(ul, &finalLine[strlen(finalLine)], 10);
00160                   }
00161                }
00162                else if (fm[1] == 'p')
00163                {
00164                unsigned int u = va_arg(vars, unsigned int);
00165                finalLine[strlen(finalLine)] = '0';
00166                finalLine[strlen(finalLine)] = 'x';
00167                op_ultoa(u, &finalLine[strlen(finalLine)], 16);
00168                }
00169                else if (fm[1] == 's')
00170                {
00171                char * s = va_arg(vars, char *);
00172                strcpy(&finalLine[strlen(finalLine)], s);
00173                }
00174                else
00175                {
00176                if (fm[1] != '%')
00177                   va_arg(vars, void*);
00178                finalLine[strlen(finalLine)] = fm[1];
00179                }
00180       
00181             format = fm + 2;
00182             }
00183          va_end(vars);
00184    
00185          strcpy(&finalLine[strlen(finalLine)], format);
00186    
00187          ((IPort*) ((LXState*) comp_ -> state) -> port -> userRecp) -> send(((LXState*) comp_ -> state) -> port -> ifHostComponent, (unsigned char*) finalLine, strlen(finalLine));
00188          
00189             /*
00190             va_list fmtargs;
00191             char buffer[1024];
00192             va_start( fmtargs, msg );
00193             vsnprintf( buffer, sizeof(buffer) - 1, msg, fmtargs );
00194             va_end( fmtargs );
00195             printf( "%s", buffer );
00196             */
00197          }
00198 
00199       private:
00200          Component *comp_;
00201    };
00202 }
00203 
00204 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines