Skip to content

Commit

Permalink
Remove std io stream interceptors
Browse files Browse the repository at this point in the history
Stream interceptors destroy the multi-threading capabilities of the
g2o library. We removed them and manage error handling by return values
as would be expected from a c/c++ library. Std io streams are silenced
in the library.
  • Loading branch information
Manuel Luitz committed Mar 4, 2019
1 parent e8bb2d1 commit 73ce827
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 128 deletions.
3 changes: 1 addition & 2 deletions g2o/apps/g2o_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ if (NOT (G2O_USE_OPENGL OR BUILD_LGPL_SHARED_LIBS OR BUILD_SHARED_LIBS OR NOT CM
# g2o library without dependencies
add_library(g2o_lib SHARED
g2o_lib.cpp
stream_interceptor.cpp
stream_interceptor.h)
)

include_directories(${CSPARSE_INCLUDE_DIR})

Expand Down
68 changes: 26 additions & 42 deletions g2o/apps/g2o_lib/g2o_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,6 @@ void sigquit_handler(int sig)
}
}


StringCallbackFuncType cerrCallback = NULL;
StringCallbackFuncType coutCallback = NULL;

void setCerrListener(StringCallbackFuncType callbackFunc) {
cerrCallback = callbackFunc;
}

void setCoutListener(StringCallbackFuncType callbackFunc) {
coutCallback = callbackFunc;
}

void cerrCallbackFunc(const char *msg) {
if (cerrCallback != NULL) {
cerrCallback(msg);
}
}

void coutCallbackFunc(const char *msg) {
if (coutCallback != NULL) {
coutCallback(msg);
}
}

static StreamInterceptor cerrInterceptor(std::cerr, cerrCallbackFunc);
static StreamInterceptor coutInterceptor(std::cout, coutCallbackFunc);


int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputFactorGraphReference) {
OptimizableGraph::initMultiThreading();
int maxIterations;
Expand Down Expand Up @@ -215,6 +187,12 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF

arg.parseArgs(argc, argv);

// silence std i/o streams
if (!verbose) {
std::cout.setstate(std::ios_base::failbit);
std::cerr.setstate(std::ios_base::failbit);
}

if (verbose) {
cout << "# Used Compiler: " << G2O_CXX_COMPILER << endl;
}
Expand Down Expand Up @@ -269,7 +247,7 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
optimizer.setAlgorithm(solverFactory->construct(strSolver, solverProperty));
if (! optimizer.solver()) {
cerr << "Error allocating solver. Allocating \"" << strSolver << "\" failed!" << endl;
return 0;
return G2OResult::ErrorAllocatingSolver;
}

if (solverProperties.size() > 0) {
Expand All @@ -291,38 +269,38 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
std::istringstream inputBuffer(inputFactorGraph);
if (!optimizer.load(inputBuffer)) {
cerr << "Error loading graph" << endl;
return 2;
return G2OResult::ErrorLoadingGraph;
}
} else if (inputFilename == "-") {
cerr << "Read input from stdin" << endl;
if (!optimizer.load(cin)) {
cerr << "Error loading graph" << endl;
return 2;
return G2OResult::ErrorLoadingGraph;
}
} else {
cerr << "Read input from " << inputFilename << endl;
ifstream ifs(inputFilename.c_str());
if (!ifs) {
cerr << "Failed to open file" << endl;
return 1;
return G2OResult::ErrorOpeningFile;
}
if (!optimizer.load(ifs)) {
cerr << "Error loading graph" << endl;
return 2;
return G2OResult::ErrorLoadingGraph;
}
}
cerr << "Loaded " << optimizer.vertices().size() << " vertices" << endl;
cerr << "Loaded " << optimizer.edges().size() << " edges" << endl;

if (optimizer.vertices().size() == 0) {
cerr << "Graph contains no vertices" << endl;
return 1;
return G2OResult::ErrorGraphContainsNoVertices;
}

set<int> vertexDimensions = optimizer.dimensions();
if (! optimizer.isSolverSuitable(solverProperty, vertexDimensions)) {
cerr << "The selected solver is not suitable for optimizing the given graph" << endl;
return 3;
return G2OResult::ErrorSolverCannotOptimizeGraph;
}
assert (optimizer.solver());
//optimizer.setMethod(str2method(strMethod));
Expand All @@ -339,7 +317,7 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
OptimizableGraph::Vertex* v=optimizer.vertex(id);
if (!v){
cerr << "fatal, not found the vertex of id " << id << " in the gaugeList. Aborting";
return -1;
return G2OResult::ErrorVertexNotFound;
} else {
if (i==0)
gauge = v;
Expand All @@ -355,10 +333,11 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
if (gaugeFreedom) {
if (! gauge) {
cerr << "# cannot find a vertex to fix in this thing" << endl;
return 2;
return G2OResult::ErrorGraphIsFixedByNode;
} else {
cerr << "# graph is fixed by node " << gauge->id() << endl;
gauge->setFixed(true);
return G2OResult::ErrorGraphIsFixedByNode;
}
} else {
cerr << "# graph is fixed by priors or already fixed vertex" << endl;
Expand Down Expand Up @@ -472,8 +451,10 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
bool v1Added = optimizer.addVertex(v);
//cerr << "adding" << v->id() << "(" << v->dimension() << ")" << endl;
assert(v1Added);
if (! v1Added)
if (! v1Added) {
cerr << "Error adding vertex " << v->id() << endl;
return G2OResult::ErrorAddingVertex;
}
else
verticesAdded.insert(v);
doInit = 1;
Expand All @@ -486,8 +467,10 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
bool v2Added = optimizer.addVertex(v);
//cerr << "adding" << v->id() << "(" << v->dimension() << ")" << endl;
assert(v2Added);
if (! v2Added)
if (! v2Added) {
cerr << "Error adding vertex " << v->id() << endl;
return G2OResult::ErrorAddingVertex;
}
else
verticesAdded.insert(v);
doInit = 2;
Expand Down Expand Up @@ -550,12 +533,12 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
if (firstRound) {
if (!optimizer.initializeOptimization()){
cerr << "initialization failed" << endl;
return 0;
return G2OResult::ErrorInitializationFailed;
}
} else {
if (! optimizer.updateInitialization(verticesAdded, edgesAdded)) {
cerr << "updating initialization failed" << endl;
return 0;
return G2OResult::ErrorInitializationFailed;
}
}
verticesAdded.clear();
Expand Down Expand Up @@ -628,6 +611,7 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
int result=optimizer.optimize(maxIterations);
if (maxIterations > 0 && result==OptimizationAlgorithm::Fail){
cerr << "Cholesky failed, result might be invalid" << endl;
return G2OResult::ErrorCholeskyFailed;
} else if (computeMarginals){
std::vector<std::pair<int, int> > blockIndices;
for (size_t i=0; i<optimizer.activeVertices().size(); i++){
Expand Down Expand Up @@ -747,7 +731,7 @@ int optimize(int argc, char **argv, const char *inputFactorGraph, char **outputF
//OptimizationAlgorithmFactory::destroy();
//HyperGraphActionLibrary::destroy();

return 0;
return G2OResult::Ok;
}

void cleanup(char *outputFactorGraphReference) {
Expand Down
26 changes: 14 additions & 12 deletions g2o/apps/g2o_lib/g2o_lib.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include "stream_interceptor.h"
#include "g2o_lib_api.h"

typedef void(*StringCallbackFuncType)(const char *message);

extern "C" {
/**
* Set the callback function for the stdout interceptor
*
* @param callbackFunc Callback function
*/
G2O_DLL_API void setCoutListener(StringCallbackFuncType callbackFunc);

/**
* Set the callback function for the stderr interceptor
*
* @param callbackFunc Callback function
* Return codes of g2o optimization
*/
G2O_DLL_API void setCerrListener(StringCallbackFuncType callbackFunc);
G2O_DLL_API enum G2OResult {
Ok = 0,
ErrorLoadingGraph = -1,
ErrorOpeningFile = -2,
ErrorAllocatingSolver = -3,
ErrorGraphContainsNoVertices = -4,
ErrorSolverCannotOptimizeGraph = -5,
ErrorVertexNotFound = -6,
ErrorGraphIsFixedByNode = -7,
ErrorAddingVertex = -8,
ErrorInitializationFailed = -9,
ErrorCholeskyFailed = -10
};

/**
* Optimize a factor graph with g2o
Expand Down
42 changes: 0 additions & 42 deletions g2o/apps/g2o_lib/stream_interceptor.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions g2o/apps/g2o_lib/stream_interceptor.h

This file was deleted.

0 comments on commit 73ce827

Please sign in to comment.