Jack2 1.9.7

JackTools.h

00001 /*
00002   Copyright (C) 2006-2008 Grame
00003 
00004   This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU Lesser General Public License as published by
00006   the Free Software Foundation; either version 2.1 of the License, or
00007   (at your option) any later version.
00008 
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU Lesser General Public License for more details.
00013 
00014   You should have received a copy of the GNU Lesser General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #ifndef __JackTools__
00021 #define __JackTools__
00022 
00023 #ifdef WIN32
00024 #include <windows.h>
00025 #define DIR_SEPARATOR '\\'
00026 #else
00027 #define DIR_SEPARATOR '/'
00028 #include <sys/stat.h>
00029 #include <sys/types.h>
00030 #include <unistd.h>
00031 #include <dirent.h>
00032 #endif
00033 
00034 #ifdef __APPLE__
00035 #include <sys/syslimits.h>
00036 #endif
00037 
00038 #include "jslist.h"
00039 #include "driver_interface.h"
00040 #include "JackCompilerDeps.h"
00041 #include "JackError.h"
00042 #include "JackException.h"
00043 
00044 #include <string>
00045 #include <algorithm>
00046 #include <vector>
00047 #include <iostream>
00048 #include <fstream>
00049 
00050 namespace Jack
00051 {
00052 
00057     struct SERVER_EXPORT JackTools
00058     {
00059         static int GetPID();
00060         static int GetUID();
00061 
00062         static void KillServer();
00063 
00064         static int MkDir(const char* path);
00065         static char* UserDir();
00066         static char* ServerDir ( const char* server_name, char* server_dir );
00067         static const char* DefaultServerName();
00068         static void CleanupFiles ( const char* server_name );
00069         static int GetTmpdir();
00070         static void RewriteName ( const char* name, char* new_name );
00071         
00072         static void ThrowJackNetException();
00073     };
00074 
00092     template <class T> class JackGnuPlotMonitor
00093     {
00094         private:
00095             uint32_t fMeasureCnt;
00096             uint32_t fMeasurePoints;
00097             uint32_t fMeasureId;
00098             T* fCurrentMeasure;
00099             T** fMeasureTable;
00100             uint32_t fTablePos;
00101             std::string fName;
00102 
00103         public:
00104             JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
00105             {
00106                 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
00107 
00108                 fMeasureCnt = measure_cnt;
00109                 fMeasurePoints = measure_points;
00110                 fTablePos = 0;
00111                 fName = name;
00112                 fCurrentMeasure = new T[fMeasurePoints];
00113                 fMeasureTable = new T*[fMeasureCnt];
00114                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00115                 {
00116                     fMeasureTable[cnt] = new T[fMeasurePoints];
00117                     fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
00118                 }
00119             }
00120 
00121             ~JackGnuPlotMonitor()
00122             {
00123                 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
00124 
00125                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00126                     delete[] fMeasureTable[cnt];
00127                 delete[] fMeasureTable;
00128                 delete[] fCurrentMeasure;
00129             }
00130 
00131             T AddNew ( T measure_point )
00132             {
00133                                 fMeasureId = 0;
00134                                 return fCurrentMeasure[fMeasureId++] = measure_point;
00135             }
00136 
00137                         uint32_t New()
00138                         {
00139                                 return fMeasureId = 0;
00140                         }
00141 
00142             T Add ( T measure_point )
00143             {
00144                                 return fCurrentMeasure[fMeasureId++] = measure_point;
00145             }
00146 
00147             uint32_t AddLast ( T measure_point )
00148             {
00149                 fCurrentMeasure[fMeasureId] = measure_point;
00150                 fMeasureId = 0;
00151                 return Write();
00152             }
00153 
00154             uint32_t Write()
00155             {
00156                 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00157                     fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
00158                 if ( ++fTablePos == fMeasureCnt )
00159                     fTablePos = 0;
00160                 return fTablePos;
00161             }
00162 
00163             int Save ( std::string name = std::string ( "" ) )
00164             {
00165                 std::string filename = ( name.empty() ) ? fName : name;
00166                 filename += ".log";
00167 
00168                 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
00169 
00170                 std::ofstream file ( filename.c_str() );
00171 
00172                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00173                 {
00174                     for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00175                         file << fMeasureTable[cnt][point] << " \t";
00176                     file << std::endl;
00177                 }
00178 
00179                 file.close();
00180                 return 0;
00181             }
00182 
00183             int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
00184                               std::string* field_names = NULL, uint32_t field_number = 0,
00185                               std::string name = std::string ( "" ) )
00186             {
00187                 std::string title = ( name.empty() ) ? fName : name;
00188                 std::string plot_filename = title + ".plt";
00189                 std::string data_filename = title + ".log";
00190 
00191                 std::ofstream file ( plot_filename.c_str() );
00192 
00193                 file << "set multiplot" << std::endl;
00194                 file << "set grid" << std::endl;
00195                 file << "set title \"" << title << "\"" << std::endl;
00196 
00197                 for ( uint32_t i = 0; i < options_number; i++ )
00198                     file << options_list[i] << std::endl;
00199 
00200                 file << "plot ";
00201                 for ( uint32_t row = 1; row <= field_number; row++ )
00202                 {
00203                     file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
00204                     file << ( ( row < field_number ) ? ", " : "\n" );
00205                 }
00206 
00207                 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
00208 
00209                 file.close();
00210                 return 0;
00211             }
00212     };
00213 
00214     void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
00215     void PrintLoadError(const char* so_name);
00216 
00217 }
00218 
00219 #endif

Generated for Jack2 by doxygen 1.7.3