Jack2 1.9.6

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 #else
00026 #include <sys/types.h>
00027 #include <unistd.h>
00028 #include <dirent.h>
00029 #endif
00030 
00031 #ifdef __APPLE__
00032 #include <sys/syslimits.h>
00033 #endif
00034 
00035 #include "jslist.h"
00036 #include "driver_interface.h"
00037 #include "JackCompilerDeps.h"
00038 #include "JackError.h"
00039 #include "JackException.h"
00040 
00041 #include <string>
00042 #include <algorithm>
00043 #include <vector>
00044 #include <iostream>
00045 #include <fstream>
00046 
00047 namespace Jack
00048 {
00049 
00054     struct SERVER_EXPORT JackTools
00055     {
00056         static int GetPID();
00057         static int GetUID();
00058 
00059         static void KillServer();
00060 
00061         static char* UserDir();
00062         static char* ServerDir ( const char* server_name, char* server_dir );
00063         static const char* DefaultServerName();
00064         static void CleanupFiles ( const char* server_name );
00065         static int GetTmpdir();
00066         static void RewriteName ( const char* name, char* new_name );
00067         
00068         static void ThrowJackNetException();
00069     };
00070 
00088     template <class T> class JackGnuPlotMonitor
00089     {
00090         private:
00091             uint32_t fMeasureCnt;
00092             uint32_t fMeasurePoints;
00093             uint32_t fMeasureId;
00094             T* fCurrentMeasure;
00095             T** fMeasureTable;
00096             uint32_t fTablePos;
00097             std::string fName;
00098 
00099         public:
00100             JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
00101             {
00102                 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
00103 
00104                 fMeasureCnt = measure_cnt;
00105                 fMeasurePoints = measure_points;
00106                 fTablePos = 0;
00107                 fName = name;
00108                 fCurrentMeasure = new T[fMeasurePoints];
00109                 fMeasureTable = new T*[fMeasureCnt];
00110                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00111                 {
00112                     fMeasureTable[cnt] = new T[fMeasurePoints];
00113                     fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
00114                 }
00115             }
00116 
00117             ~JackGnuPlotMonitor()
00118             {
00119                 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
00120 
00121                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00122                     delete[] fMeasureTable[cnt];
00123                 delete[] fMeasureTable;
00124                 delete[] fCurrentMeasure;
00125             }
00126 
00127             T AddNew ( T measure_point )
00128             {
00129                                 fMeasureId = 0;
00130                                 return fCurrentMeasure[fMeasureId++] = measure_point;
00131             }
00132 
00133                         uint32_t New()
00134                         {
00135                                 return fMeasureId = 0;
00136                         }
00137 
00138             T Add ( T measure_point )
00139             {
00140                                 return fCurrentMeasure[fMeasureId++] = measure_point;
00141             }
00142 
00143             uint32_t AddLast ( T measure_point )
00144             {
00145                 fCurrentMeasure[fMeasureId] = measure_point;
00146                 fMeasureId = 0;
00147                 return Write();
00148             }
00149 
00150             uint32_t Write()
00151             {
00152                 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00153                     fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
00154                 if ( ++fTablePos == fMeasureCnt )
00155                     fTablePos = 0;
00156                 return fTablePos;
00157             }
00158 
00159             int Save ( std::string name = std::string ( "" ) )
00160             {
00161                 std::string filename = ( name.empty() ) ? fName : name;
00162                 filename += ".log";
00163 
00164                 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
00165 
00166                 std::ofstream file ( filename.c_str() );
00167 
00168                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00169                 {
00170                     for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00171                         file << fMeasureTable[cnt][point] << " \t";
00172                     file << std::endl;
00173                 }
00174 
00175                 file.close();
00176                 return 0;
00177             }
00178 
00179             int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
00180                               std::string* field_names = NULL, uint32_t field_number = 0,
00181                               std::string name = std::string ( "" ) )
00182             {
00183                 std::string title = ( name.empty() ) ? fName : name;
00184                 std::string plot_filename = title + ".plt";
00185                 std::string data_filename = title + ".log";
00186 
00187                 std::ofstream file ( plot_filename.c_str() );
00188 
00189                 file << "set multiplot" << std::endl;
00190                 file << "set grid" << std::endl;
00191                 file << "set title \"" << title << "\"" << std::endl;
00192 
00193                 for ( uint32_t i = 0; i < options_number; i++ )
00194                     file << options_list[i] << std::endl;
00195 
00196                 file << "plot ";
00197                 for ( uint32_t row = 1; row <= field_number; row++ )
00198                 {
00199                     file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
00200                     file << ( ( row < field_number ) ? ", " : "\n" );
00201                 }
00202 
00203                 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
00204 
00205                 file.close();
00206                 return 0;
00207             }
00208     };
00209 
00210     void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
00211     void PrintLoadError(const char* so_name);
00212 
00213 }
00214 
00215 #endif