summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel M. Pelt <D.M.Pelt@cwi.nl>2015-03-13 17:12:42 +0100
committerDaniel M. Pelt <D.M.Pelt@cwi.nl>2015-03-13 17:12:42 +0100
commitf21700e00e81538d5510973a51b8ae97fb4a24dd (patch)
treed1dcd07f2a29243e279fe2915c5db7814e99ea13
parente4614cf09b90cc9a0e38d370bb090a11f3877b33 (diff)
downloadastra-f21700e00e81538d5510973a51b8ae97fb4a24dd.tar.gz
astra-f21700e00e81538d5510973a51b8ae97fb4a24dd.tar.bz2
astra-f21700e00e81538d5510973a51b8ae97fb4a24dd.tar.xz
astra-f21700e00e81538d5510973a51b8ae97fb4a24dd.zip
Enable logging to Matlab window using callback function
Also introduces a mex initialize function that is called at the first invocation of any mex method.
-rw-r--r--build/linux/Makefile.in1
-rw-r--r--include/astra/Logging.h9
-rw-r--r--include/astra/clog.h53
-rw-r--r--matlab/mex/astra_mex.cpp3
-rw-r--r--matlab/mex/astra_mex_algorithm_c.cpp3
-rw-r--r--matlab/mex/astra_mex_c.cpp3
-rw-r--r--matlab/mex/astra_mex_data2d_c.cpp3
-rw-r--r--matlab/mex/astra_mex_data3d_c.cpp3
-rw-r--r--matlab/mex/astra_mex_log_c.cpp3
-rw-r--r--matlab/mex/astra_mex_matrix_c.cpp3
-rw-r--r--matlab/mex/astra_mex_projector3d_c.cpp3
-rw-r--r--matlab/mex/astra_mex_projector_c.cpp3
-rw-r--r--matlab/mex/mexInitFunctions.cpp24
-rw-r--r--matlab/mex/mexInitFunctions.h6
-rw-r--r--src/Logging.cpp8
15 files changed, 125 insertions, 3 deletions
diff --git a/build/linux/Makefile.in b/build/linux/Makefile.in
index f862114..49220df 100644
--- a/build/linux/Makefile.in
+++ b/build/linux/Makefile.in
@@ -216,6 +216,7 @@ TEST_OBJECTS=\
MATLAB_CXX_OBJECTS=\
matlab/mex/mexHelpFunctions.o \
matlab/mex/mexCopyDataHelpFunctions.o \
+ matlab/mex/mexInitFunctions.o \
matlab/mex/mexDataManagerHelpFunctions.o
MATLAB_MEX=\
diff --git a/include/astra/Logging.h b/include/astra/Logging.h
index 5695663..e822c24 100644
--- a/include/astra/Logging.h
+++ b/include/astra/Logging.h
@@ -75,7 +75,7 @@ public:
* @param ...
* Any additional format arguments.
*/
- static void debug(const char *sfile, int sline, const char *fmt, ...);
+ static void debug(const char *sfile, int sline, const char *fmt, ...);
static void info(const char *sfile, int sline, const char *fmt, ...);
static void warn(const char *sfile, int sline, const char *fmt, ...);
static void error(const char *sfile, int sline, const char *fmt, ...);
@@ -143,6 +143,13 @@ public:
static void disableScreen();
static void disableFile();
+ /**
+ * Set callback function for logging to screen.
+ * @return whether callback was set succesfully.
+ *
+ */
+ static bool setCallbackScreen(void (*cb)(const char *msg, size_t len));
+
};
}
diff --git a/include/astra/clog.h b/include/astra/clog.h
index 4d8e39d..3b7e18b 100644
--- a/include/astra/clog.h
+++ b/include/astra/clog.h
@@ -231,6 +231,32 @@ int clog_set_date_fmt(int id, const char *fmt);
*/
int clog_set_fmt(int id, const char *fmt);
+/**
+ * Set the callback function.
+ *
+ * @param cb
+ * The new callback function.
+ *
+ * @return
+ * Zero on success, non-zero on failure.
+ */
+int clog_set_cb(int id, void (*cb)(const char *msg, size_t len));
+
+/**
+ * Set the file descriptor.
+ *
+ * @param id
+ * The identifier of the logger.
+ *
+ * @param fd
+ * The new file descriptor.
+ *
+ * @return
+ * Zero on success, non-zero on failure.
+ */
+int clog_set_fd(int id, int fd);
+
+
/*
* No need to read below this point.
*/
@@ -257,6 +283,9 @@ struct clog {
/* Tracks whether the fd needs to be closed eventually. */
int opened;
+
+ /* Callback function for each log message. */
+ void (*cb)(const char *msg, size_t len);
};
void _clog_err(const char *fmt, ...);
@@ -314,6 +343,7 @@ clog_init_fd(int id, int fd)
strcpy(logger->fmt, CLOG_DEFAULT_FORMAT);
strcpy(logger->date_fmt, CLOG_DEFAULT_DATE_FORMAT);
strcpy(logger->time_fmt, CLOG_DEFAULT_TIME_FORMAT);
+ logger->cb = NULL;
_clog_loggers[id] = logger;
return 0;
@@ -345,6 +375,16 @@ clog_set_level(int id, enum clog_level level)
}
int
+clog_set_fd(int id, int fd)
+{
+ if (_clog_loggers[id] == NULL) {
+ return 1;
+ }
+ _clog_loggers[id]->fd = fd;
+ return 0;
+}
+
+int
clog_set_time_fmt(int id, const char *fmt)
{
struct clog *logger = _clog_loggers[id];
@@ -392,6 +432,18 @@ clog_set_fmt(int id, const char *fmt)
return 0;
}
+int
+clog_set_cb(int id, void (*cb)(const char *msg, size_t len))
+{
+ struct clog *logger = _clog_loggers[id];
+ if (logger == NULL) {
+ _clog_err("clog_set_cb: No such logger: %d\n", id);
+ return 1;
+ }
+ logger->cb = cb;
+ return 0;
+}
+
/* Internal functions */
size_t
@@ -563,6 +615,7 @@ _clog_log(const char *sfile, int sline, enum clog_level level,
return;
}
result = write(logger->fd, message, strlen(message));
+ if (logger->cb) logger->cb(message,strlen(message));
if (result == -1) {
_clog_err("Unable to write to log file: %s\n", strerror(errno));
}
diff --git a/matlab/mex/astra_mex.cpp b/matlab/mex/astra_mex.cpp
index 0eb5662..4bf42dd 100644
--- a/matlab/mex/astra_mex.cpp
+++ b/matlab/mex/astra_mex.cpp
@@ -28,6 +28,7 @@ $Id$
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "astra/Globals.h"
@@ -104,6 +105,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == std::string("version")) {
astra_mex_version(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_algorithm_c.cpp b/matlab/mex/astra_mex_algorithm_c.cpp
index 669af8c..e4afa63 100644
--- a/matlab/mex/astra_mex_algorithm_c.cpp
+++ b/matlab/mex/astra_mex_algorithm_c.cpp
@@ -32,6 +32,7 @@ $Id$
*/
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "astra/Globals.h"
#define USE_MATLAB_UNDOCUMENTED
@@ -325,6 +326,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == "create") {
astra_mex_algorithm_create(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_c.cpp b/matlab/mex/astra_mex_c.cpp
index 760bd51..4a331f5 100644
--- a/matlab/mex/astra_mex_c.cpp
+++ b/matlab/mex/astra_mex_c.cpp
@@ -33,6 +33,7 @@ $Id$
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "astra/Globals.h"
@@ -128,6 +129,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == std::string("version")) {
astra_mex_version(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_data2d_c.cpp b/matlab/mex/astra_mex_data2d_c.cpp
index 5f79e98..9576896 100644
--- a/matlab/mex/astra_mex_data2d_c.cpp
+++ b/matlab/mex/astra_mex_data2d_c.cpp
@@ -32,6 +32,7 @@ $Id$
*/
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include <list>
@@ -635,6 +636,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == std::string("get")) {
astra_mex_data2d_get(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_data3d_c.cpp b/matlab/mex/astra_mex_data3d_c.cpp
index 0a3f85d..32b0ba7 100644
--- a/matlab/mex/astra_mex_data3d_c.cpp
+++ b/matlab/mex/astra_mex_data3d_c.cpp
@@ -32,6 +32,7 @@ $Id$
*/
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "mexCopyDataHelpFunctions.h"
#include "mexDataManagerHelpFunctions.h"
@@ -371,6 +372,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// 3D data
if (sMode == std::string("create")) {
astra_mex_data3d_create(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_log_c.cpp b/matlab/mex/astra_mex_log_c.cpp
index 14ae391..79fe3d5 100644
--- a/matlab/mex/astra_mex_log_c.cpp
+++ b/matlab/mex/astra_mex_log_c.cpp
@@ -32,6 +32,7 @@ $Id$
*/
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "astra/Logging.h"
@@ -278,6 +279,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == "debug") {
astra_mex_log_debug(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_matrix_c.cpp b/matlab/mex/astra_mex_matrix_c.cpp
index 01ad08b..aa31383 100644
--- a/matlab/mex/astra_mex_matrix_c.cpp
+++ b/matlab/mex/astra_mex_matrix_c.cpp
@@ -32,6 +32,7 @@ $Id$
*/
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include <list>
@@ -412,6 +413,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == std::string("get")) {
astra_mex_matrix_get(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_projector3d_c.cpp b/matlab/mex/astra_mex_projector3d_c.cpp
index 5381cf6..c3b547f 100644
--- a/matlab/mex/astra_mex_projector3d_c.cpp
+++ b/matlab/mex/astra_mex_projector3d_c.cpp
@@ -33,6 +33,7 @@ $Id$
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "astra/Globals.h"
@@ -403,6 +404,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == "create") {
astra_mex_projector3d_create(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/astra_mex_projector_c.cpp b/matlab/mex/astra_mex_projector_c.cpp
index 58cd953..204ba8e 100644
--- a/matlab/mex/astra_mex_projector_c.cpp
+++ b/matlab/mex/astra_mex_projector_c.cpp
@@ -34,6 +34,7 @@ $Id$
#include <mex.h>
#include "mexHelpFunctions.h"
+#include "mexInitFunctions.h"
#include "astra/AstraObjectManager.h"
#include "astra/Projector2D.h"
@@ -476,6 +477,8 @@ void mexFunction(int nlhs, mxArray* plhs[],
return;
}
+ initASTRAMex();
+
// SWITCH (MODE)
if (sMode == "create") {
astra_mex_projector_create(nlhs, plhs, nrhs, prhs);
diff --git a/matlab/mex/mexInitFunctions.cpp b/matlab/mex/mexInitFunctions.cpp
new file mode 100644
index 0000000..d8a50d7
--- /dev/null
+++ b/matlab/mex/mexInitFunctions.cpp
@@ -0,0 +1,24 @@
+#include <mex.h>
+#include "astra/Logging.h"
+
+bool mexIsInitialized=false;
+
+/**
+ * Callback to print log message to Matlab window.
+ *
+ */
+void logCallBack(const char *msg, size_t len){
+ mexPrintf(msg);
+}
+
+/**
+ * Initialize mex functions.
+ *
+ */
+void initASTRAMex(){
+ if(mexIsInitialized) return;
+ if(!astra::CLogger::setCallbackScreen(&logCallBack)){
+ mexErrMsgTxt("Error initializing mex functions.");
+ }
+ mexIsInitialized=true;
+}
diff --git a/matlab/mex/mexInitFunctions.h b/matlab/mex/mexInitFunctions.h
new file mode 100644
index 0000000..f16e9c9
--- /dev/null
+++ b/matlab/mex/mexInitFunctions.h
@@ -0,0 +1,6 @@
+#ifndef _INC_ASTRA_MEX_INITFUNCTIONS
+#define _INC_ASTRA_MEX_INITFUNCTIONS
+
+void initASTRAMex();
+
+#endif \ No newline at end of file
diff --git a/src/Logging.cpp b/src/Logging.cpp
index 9011d37..9d7c219 100644
--- a/src/Logging.cpp
+++ b/src/Logging.cpp
@@ -124,8 +124,7 @@ void CLogger::_setLevel(int id, log_level m_eLevel)
void CLogger::setOutputScreen(int fd, log_level m_eLevel)
{
_assureIsInitialized();
- clog_free(0);
- clog_init_fd(0, fd);
+ clog_set_fd(0, fd);
_setLevel(0,m_eLevel);
}
@@ -169,6 +168,11 @@ CLogger::CLogger()
;
}
+bool CLogger::setCallbackScreen(void (*cb)(const char *msg, size_t len)){
+ _assureIsInitialized();
+ return clog_set_cb(0,cb)==0;
+}
+
bool CLogger::m_bEnabledScreen = true;
bool CLogger::m_bEnabledFile = true;
bool CLogger::m_bFileProvided = false;