LogFiles.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "LogFiles.h"
  2. #include <string.h>
  3. const string CLogFiles::m_strFileNameLoglaserBinary1 = "laser1.data";
  4. const string CLogFiles::m_strFileNameLoglaserValue1 = "laser1.txt";
  5. const string CLogFiles::m_strFileNameLogPoints1 = "points1.txt";
  6. const string CLogFiles::m_strFileNameLoglaserBinary2 = "laser2.data";
  7. const string CLogFiles::m_strFileNameLoglaserValue2 = "laser2.txt";
  8. const string CLogFiles::m_strFileNameLogPoints2 = "points2.txt";
  9. CLogFile::CLogFile()
  10. {
  11. }
  12. CLogFile::~CLogFile()
  13. {
  14. if (m_file)
  15. {
  16. m_lock.lock();
  17. m_file.close();
  18. m_lock.unlock();
  19. }
  20. }
  21. void CLogFile::open(const char *_Filename, bool binary /* = false */)
  22. {
  23. if (!m_file.is_open())
  24. {
  25. m_lock.lock();
  26. if (binary)
  27. m_file.open(_Filename, ios_base::out | ios_base::binary);
  28. else
  29. m_file.open(_Filename, ios_base::out);
  30. m_lock.unlock();
  31. }
  32. }
  33. void CLogFile::write(const char *_Str, streamsize _Count)
  34. {
  35. if (m_file.is_open())
  36. {
  37. m_lock.lock();
  38. m_file.write(_Str, _Count);
  39. m_lock.unlock();
  40. }
  41. }
  42. void CLogFile::write_double(double val)
  43. {
  44. char buffer[512] = { 0 };
  45. if (m_file.is_open())
  46. {
  47. m_lock.lock();
  48. sprintf(buffer, "%f ", val);
  49. m_file.write(buffer, strlen(buffer));
  50. m_lock.unlock();
  51. }
  52. }
  53. void CLogFile::write_int(int val)
  54. {
  55. char buffer[512] = { 0 };
  56. if (m_file.is_open())
  57. {
  58. m_lock.lock();
  59. sprintf(buffer, "%d ", val);
  60. m_file.write(buffer, strlen(buffer));
  61. m_lock.unlock();
  62. }
  63. }
  64. void CLogFile::close()
  65. {
  66. if (m_file.is_open())
  67. {
  68. m_lock.lock();
  69. m_file.close();
  70. m_lock.unlock();
  71. }
  72. }
  73. /****************************************************************************/
  74. CLogFiles::CLogFiles()
  75. : m_bBinary(true)
  76. , m_bValue(true)
  77. , m_bPoints(true)
  78. {
  79. }
  80. CLogFiles::~CLogFiles()
  81. {
  82. close_project();
  83. }
  84. void CLogFiles::close_project()
  85. {
  86. m_logLaserBinary1.close();
  87. m_logLaserValue1.close();
  88. m_logPoints1.close();
  89. m_logLaserBinary2.close();
  90. m_logLaserValue2.close();
  91. m_logPoints2.close();
  92. }
  93. void CLogFiles::new_project(const char * path)
  94. {
  95. close_project();
  96. if (m_bBinary)
  97. {
  98. string filepath1 = string(path) + m_strFileNameLoglaserBinary1;
  99. m_logLaserBinary1.open(filepath1.c_str(), true);
  100. string filepath3 = string(path) + m_strFileNameLoglaserBinary2;
  101. m_logLaserBinary2.open(filepath3.c_str(), true);
  102. }
  103. if (m_bValue)
  104. {
  105. string filepath1 = string(path) + m_strFileNameLoglaserValue1;
  106. m_logLaserValue1.open(filepath1.c_str(), true);
  107. string filepath3 = string(path) + m_strFileNameLoglaserValue2;
  108. m_logLaserValue2.open(filepath3.c_str(), true);
  109. }
  110. if (m_bPoints)
  111. {
  112. string filepath2 = string(path) + m_strFileNameLogPoints1;
  113. m_logPoints1.open(filepath2.c_str(), true);
  114. string filepath4 = string(path) + m_strFileNameLogPoints2;
  115. m_logPoints2.open(filepath4.c_str(), true);
  116. }
  117. }