From 652ab6f3babc221cc42dad4d5ad7610365b9ea0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ne=C4=8Dada?= Date: Wed, 27 Mar 2019 09:03:24 +0200 Subject: [PATCH] Debug message types Former-commit-id: 666a031026a363b15c01ca8cdc174fbf152dee8e --- qpms/error.c | 22 ++++++++++++++++++++++ qpms/qpms_c.pyx | 21 +++++++++++++++++++++ qpms/qpms_cdefs.pxd | 8 ++++++++ qpms/qpms_error.h | 19 ++++++++++++++++++- qpms/scatsystem.c | 6 +++--- 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/qpms/error.c b/qpms/error.c index 2a8ec18..f64ee9c 100644 --- a/qpms/error.c +++ b/qpms/error.c @@ -3,6 +3,16 @@ #include #include +qpms_dbgmsg_flags qpms_dbgmsg_enabled = QPMS_DBGMSG_MISC; + +qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types) { + return (qpms_dbgmsg_enabled |= types); +} + +qpms_dbgmsg_flags qpms_dbgmsg_disable(qpms_dbgmsg_flags types) { + return (qpms_dbgmsg_enabled &= ~types); +} + void qpms_pr_error(const char *fmt, ...) { fflush(stdout); va_list ap; @@ -68,6 +78,18 @@ void qpms_pr_debug_at_flf(const char *filename, unsigned int linenum, fflush(stderr); } +void qpms_debug_at_flf(const char *filename, unsigned int linenum, + const char *func, qpms_dbgmsg_flags type, + const char *fmt, ...) { + if (!(type & qpms_dbgmsg_enabled)) return; + fprintf(stderr, "%s:%u, %s: ", filename, linenum, func); + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fputc('\n', stderr); + fflush(stderr); +} //void qpms_error_at_line(const char *filename, unsigned int linenum, // const char *fmt, ...); diff --git a/qpms/qpms_c.pyx b/qpms/qpms_c.pyx index f434c8a..81f2a67 100644 --- a/qpms/qpms_c.pyx +++ b/qpms/qpms_c.pyx @@ -34,6 +34,27 @@ class VSWFNorm(enum.IntEnum): TAYLOR = QPMS_NORMALISATION_TAYLOR TAYLOR_CS = QPMS_NORMALISATION_TAYLOR_CS +try: + class DebugFlags(enum.IntFlag): # Should be IntFlag if python version >= 3.6 + MISC = QPMS_DBGMSG_MISC + THREADS = QPMS_DBGMSG_THREADS + has_IntFlag = True +except AttributeError: # For old versions of enum, use IntEnum instead + class DebugFlags(enum.IntEnum): + MISC = QPMS_DBGMSG_MISC + THREADS = QPMS_DBGMSG_THREADS + has_IntFlag = False + +def dbgmsg_enable(qpms_dbgmsg_flags types): + flags = qpms_dbgmsg_enable(types) + return DebugFlags(flags) if has_IntFlag else flags +def dbgmsg_disable(qpms_dbgmsg_flags types): + flags = qpms_dbgmsg_disable(types) + return DebugFlags(flags) if has_IntFlag else flags +def dbgmsg_active(): + flags = qpms_dbgmsg_enable(0) + return DebugFlags(flags) if has_IntFlag else flags + import math # for copysign in crep methods #import re # TODO for crep methods? diff --git a/qpms/qpms_cdefs.pxd b/qpms/qpms_cdefs.pxd index 00fdfcb..9c61760 100644 --- a/qpms/qpms_cdefs.pxd +++ b/qpms/qpms_cdefs.pxd @@ -87,6 +87,14 @@ cdef extern from "qpms_types.h": bint owns_m # FIXME in fact bool # maybe more if needed +cdef extern from "qpms_error.h": + ctypedef enum qpms_dbgmsg_flags: + QPMS_DBGMSG_MISC + QPMS_DBGMSG_THREADS + qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types) + qpms_dbgmsg_flags qpms_dbgmsg_disable(qpms_dbgmsg_flags types) + + cdef extern from "indexing.h": qpms_uvswfi_t qpms_tmn2uvswfi(qpms_vswf_type_t t, qpms_m_t m, qpms_l_t n) qpms_errno_t qpms_uvswfi2tmn(qpms_uvswfi_t u, qpms_vswf_type_t* t, qpms_m_t* m, qpms_l_t* n) diff --git a/qpms/qpms_error.h b/qpms/qpms_error.h index 8f80796..1548d3a 100644 --- a/qpms/qpms_error.h +++ b/qpms/qpms_error.h @@ -25,9 +25,26 @@ void qpms_pr_debug_at_flf(const char *filename, unsigned int linenum, // const char *fmt, ...); +typedef enum { + QPMS_DBGMSG_MISC = 1, + QPMS_DBGMSG_THREADS = 2 // Multithreading-related debug messages. +} qpms_dbgmsg_flags; + +void qpms_debug_at_flf(const char *filename, unsigned int linenum, + const char *func, + qpms_dbgmsg_flags type, + const char *fmt, ...); + +extern qpms_dbgmsg_flags qpms_dbgmsg_enabled; + +qpms_dbgmsg_flags qpms_dbgmsg_disable(qpms_dbgmsg_flags types); +qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types); + + + #define QPMS_WARN(msg, ...) qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__) -#define QPMS_DEBUG(msg, ...) qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__) +#define QPMS_DEBUG(type, msg, ...) qpms_debug_at_flf(__FILE__,__LINE__,__func__,type,msg, ##__VA_ARGS__) #define QPMS_CRASHING_MALLOC(pointer, size) {(pointer) = malloc(size); if(!pointer && (size)) qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__, "Allocation of %zd bytes for " #pointer " failed.", (size_t) (size));} diff --git a/qpms/scatsystem.c b/qpms/scatsystem.c index 952fd4e..3a62ed3 100644 --- a/qpms/scatsystem.c +++ b/qpms/scatsystem.c @@ -1427,16 +1427,16 @@ complex double *qpms_scatsys_build_modeproblem_matrix_irrep_packed_parallelR( long nthreads; if (qpms_scatsystem_nthreads_override > 0) { nthreads = qpms_scatsystem_nthreads_override; - QPMS_WARN("Using overriding value of %ld thread(s).", + QPMS_DEBUG(QPMS_DBGMSG_THREADS, "Using overriding value of %ld thread(s).", nthreads); } else { nthreads = sysconf(_SC_NPROCESSORS_ONLN); if (nthreads < 1) { - QPMS_WARN("_SC_NPROCESSORS_ONLN returned %ld, using %ld thread(s) instead.", + QPMS_DEBUG(QPMS_DBGMSG_THREADS, "_SC_NPROCESSORS_ONLN returned %ld, using %ld thread(s) instead.", nthreads, qpms_scatsystem_nthreads_default); nthreads = qpms_scatsystem_nthreads_default; } else { - QPMS_DEBUG("_SC_NRPOCESSORS_ONLN returned %ld.", nthreads); + QPMS_DEBUG(QPMS_DBGMSG_THREADS, "_SC_NRPOCESSORS_ONLN returned %ld.", nthreads); } } pthread_t thread_ids[nthreads];