Skip to content

Commit 73c5966

Browse files
authored
Merge pull request #62 from jmirabel/devel
[TracerRealTime] Check that output file is valid.
2 parents 3aa6df4 + 982ab8f commit 73c5966

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

include/dynamic-graph/tracer-real-time.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,18 @@ class DG_TRACERREALTIME_DLLAPI TracerRealTime : public Tracer {
5050
DG_TRACERREALTIME_DLLAPI friend std::ostream &
5151
operator<<(std::ostream &os, const TracerRealTime &t);
5252

53-
protected:
54-
virtual void openFile(const SignalBase<int> &sig,
55-
const std::string &filename);
56-
57-
virtual void recordSignal(std::ostream &os, const SignalBase<int> &sig);
5853
void emptyBuffers();
5954

6055
void setBufferSize(const int &SIZE) { bufferSize = SIZE; }
6156

6257
const int &getBufferSize() { return bufferSize; }
6358

59+
protected:
60+
virtual void openFile(const SignalBase<int> &sig,
61+
const std::string &filename);
62+
63+
virtual void recordSignal(std::ostream &os, const SignalBase<int> &sig);
64+
6465
typedef std::list<std::ofstream *> HardFileList;
6566
static const int BUFFER_SIZE_DEFAULT = 1048576; // 1Mo
6667

src/traces/tracer-real-time.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ void TracerRealTime::openFile(const SignalBase<int> &sig,
125125
string filename = rootdir + basename + signame + suffix;
126126
dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
127127
std::ofstream *newfile = new std::ofstream(filename.c_str());
128+
if (!newfile->good()) {
129+
delete newfile;
130+
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
131+
"Could not open file " + filename + " for signal " + signame, "");
132+
}
128133
dgDEBUG(5) << "Newfile:" << (void *)newfile << endl;
129134
hardFiles.push_back(newfile);
130135
dgDEBUG(5) << "Creating Outstringstream" << endl;
@@ -264,7 +269,7 @@ void TracerRealTime::recordSignal(std::ostream &os,
264269
<< "> " << endl;
265270

266271
} catch (ExceptionAbstract &exc) {
267-
throw exc;
272+
throw;
268273
} catch (...) {
269274
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
270275
"The buffer is not open", "");

src/traces/tracer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ Tracer::Tracer(const std::string n)
8888
void Tracer::addSignalToTrace(const SignalBase<int> &sig,
8989
const string &filename) {
9090
dgDEBUGIN(15);
91+
// openFile may throw so it should be called first.
92+
if (namesSet)
93+
openFile(sig, filename);
9194
toTraceSignals.push_back(&sig);
9295
dgDEBUGF(15, "%p", &sig);
9396
names.push_back(filename);
94-
if (namesSet)
95-
openFile(sig, filename);
9697
triger.addDependency(sig);
9798
dgDEBUGOUT(15);
9899
}

tests/debug-real-time-tracer.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,43 @@ BOOST_AUTO_TEST_CASE(test_tracer) {
6565
std::string basename("my-tracer");
6666
std::string suffix(".dat");
6767

68-
/// Test openfiles
69-
atracer.openFiles(rootdir, basename, suffix);
68+
atracer.setBufferSize(1<<14);
7069

71-
/// Add trace by name
70+
// Check that an exception is thrown if the filename is invalid.
71+
atracer.openFiles(rootdir, "invalid/filename", suffix);
72+
BOOST_CHECK_THROW(atracer.addSignalToTraceByName("my-entity.out_double", "output"),
73+
ExceptionTraces);
74+
75+
// Test openfiles
76+
atracer.openFiles(rootdir, basename, suffix);
77+
// Add trace by name
7278
atracer.addSignalToTraceByName("my-entity.out_double", "output");
7379

7480
/// Add trace by name
75-
SignalBase<int> &aSignal = entity.getSignal("out2double");
76-
77-
entity.m_sigdTwoTimeDepSOUT.recompute(2);
81+
SignalBase<int> &out_double = entity.getSignal("out_double");
82+
SignalBase<int> &out_double_2 = entity.getSignal("out2double");
7883

79-
Signal<double, int> &aSignalInt =
84+
Signal<double, int> &in_double =
8085
*(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double")));
8186

82-
aSignalInt.setConstant(1.5);
87+
in_double.setConstant(1.5);
8388
atracer.start();
8489

85-
atracer.trace();
86-
8790
std::string emptybuf_cmd_str("empty");
8891
command::Command *acmd = atracer.getNewStyleCommand(emptybuf_cmd_str);
8992
acmd->execute();
9093
for (int i = 0; i < 1000; i++) {
91-
aSignal.setTime(i);
92-
aSignalInt.setTime(i);
94+
in_double.setTime(i);
95+
out_double.recompute(i);
96+
out_double_2.recompute(i);
9397
atracer.recordTrigger(i, i);
9498
}
9599
output_test_stream output;
100+
96101
atracer.display(output);
97102

98103
atracer.stop();
104+
atracer.trace();
99105
atracer.clearSignalToTrace();
100106
atracer.closeFiles();
101107
acmd->execute();
@@ -105,5 +111,5 @@ BOOST_AUTO_TEST_CASE(test_tracer) {
105111
"TracerRealTime my-tracer [mode=play] : \n"
106112
" - Dep list: \n"
107113
" -> MyEntity(my-entity)::input(double)::out_double (in output)"
108-
" [0Mo/1Mo] \n"));
114+
" [9Ko/16Ko] \n"));
109115
}

0 commit comments

Comments
 (0)