Skip to content
Snippets Groups Projects
Commit 27a6e7ea authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH: python logger (wip)

parent 8a7a9329
Branches
Tags
No related merge requests found
......@@ -29,6 +29,8 @@
#include "itkCommand.h"
#include "itkLogOutput.h"
typedef itk::LightObject itkLightObject;
typedef itk::LightObject::Pointer itkLightObject_Pointer;
typedef itk::Object itkObject;
......@@ -60,4 +62,6 @@ typedef itk::EndPickEvent itkEndPickEvent;
typedef itk::AbortCheckEvent itkAbortCheckEvent;
typedef itk::UserEvent itkUserEvent;
typedef itk::LogOutput itkLogOutput;
#endif
......@@ -203,6 +203,11 @@ public:
} // end of namespace otb
#if SWIGPYTHON
%include "otbPythonLogOutput.i"
#endif
class Application: public itkObject
{
public:
......
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if SWIGPYTHON
%module(directors="1") cb
%{
#include "otbPythonLogOutput.h"
#include "otbLogOutputCallback.h"
%}
%feature("director") LogOutputCallback;
%include "otbLogOutputCallback.h"
%pythoncode
{
class PythonLogOutputCallback(LogOutputCallback):
def __init__(self):
super(PythonLogOutputCallback, self).__init__()
def Call(self, content):
print(content)
def Flush(self):
sys.stdout.flush()
}
#endif
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbPythonLogOutputCallback_h
#define otbPythonLogOutputCallback_h
namespace otb
{
class LogOutputCallback
{
public:
/** Constructor */
LogOutputCallback() = default;
/** Destructor */
virtual ~LogOutputCallback() = default;
virtual void Call(std::string const & content) {};
virtual void Flush() {};
};
} // namespace otb
#endif // otbPythonLogOutputCallback_h
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "otbPythonLogOutput.h"
namespace otb
{
void PythonLogOutput::Flush()
{
m_Callback.Flush();
}
void PythonLogOutput::Write(std::string const & content)
{
m_Callback.Call(content);
}
void PythonLogOutput::PrintSelf(std::ostream & os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // namespace otb
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbPythonLogOutput_h
#define otbPythonLogOutput_h
#include "itkLogOutput.h"
#include "otbLogOutputCallback.h"
namespace otb
{
class PythonLogOutput : public itk::LogOutput
{
public:
typedef PythonLogOutput Self;
typedef itk::LogOutput Superclass;
typedef itk::SmartPointer< Self > Pointer;
typedef itk::SmartPointer< const Self > ConstPointer;
typedef LogOutputCallback CallbackType;
itkTypeMacro(PythonLogOutput, itk::LogOutput);
itkNewMacro(PythonLogOutput);
/** Set the callback method */
void SetCallback(CallbackType const & callback)
{
m_Callback = callback;
this->Modified();
}
/** flush a buffer */
virtual void Flush() override;
/** Write to multiple outputs */
virtual void Write(double timestamp) override {};
/** Write to a buffer */
virtual void Write(std::string const & content) override;
/** Write to a buffer */
virtual void Write(std::string const & content, double timestamp) override {};
protected:
/** Constructor */
PythonLogOutput() = default;
/** Destructor */
virtual ~PythonLogOutput() override = default;
virtual void PrintSelf(std::ostream & os, itk::Indent indent) const override;
private:
CallbackType m_Callback;
};
} // namespace otb
#endif // otbPythonLogOutput_h
......@@ -32,8 +32,10 @@ set(SWIG_MODULE_otbApplication_EXTRA_DEPS
${CMAKE_CURRENT_SOURCE_DIR}/../Python.i
${CMAKE_CURRENT_SOURCE_DIR}/../PyCommand.i
itkPyCommand.h
otbLogOutputCallback.h
otbPythonLogOutput.h
OTBApplicationEngine)
SWIG_add_module( otbApplicationPy3 python ../otbApplication.i otbApplicationPYTHON_wrap.cxx ../python/itkPyCommand.cxx )
SWIG_add_module( otbApplicationPy3 python ../otbApplication.i otbApplicationPYTHON_wrap.cxx ../python/itkPyCommand.cxx ../python/otbPythonLogOutput.cxx)
SWIG_link_libraries( otbApplicationPy3 ${PYTHON3_LIBRARIES} OTBApplicationEngine )
set_target_properties(_otbApplicationPy3 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR})
set_target_properties(_otbApplicationPy3 PROPERTIES LIBRARY_OUTPUT_NAME _otbApplication)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment