From f21700e00e81538d5510973a51b8ae97fb4a24dd Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Fri, 13 Mar 2015 17:12:42 +0100 Subject: 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. --- include/astra/Logging.h | 9 ++++++++- include/astra/clog.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) (limited to 'include') 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; @@ -344,6 +374,16 @@ clog_set_level(int id, enum clog_level level) return 0; } +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) { @@ -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)); } -- cgit v1.2.3