Skip to content

Commit 1ccd27c

Browse files
committed
Support for messages to be printed during tape evaluations.
1 parent 32b43d4 commit 1ccd27c

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

include/opdi/misc/output.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131
namespace opdi {
3232

3333
struct Output {
34-
private:
34+
protected:
3535

3636
// print functionality
3737

3838
template<typename First, typename... Args>
39-
static void printRec(First const& first, Args const&... args) {
40-
std::cerr << first << " ";
41-
Output::printRec(args...);
39+
static void printRec(std::ostream& out, First const& first, Args const&... args) {
40+
out << first << " ";
41+
Output::printRec(out, args...);
4242
}
4343

4444
template<typename Last>
45-
static void printRec(Last const& last) {
46-
std::cerr << last << std::endl;
45+
static void printRec(std::ostream& out, Last const& last) {
46+
out << last << std::endl;
4747
}
4848

4949
public:
@@ -63,14 +63,14 @@ namespace opdi {
6363
template<typename... Args>
6464
static void print(Args const&... args) {
6565
omp_set_lock(&Output::lock);
66-
Output::printRec(args...);
66+
Output::printRec(std::cerr, args...);
6767
omp_unset_lock(&Output::lock);
6868
}
6969

7070
template<typename... Args>
7171
static void printThread(Args const&... args) {
7272
omp_set_lock(&Output::lock);
73-
Output::printRec("thread", omp_get_thread_num(), args...);
73+
Output::printRec(std::cerr, "thread", omp_get_thread_num(), args...);
7474
omp_unset_lock(&Output::lock);
7575
}
7676
};

include/opdi/misc/tapedOutput.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#pragma once
2727

28+
#include <sstream>
29+
2830
#include "../tool/toolInterface.hpp"
2931

3032
#include "output.hpp"
@@ -42,6 +44,21 @@ namespace opdi {
4244
TapedOutput::setActive(false);
4345
}
4446

47+
struct ReversePrintData {
48+
public:
49+
std::string message;
50+
};
51+
52+
static void reversePrint(void* dataPtr) {
53+
ReversePrintData* data = (ReversePrintData*)dataPtr;
54+
print(data->message);
55+
}
56+
57+
static void reversePrintDelete(void* dataPtr) {
58+
ReversePrintData* data = (ReversePrintData*)dataPtr;
59+
delete data;
60+
}
61+
4562
static bool active;
4663

4764
public:
@@ -65,5 +82,23 @@ namespace opdi {
6582
Output::print(args...);
6683
}
6784
}
85+
86+
template<typename... Args>
87+
static void printReverse(Args const&... args) {
88+
if (TapedOutput::active) {
89+
std::stringstream out;
90+
Output::printRec(out, args...);
91+
92+
ReversePrintData* data = new ReversePrintData;
93+
data->message = out.rdbuf()->str();
94+
95+
Handle* handle = new Handle;
96+
handle->data = data;
97+
handle->reverseFunc = reversePrint;
98+
handle->deleteFunc = reversePrintDelete;
99+
100+
tool->pushExternalFunction(tool->getThreadLocalTape(), handle);
101+
}
102+
}
68103
};
69104
}

0 commit comments

Comments
 (0)