jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.3KB

  1. /*
  2. Copyright (C) 2006-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackTools__
  16. #define __JackTools__
  17. #ifdef WIN32
  18. #include <windows.h>
  19. #else
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <dirent.h>
  23. #endif
  24. #ifdef __APPLE__
  25. #include <sys/syslimits.h>
  26. #endif
  27. #include <string>
  28. #include <algorithm>
  29. #include <vector>
  30. #include <iostream>
  31. #include <fstream>
  32. #include "jslist.h"
  33. #include "driver_interface.h"
  34. #include "JackExports.h"
  35. #include "JackError.h"
  36. namespace Jack
  37. {
  38. /*!
  39. \brief Utility functions.
  40. */
  41. struct EXPORT JackTools
  42. {
  43. static int GetPID();
  44. static int GetUID();
  45. static char* UserDir();
  46. static char* ServerDir ( const char* server_name, char* server_dir );
  47. static const char* DefaultServerName();
  48. static void CleanupFiles ( const char* server_name );
  49. static int GetTmpdir();
  50. static void RewriteName ( const char* name, char* new_name );
  51. };
  52. /*!
  53. \brief Internal cient command line parser.
  54. */
  55. class EXPORT JackArgParser
  56. {
  57. private:
  58. std::string fArgString;
  59. int fArgc;
  60. std::vector<std::string> fArgv;
  61. public:
  62. JackArgParser ( const char* arg );
  63. ~JackArgParser();
  64. std::string GetArgString();
  65. int GetNumArgv();
  66. int GetArgc();
  67. int GetArgv ( std::vector<std::string>& argv );
  68. int GetArgv ( char** argv );
  69. void DeleteArgv ( const char** argv );
  70. int ParseParams ( jack_driver_desc_t* desc, JSList** param_list );
  71. };
  72. /*!
  73. \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
  74. */
  75. template <class T> class JackGnuPlotMonitor
  76. {
  77. private:
  78. uint32_t fMeasureCnt;
  79. uint32_t fMeasurePoints;
  80. T** fMeasureTable;
  81. uint32_t fTablePos;
  82. std::string fName;
  83. public:
  84. JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
  85. {
  86. jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor measure_cnt %u measure_points %u", measure_cnt, measure_points );
  87. fMeasureCnt = measure_cnt;
  88. fMeasurePoints = measure_points;
  89. fTablePos = 0;
  90. fName = name;
  91. fMeasureTable = new T*[fMeasureCnt];
  92. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  93. {
  94. fMeasureTable[cnt] = new T[fMeasurePoints];
  95. fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
  96. }
  97. }
  98. ~JackGnuPlotMonitor()
  99. {
  100. jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
  101. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  102. delete[] fMeasureTable[cnt];
  103. delete[] fMeasureTable;
  104. }
  105. uint32_t Write ( T* measure )
  106. {
  107. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  108. fMeasureTable[fTablePos][point] = measure[point];
  109. if ( ++fTablePos == fMeasureCnt )
  110. fTablePos = 0;
  111. return fTablePos;
  112. }
  113. int Save ( std::string name = std::string ( "" ) )
  114. {
  115. std::string filename = ( name.empty() ) ? fName : name;
  116. filename += ".log";
  117. jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
  118. std::ofstream file ( filename.c_str() );
  119. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  120. {
  121. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  122. file << fMeasureTable[cnt][point] << " \t";
  123. file << std::endl;
  124. }
  125. file.close();
  126. return 0;
  127. }
  128. int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
  129. std::string* field_names = NULL, uint32_t field_number = 0,
  130. std::string name = std::string ( "" ) )
  131. {
  132. std::string title = ( name.empty() ) ? fName : name;
  133. std::string plot_filename = title + ".plt";
  134. std::string data_filename = title + ".log";
  135. std::ofstream file ( plot_filename.c_str() );
  136. file << "set multiplot" << std::endl;
  137. file << "set grid" << std::endl;
  138. file << "set title \"" << title << "\"" << std::endl;
  139. for ( uint32_t i = 0; i < options_number; i++ )
  140. {
  141. jack_log ( "JackGnuPlotMonitor::SetPlotFile - Add plot option : '%s'", options_list[i].c_str() );
  142. file << options_list[i] << std::endl;
  143. }
  144. file << "plot ";
  145. for ( uint32_t row = 1; row <= field_number; row++ )
  146. {
  147. jack_log ( "JackGnuPlotMonitor::SetPlotFile - Add plot : file '%s' row '%d' title '%s' field '%s'",
  148. data_filename.c_str(), row, title.c_str(), field_names[row-1].c_str() );
  149. file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
  150. file << ( ( row < field_number ) ? ", " : "\n" );
  151. }
  152. jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot '.plt' file to '%s'", plot_filename.c_str() );
  153. file.close();
  154. return 0;
  155. }
  156. };
  157. }
  158. #endif