00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00040 #include <string>
00041 #include <algorithm>
00042 #include <vector>
00043 #include <iostream>
00044 #include <fstream>
00045
00046 namespace Jack
00047 {
00048
00053 struct SERVER_EXPORT JackTools
00054 {
00055 static int GetPID();
00056 static int GetUID();
00057
00058 static char* UserDir();
00059 static char* ServerDir ( const char* server_name, char* server_dir );
00060 static const char* DefaultServerName();
00061 static void CleanupFiles ( const char* server_name );
00062 static int GetTmpdir();
00063 static void RewriteName ( const char* name, char* new_name );
00064 };
00065
00070 class SERVER_EXPORT JackArgParser
00071 {
00072 private:
00073
00074 std::string fArgString;
00075 int fArgc;
00076 std::vector<std::string> fArgv;
00077
00078 public:
00079
00080 JackArgParser ( const char* arg );
00081 ~JackArgParser();
00082 std::string GetArgString();
00083 int GetNumArgv();
00084 int GetArgc();
00085 int GetArgv ( std::vector<std::string>& argv );
00086 int GetArgv ( char** argv );
00087 void DeleteArgv ( const char** argv );
00088 void ParseParams ( jack_driver_desc_t* desc, JSList** param_list );
00089 void FreeParams ( JSList* param_list );
00090 };
00091
00109 template <class T> class JackGnuPlotMonitor
00110 {
00111 private:
00112 uint32_t fMeasureCnt;
00113 uint32_t fMeasurePoints;
00114 uint32_t fMeasureId;
00115 T* fCurrentMeasure;
00116 T** fMeasureTable;
00117 uint32_t fTablePos;
00118 std::string fName;
00119
00120 public:
00121 JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
00122 {
00123 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
00124
00125 fMeasureCnt = measure_cnt;
00126 fMeasurePoints = measure_points;
00127 fTablePos = 0;
00128 fName = name;
00129 fCurrentMeasure = new T[fMeasurePoints];
00130 fMeasureTable = new T*[fMeasureCnt];
00131 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00132 {
00133 fMeasureTable[cnt] = new T[fMeasurePoints];
00134 fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
00135 }
00136 }
00137
00138 ~JackGnuPlotMonitor()
00139 {
00140 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
00141
00142 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00143 delete[] fMeasureTable[cnt];
00144 delete[] fMeasureTable;
00145 delete[] fCurrentMeasure;
00146 }
00147
00148 T AddNew ( T measure_point )
00149 {
00150 fMeasureId = 0;
00151 return fCurrentMeasure[fMeasureId++] = measure_point;
00152 }
00153
00154 uint32_t New()
00155 {
00156 return fMeasureId = 0;
00157 }
00158
00159 T Add ( T measure_point )
00160 {
00161 return fCurrentMeasure[fMeasureId++] = measure_point;
00162 }
00163
00164 uint32_t AddLast ( T measure_point )
00165 {
00166 fCurrentMeasure[fMeasureId] = measure_point;
00167 fMeasureId = 0;
00168 return Write();
00169 }
00170
00171 uint32_t Write()
00172 {
00173 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00174 fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
00175 if ( ++fTablePos == fMeasureCnt )
00176 fTablePos = 0;
00177 return fTablePos;
00178 }
00179
00180 int Save ( std::string name = std::string ( "" ) )
00181 {
00182 std::string filename = ( name.empty() ) ? fName : name;
00183 filename += ".log";
00184
00185 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
00186
00187 std::ofstream file ( filename.c_str() );
00188
00189 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00190 {
00191 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00192 file << fMeasureTable[cnt][point] << " \t";
00193 file << std::endl;
00194 }
00195
00196 file.close();
00197 return 0;
00198 }
00199
00200 int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
00201 std::string* field_names = NULL, uint32_t field_number = 0,
00202 std::string name = std::string ( "" ) )
00203 {
00204 std::string title = ( name.empty() ) ? fName : name;
00205 std::string plot_filename = title + ".plt";
00206 std::string data_filename = title + ".log";
00207
00208 std::ofstream file ( plot_filename.c_str() );
00209
00210 file << "set multiplot" << std::endl;
00211 file << "set grid" << std::endl;
00212 file << "set title \"" << title << "\"" << std::endl;
00213
00214 for ( uint32_t i = 0; i < options_number; i++ )
00215 file << options_list[i] << std::endl;
00216
00217 file << "plot ";
00218 for ( uint32_t row = 1; row <= field_number; row++ )
00219 {
00220 file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
00221 file << ( ( row < field_number ) ? ", " : "\n" );
00222 }
00223
00224 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
00225
00226 file.close();
00227 return 0;
00228 }
00229 };
00230 }
00231
00232 #endif