Skip to content

Commit

Permalink
Merge pull request #8 from arminms:issue_#7
Browse files Browse the repository at this point in the history
Fix sync issue and improve log retrieval (#7)
  • Loading branch information
arminms authored Feb 2, 2024
2 parents 0bca8fd + 86ec556 commit 830c467
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ option(G3P_ENABLE_TESTS "Enable the unit tests ?" ON)
#
project(
g3p
VERSION 1.0.1
VERSION 1.0.2
LANGUAGES CXX
DESCRIPTION "gnuplot for Modern C++ with support for Jupyter"
)
Expand Down
77 changes: 36 additions & 41 deletions include/g3p/gnuplot
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace g3p
// -- isolated functions in a namespace ----------------------------------------

namespace detail
{ inline std::string random_name(const int len)
{ inline std::string random_name(const int len = 8)
{ static const char alphanum[] =
"1234567890"
"abcdefghijklmnopqrstuvwxyz";
Expand All @@ -60,21 +60,13 @@ namespace g3p
return tmp_s;
}


inline std::string tmpnam()
{ auto filename = std::filesystem::temp_directory_path().string();
filename += "/g3p" + random_name(16);
return filename;
}

void wait4signal(std::uintmax_t size, const std::filesystem::path& file)
{ auto start = std::chrono::system_clock::now();
while
( std::filesystem::file_size(file) == size
&& std::chrono::system_clock::now() - start <= std::chrono::seconds{1}
)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

std::string get_last_lines
( const std::string& filename
, size_t line_count
Expand Down Expand Up @@ -110,6 +102,11 @@ namespace g3p
#endif //_MSC_VER
return std::string(start, end);
}

void wait4tag(const std::string& tag, const std::string& filename)
{ while (tag != get_last_lines(filename, 1))
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

// -- simulated ostream manipulators -------------------------------------------
Expand Down Expand Up @@ -204,23 +201,19 @@ namespace g3p
// -- log file -----------------------------------------------------------------

std::string log(size_t line_count = 0) const
{ if (_logfile.empty())
return std::string{"g3p::gnuplot -- no log file!\n"};
std::filesystem::path f(_logfile);
while(!std::filesystem::exists(f))
std::this_thread::sleep_for(std::chrono::milliseconds(1));
auto size = std::filesystem::file_size(f);
fflush(_gp);
std::thread t(detail::wait4signal, size, std::cref(f));
t.join();
{ sync();
std::string log;
if (0 == line_count)
{ std::ifstream fin(_logfile);
std::stringstream log;
log << fin.rdbuf();
return log.str();
std::stringstream logstream;
logstream << fin.rdbuf();
log = logstream.str();
return log.erase(log.size() - 9, 9);
}
else
return detail::get_last_lines(_logfile, line_count);
{ log = detail::get_last_lines(_logfile, line_count + 1);
return log.erase(log.size() - 9, 9);
}
}

#ifdef __CLING__
Expand All @@ -232,6 +225,18 @@ namespace g3p

#endif //__CLING__

// -- sync helper function -------------------------------------------------

const gnuplot& sync() const
{ auto tag = detail::random_name(8);
fprintf(_gp, "print '%s'\n", tag.c_str());
fflush(_gp);
tag += '\n';
std::thread t(detail::wait4tag, tag, _logfile);
t.join();
return *this;
}

#ifdef __GNUG__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wformat-security"
Expand All @@ -253,7 +258,10 @@ namespace g3p
}

std::istringstream operator>> (size_t line_count) const
{ std::istringstream iss{log(line_count)};
{ sync();
auto line = detail::get_last_lines(_logfile, line_count + 1);
line.erase(line.size() - 9, 9);
std::istringstream iss{line};
return iss;
}

Expand Down Expand Up @@ -306,25 +314,12 @@ namespace g3p
}

#ifdef __CLING__
namespace detail
{ void wait4tag(std::string tag, const gnuplot& gp)
{ while (tag.npos == gp.log().rfind(tag))
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

nlohmann::json mime_bundle_repr(const gnuplot& gp)
{ std::filesystem::path f(gp.plotfile());
auto tag = detail::random_name(8);
gp("print \"plot -> %s\"", tag.c_str()).flush();
std::thread t(detail::wait4tag, tag, std::ref(gp));
t.join();
{ gp.sync();
std::filesystem::path f(gp.plotfile());
if (0 == std::filesystem::file_size(f))
{ tag = detail::random_name(8);
gp ("replot")
("print \"plot -> %s\"", tag.c_str()).flush();
std::thread t(detail::wait4tag, tag, std::cref(gp));
t.join();
{ gp("replot");
gp.sync();
}
std::ifstream fin(gp.plotfile(), std::ios::binary);
std::stringstream buffer;
Expand Down
1 change: 0 additions & 1 deletion test/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include <algorithm>
#include <catch2/catch_all.hpp>
#include <g3p/gnuplot>

Expand Down

0 comments on commit 830c467

Please sign in to comment.