YANE-Framework Tutorial 1.1.0

examples/debugmaster/src/Debugmasterhtml/debugmasterhtml.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  * Copyright (C) 2011 by www.nonlinearmpc.com                              *
00004  *                                                                         *
00005  * Authors:                                                                *
00006  *  Juergen Pannek <juergen.pannek@googlemail.com>                         *
00007  *  Michael Schamel <michael.schamel@uni-bayreuth.de>                      *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 #include "debugmasterhtml.h"
00012 
00013 #include <yane/utils.h>
00014 
00015 #include <fstream>
00016 #include <iostream>
00017 #include <sstream>
00018 #include <iomanip>
00019 
00020 using namespace std;
00021 
00022 #include "libexport.h"
00023 
00024 LIB_EXPORT DebugMasterHTML::DebugMasterHTML ( yane::Utils::RTClock * clock,
00025         std::string filename, int textwidth, bool overwrite ) :
00026         yane::Utils::DebugMasterTextStream ( clock, new ofstream (
00027                 filename.c_str ( ) ), textwidth )
00028 {
00029         stringstream header;
00030         char quotes = ( char ) 34;
00031 
00032         // Write HTML-Header
00033         header << "<!DOCTYPE html PUBLIC " << quotes << "-//W3C//DTD HTML 4.01//EN"
00034                 << quotes << " " << quotes;
00035         header << "http://www.w3.org/TR/html4/strict.dtd" << quotes << ">" << endl;
00036         header << "<html>" << endl << "<head>" << endl;
00037         header << "<meta http-equiv=" << quotes << "Content-Type" << quotes
00038                 << " content=" << quotes;
00039         header << "text/html;charset =utf-8" << quotes << ">" << endl;
00040         header << "<title>Debug-Log</title>" << endl;
00041         header << "</head>" << endl << "<body>" << endl;
00042 
00043         ( * _textstream ) << header.str ( );
00044 
00045 }
00046 
00047 LIB_EXPORT DebugMasterHTML::~DebugMasterHTML ( )
00048 {
00049         // Close HTML
00050         stringstream footer;
00051         footer << "</body>" << endl << "</html>" << endl;
00052         ( * _textstream ) << footer.str ( );
00053 
00054         // Close File
00055         ( ( ofstream* ) _textstream )->close ( );
00056         delete _textstream;
00057 }
00058 
00059 // Format the incoming data
00060 LIB_EXPORT void DebugMasterHTML::addDebugInfo ( std::string clientname,
00061         std::string message, std::string dataname, void * data, int datasize,
00062         int datatype, int level )
00063 {
00064 
00065         _mutex->lock ( );
00066         if ( _textstream == 0 )
00067         {
00068                 _mutex->unlock ( );
00069                 return;
00070         }
00071         char quotes = ( char ) 34;
00072 
00073         ( * _textstream ) << "<div style=" << quotes << "background-color:#F6F6F6;"
00074                 << quotes << endl;
00075 
00076         // Create and add data-header
00077         stringstream header;
00078         header << setw ( 10 ) << setprecision ( 6 ) << fixed
00079                 << _clock->elapsedSeconds ( ) << " | ";
00080         int headerlength = header.str ( ).size ( );
00081         ( * _textstream ) << "<i>" << header.str ( ) << "</i>" << "[" << "<b>"
00082                 << clientname << "</b>" << "] " << message << endl;
00083         ( * _textstream ) << "</div>" << endl;
00084 
00085         ( * _textstream ) << "<div style=" << quotes
00086                 << "border-bottom:1px dashed black; padding-left: 80px" << quotes
00087                 << ">" << endl;
00088 
00089         // If there is some data
00090         if ( data != 0 && datasize > 0 )
00091         {
00092                 // Format data-header
00093                 string dataheader = string ( "<b>DATA: </b>" ) + dataname;
00094                 ( * _textstream ) << " " << dataheader << " " << "<br />" << endl;
00095 
00096                 // Add data
00097                 ( * _textstream ) << "<pre>" << formatData ( data, datasize, datatype,
00098                         0, _textwidth, 5, false ) << "</pre>" << endl;
00099         }
00100         ( * _textstream ) << "</div><br />" << endl;
00101         _mutex->unlock ( );
00102 }